spring boot 1.5.4 整合 mybatis(十二)

您所在的位置:网站首页 mybatis与spring整合 spring boot 1.5.4 整合 mybatis(十二)

spring boot 1.5.4 整合 mybatis(十二)

#spring boot 1.5.4 整合 mybatis(十二)| 来源: 网络整理| 查看: 265

上一篇:spring boot 1.5.4 整合log4j2(十一)

 

Spring Boot集成Mybatis

更多更详细的配置参考文件:application.properties和《SpringBoot之application配置详解》(新版本新增属性缺失)  或参考官网http://projects.spring.io/spring-boot/

 

Spring Boot集成Mybatis有两种方式:

 

方式一:传统的引入外部资源配置的方式,方便对mybatis的控制;

方式二:mybatis官方提供spring-boot整合的方式。

 

这里,还是使用UserMapper类和userMapper.xml文件分离的做法。关于mapper.xml的sql语句可以直接集成到Mapper接口中。详见第4章节:将SQL语句集成到UserMapper接口类中

 

1      方式一:整合mybatis资源1.1    新建spring-boot-mybatis项目

spring-boot-mybatis项目源码地址:

spring-boot相关项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址:https://github.com/wyait/spring-boot-1.5.4.git

项目整体结构:

spring boot 1.5.4 整合 mybatis(十二)_springboot1.5.4

1.2    pom.xml  

   4.0.0

  

     

      org.springframework.boot

      spring-boot-starter-parent

      1.5.4.RELEASE

  

   com.wyait.boot

   spring-boot-mybatis

   0.0.1-SNAPSHOT

   war

  

     

       

        org.springframework.boot

        spring-boot-starter-web

       

          

              spring-boot-starter-logging

              org.springframework.boot

          

       

     

     

       

        org.mybatis.spring.boot

        mybatis-spring-boot-starter

        1.1.1

     

     

        mysql

        mysql-connector-java

     

     

     

        com.github.pagehelper

        pagehelper

        4.1.0

     

     

     

        org.springframework.boot

        spring-boot-starter-tomcat

       

        provided

     

     

       

        org.apache.tomcat.embed

        tomcat-embed-jasper

        provided

     

     

        javax.servlet

        jstl

     

     

        org.springframework.boot

        spring-boot-starter-log4j

       

        1.3.2.RELEASE

     

 

     

     

        io.springfox

        springfox-swagger2

        2.6.1

     

     

        io.springfox

        springfox-swagger-ui

        2.6.1

     

     

     

        org.springframework.boot

        spring-boot-devtools

        true

       

     

  

  

     

       

          

           org.springframework.boot

           spring-boot-maven-plugin

          

       

     

  

1.3    Application.java

// 这是一个配置Spring的配置类

@Configuration

// @SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置,自动扫描该类同级包以及子包。

@SpringBootApplication

public class Application {

 

   publicstatic void main(String[] args) {

      //启动spring boot应用

      SpringApplicationsa = new SpringApplication(Application.class);

      //禁用devTools热部署

      System.setProperty("spring.devtools.restart.enabled","false");

      //禁用命令行更改application.properties属性

      sa.setAddCommandLineProperties(false);

      sa.run(args);

   }

}

1.4    mybatis相关配置类:集成pageHelper分页插件,并开启事务

 

@Configuration

@EnableTransactionManagement

// 开启注解事务支持

public class MybatisConfigimplements TransactionManagementConfigurer {

   //spring容器管理,可以直接注入使用

   @Autowired

   DataSourcedataSource;

 

   @Bean(name= "sqlSessionFactory")

   publicSqlSessionFactory sqlSessionFactoryBean() {

      SqlSessionFactoryBeanbean = new SqlSessionFactoryBean();

      bean.setDataSource(dataSource);

      bean.setTypeAliasesPackage("com.wyait.boot.pojo");

 

      //分页插件

      PageHelperpageHelper = new PageHelper();

      Propertiesproperties = new Properties();

      properties.setProperty("reasonable","true");

      properties.setProperty("supportMethodsArguments","true");

      properties.setProperty("returnPageInfo","check");

      properties.setProperty("params","count=countSql");

      pageHelper.setProperties(properties);

 

      //添加插件

      bean.setPlugins(newInterceptor[] { pageHelper });

 

      //添加XML目录

      ResourcePatternResolverresolver = new PathMatchingResourcePatternResolver();

      try{

        bean.setMapperLocations(resolver

              .getResources("classpath:mybatis/*.xml"));

        returnbean.getObject();

      }catch (Exception e) {

        e.printStackTrace();

        thrownew RuntimeException(e);

      }

   }

 

   @Bean

   publicSqlSessionTemplate sqlSessionTemplate(

        SqlSessionFactorysqlSessionFactory) {

      returnnew SqlSessionTemplate(sqlSessionFactory);

   }

 

   //开启注解事务

   @Bean

   @Override

   publicPlatformTransactionManager annotationDrivenTransactionManager() {

      returnnew DataSourceTransactionManager(dataSource);

   }

}

1.5    TODO 编写实体类、service、mapper、mapper.xml1.6    启动,测试

TODO

 

2      方式二:mybatis整合spring-boot

mybatis-spring-boot项目源码地址:

