Mybatis和spingboot整合

Posted 明亮且蛋糕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis和spingboot整合相关的知识,希望对你有一定的参考价值。

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整合的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合shiro系列-SpingBoot是如何将shiroFilter注册到servlet容器中的

Springboot整合druid

Spingboot整合Redis,用注解(@Cacheable@CacheEvict@CachePut@Caching)管理缓存

SpingBoot:整合Elasticsearch7.2.0

Spring+SpringMVC+MyBatis+Maven框架整合

Mybatis