• 22. SpringBoot 集成 Mybatis


    1. 引入Mybatis的maven 依赖

    <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
    </dependency>

     2. MybatisGenerator自动生成Mapper、dao、model

    3. MapperDao上标注@Mapper注解

    @Mapper
    public interface UserMapper {
        int deleteByPrimaryKey(Integer id);
    
        int insert(User record);
    
        int insertSelective(User record);
    
        User selectByPrimaryKey(Integer id);
    
        int updateByPrimaryKeySelective(User record);
    
        int updateByPrimaryKey(User record);
    }

     4. 配置文件中指明mybatis的配置文件位置

     UserMapper.xml配置文件方式实现时,需要制定配置文件位置

    #MapperDaoInterface若是用注解的方式实现SQL的话就用不着/mapper/*.xml配置
    mybatis:
      mapper-locations: classpath:mybatis/mapper/*.xml
      config-location: classpath:mybatis/mybatis-config.xml
      configuration:
        map-underscore-to-camel-case: true #开启驼峰标识 表:user_name  --->  JavaBean:userName
    
    #配置com.example.jdbc.dao包下的mapper接口类的日志级别,配上此日志控制台会打印SQL
    logging:
      level:
        com:
          example:
            jdbc:
              dao: debug

    注意:2.1.3.RELEASE版本的springboot中,mybatis.configuration 和mybatis.configLocation 配置项不可以一起使用

    Caused by: java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not specified with together
        at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:405) ~[mybatis-spring-2.0.0.jar:2.0.0]
        at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:541) ~[mybatis-spring-2.0.0.jar:2.0.0]
        at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:150) ~[mybatis-spring-boot-autoconfigure-2.0.0.jar:2.0.0]
        at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$99dc0108.CGLIB$sqlSessionFactory$2(<generated>) ~[mybatis-spring-boot-autoconfigure-2.0.0.jar:2.0.0]
        at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$99dc0108$$FastClassBySpringCGLIB$$677faa16.invoke(<generated>) ~[mybatis-spring-boot-autoconfigure-2.0.0.jar:2.0.0]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$99dc0108.sqlSessionFactory(<generated>) ~[mybatis-spring-boot-autoconfigure-2.0.0.jar:2.0.0]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        ... 57 common frames omitted

    5. 不用配置文件,用注解

    @Mapper
    public interface UserMapper {
    
        @Delete("delete from user where id = #{id}")
        int deleteByPrimaryKey(Integer id);
    
        @Insert("insert into user(name,age,sex) values(#{name},#{age},#{sex})")
        int insert(User record);
    
    
        int insertSelective(User record);
    
        @Select("select * from user where id = #{id}")
        User selectByPrimaryKey(Integer id);
    
        int updateByPrimaryKeySelective(User record);
    
        @Update("update user set name=#{name}, age=#{age}, sex=#{sex}")
        int updateByPrimaryKey(User record);
    }

    @MapperScan注解与@Mapper注解选其一用即可

    直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦。

    通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,可以解决mapper类没有在Spring Boot主程序的情况

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    @MapperScan("com.example.jdbc.dao")
    public class SpringBootJdbcApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootJdbcApplication.class, args);
        }
    }
    @MapperScan 支持多个包扫描和包名模糊匹配
    @MapperScan({"com.kfit.demo","com.kfit.user"}) 
    @MapperScan({"com.kfit.*.mapper","org.kfit.*.mapper"}) 
  • 相关阅读:
    Vue 2.0学习(三)指令与事件
    Vue 2.0学习(二)数据绑定
    Vue 2.0学习(一)简介
    大数据入门学习(一):初识大数据
    Knockout.js(四):自定义绑定
    03 Python基础
    02 测试环境的搭建
    01 自动化测试基础
    第29章 项目10:DIY街机游戏
    第27章 项目8:使用XML-RPC进行文件共享
  • 原文地址:https://www.cnblogs.com/guchunchao/p/10059208.html
Copyright © 2020-2023  润新知