码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis和spingboot整合

时间:2019-10-22 00:39:26      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:modify   for   use   别名   string   nec   sep   col   tst   

0. 导包

 <!-- 统一管理springboot相关的包 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <!-- controller和前端交互; springboot和前端整合 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- springboot和mybatis整合 -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.0</version>
    </dependency>
    <!-- mysql驱动包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- 红辣椒 -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

1.创建 application.properties 文件

#数据库配置
spring.datasource.password=ladeng
spring.datasource.url=jdbc:mysql://localhost:3306/schoolSystem??serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#时间格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#输出日志的级别
logging.level.com.xmg.mapper=debug

2. 创建一个启动类

需要贴人的标签

@MapperScan("需要扫描的包名")
@SpringBootApplication      这个是启动标签

3.  Controller类

@Controller   // 可以在一个类中定义多个接口
@RequestMapping("/stuController")
public class StuController {
     @Autowired
     private StuService stuService;
    
    @RequestMapping("/list")
    @ResponseBody     // json格式
    public List<Stu> list() {
        return stuService.list();
    }
}

4. Mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="Mapper接口全限定名">

    <resultMap type="domain全限定名" id="stuMap">
        <!--
         column:数据库例
         property:可以给column里面写的参数取别名
         javaType: java的类型
         jdbcType:数据库的类型
         -->
        <id column="id" property="id" javaType="java.lang.Long" jdbcType="BIGINT"/>
        <result column="name" property="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result column="age" property="age" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result column="course_id" property="courseId" javaType="java.lang.Long" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" javaType="java.sql.Date" jdbcType="DATE"/>
        <result column="modify_time" property="modifyTime" javaType="java.sql.Date" jdbcType="DATE"/>
    </resultMap>

    <sql id="filedId" >id, name, age, course_id, create_time</sql>

    <insert id="addStu" parameterType="com.xmg.domain.Stu" >
        insert into stu (name, age, course_id, create_time)
        values (#{name}, #{age}, #{courseId}, #{createTime})
    </insert>

    <select id="getStu" resultMap="stuMap" >
        select
        <include refid="filedId" />
        from stu
        <!--
        prefix: 在前面添加where
        suffixOverrides:如果后面没有条件就去除and
         -->
        <trim prefix="where" suffixOverrides="and" >
            <if test="id != null">
                 id = #{id} and
            </if>
            <if test="name2 != null and name2 != ‘‘" >
                 name = #{name2} and
            </if>
        </trim>
        limit 1
    </select>

    <select id="selectByNameList" resultType="com.xmg.domain.Stu" >
        select
        <include refid="filedId" />
        from stu
        <trim prefix="where name in ">
            <!--
            open: 最开始拼接 ‘(‘
            close: 结束后拼接 ‘)‘
            separator: 用什么分割
             -->
            <foreach collection="nameList" item="name" open="(" close=")" separator=",">
                #{name}
            </foreach>
        </trim>
    </select>
</mapper>

 

Mybatis和spingboot整合

标签:modify   for   use   别名   string   nec   sep   col   tst   

原文地址:https://www.cnblogs.com/zengqinghong/p/11717089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!