spring-boot相关项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址:https://github.com/wyait/spring-boot-1.5.4.git

2.1    新建mybatis-spring-boot工程

项目结构:

spring boot 1.5.4 整合 mybatis(十二)_mybatis_02

Application.java

// 这是一个配置Spring的配置类

@Configuration

// @SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置,自动扫描该类同级包以及子包。

@SpringBootApplication

//@MapperScan(basePackages ="com.wyait.boot.dao")

public class Application {

 

   publicstatic void main(String[] args) {

      //启动spring boot应用

      SpringApplicationsa = new SpringApplication(Application.class);

      //禁用devTools热部署

      System.setProperty("spring.devtools.restart.enabled","false");

      //禁用命令行更改application.properties属性

      sa.setAddCommandLineProperties(false);

      sa.run(args);

   }

}

 

2.2    pom.xml

   4.0.0

  

     

      org.springframework.boot

      spring-boot-starter-parent

      1.5.4.RELEASE

  

   com.wyait.mybatis

   mybatis-spring-boot

   0.0.1-SNAPSHOT

   war

  

     

       

         org.springframework.boot

        spring-boot-starter-web

       

          

              spring-boot-starter-logging

              org.springframework.boot

          

        

     

     

       

        org.mybatis.spring.boot

        mybatis-spring-boot-starter

        1.1.1

     

     

        mysql

        mysql-connector-java

     

     

     

        com.github.pagehelper

        pagehelper-spring-boot-starter

        1.1.1

     

     

     

     

     

        org.springframework.boot

        spring-boot-starter-tomcat

       

        provided

     

     

       

        org.apache.tomcat.embed

        tomcat-embed-jasper

        provided

     

     

        javax.servlet

        jstl

     

     

        org.springframework.boot

        spring-boot-starter-log4j

       

        1.3.2.RELEASE

     

 

     

     

        io.springfox

        springfox-swagger2

        2.6.1

     

     

        io.springfox

        springfox-swagger-ui

        2.6.1

     

     

     

        org.springframework.boot

        spring-boot-devtools

        true

       

     

  

  

     

       

          

           org.springframework.boot

           spring-boot-maven-plugin

          

       

     

  

2.3    application.properties配置:集成pageHelper,指定mapper.xml路径

# mysql

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 使用druid连接池  需要注意的是:spring.datasource.type旧的spring boot版本是不能识别的。

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# mybatis

mybatis.type-aliases-package=com.wyait.boot.pojo

mybatis.mapper-locations=classpath:mapper/*.xml

# 通用mapper配置

#mapper.mappers=com.wyait.boot.dao

#mapper.not-empty=false

#mapper.identity=MYSQL

# pagehelper

pagehelper.helperDialect=mysql

pagehelper.reasonable=true

pagehelper.supportMethodsArguments=true

pagehelper.returnPageInfo=check

pagehelper.params=count=countSql

2.4    UserMapper

TODO 详见项目

mybatis-spring-boot项目源码地址:https://git.oschina.net/wyait/springboot1.5.4.git

在接口上添加@Mapper注解即可或者在Application上添加扫描:@MapperScan(basePackages = "com.wyait.boot.dao")

2.5    启动,测试结果

spring boot 1.5.4 整合 mybatis(十二)_springboot整合mybatis_03

3      将SQL语句集成到UserMapperXML接口类中

写法:

@Mapper

public interface UserMapperXML {

  @Select("SELECT * FROM USERWHERE NAME = #{name}")

  public UserfindUser(@Param("name") String name);

 

  @Select("SELECT * FROMUSER")

  public ListfindAllUser();

 

  /**

   *

   * @描述:更新用户信息

   * @创建人:wyait

   * @创建时间:2017年6月29日下午1:33:09

   * @param user

   * @return

   */

  @Update("update user setage=#{age} where id=#{id}")

  public int update(User user);

}

 

更多用法可进行百度。

4      总结

项目:mybatis-spring-boot整合了Mapper接口分离Sql在xml中的写法和注解sql写法。详见项目源码。

spring-boot相关项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址:https://github.com/wyait/spring-boot-1.5.4.git

 

spring boot系列文章:

spring boot 1.5.4 概述(一)

spring boot 1.5.4 入门和原理(二)

spring boot 1.5.4 之web开发(三)

spring boot 1.5.4 整合JSP(四)

spring boot 1.5.4 集成devTools(五)

spring boot 1.5.4 集成JdbcTemplate(六)

spring boot 1.5.4 集成spring-Data-JPA(七)

spring boot 1.5.4 配置文件详解(八)

spring boot 1.5.4 统一异常处理(九)

spring boot 1.5.4 定时任务和异步调用(十)

spring boot 1.5.4 整合log4j2(十一)

spring boot 1.5.4 整合 mybatis(十二)

spring boot 1.5.4 整合 druid(十三)

spring boot 1.5.4 之监控Actuator(十四)

spring boot 1.5.4 整合webService(十五)

spring boot 1.5.4 整合redis、拦截器、过滤器、监听器、静态资源配置(十六)

spring boot 1.5.4 整合rabbitMQ(十七)

spring boot 1.5.4 集成Swagger2构建Restful API(十八)

spring boot 1.5.9 整合redis(十九)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3