spring boot 1.5.4 整合 mybatis(十二)

2024-05-11 04:35:37

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

Spring Boot集成Mybatis

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

Spring Boot集成Mybatis有两种方式:

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

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

这里,还是使用UserMapper类和userMapper.xml文件分离的做法。关于mapper.xmlsql语句可以直接集成到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

项目整体结构:

wKioL1nNqVCAi8xuAABEF4cxZ-w299.png

1.2pom.xml

<projectxmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<!--spring boot项目的parent -->

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.4.RELEASE</version>

</parent>

<groupId>com.wyait.boot</groupId>

<artifactId>spring-boot-mybatis</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<dependencies>

<dependency>

<!--spring boot 引入Web模块。自动配置:tomcatspringmvcjackson -->

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<exclusions>

<exclusion>

<artifactId>spring-boot-starter-logging</artifactId>

<groupId>org.springframework.boot</groupId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<!--spring-boot整合mybatis-->

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<!--MyBatis提供了拦截器接口,我们可以实现自己的拦截器,将其作为一个plugin装入到SqlSessionFactory中。 Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。

Github项目地址:https://github.com/pagehelper/Mybatis-PageHelper -->

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>4.1.0</version>

</dependency>

<!--tomcat 的支持. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<!--添加<scope>provided</scope>,因为provided表明该包只在编译和测试的时候用 -->

<scope>provided</scope>

</dependency>

<dependency>

<!--jsp页面支持 -->

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-log4j</artifactId>

<!--这里需要指定版本,否则报错【有可能是spring-boot-1.5.4.RELEASE版本没有管理log4j版本的原因】 -->

<version>1.3.2.RELEASE</version>

</dependency>

<!--spring boot集成Swagger2-->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.6.1</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.6.1</version>

</dependency>

<!--devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false或者视图解析器设置缓存为false来实现),

实现类文件热部署(类文件修改后不会立即生效,待编译后生效),实现对属性文件的热部署。devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<optional>true</optional>

<!--optional=true,依赖不会传递,该项目依赖devtools;之后依赖SpringBoot1项目的项目如果想要使用devtools,需要重新引入 -->

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<!--配置spring bootmaven插件 -->

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<!--<configuration> fork : 如果没有该项配置,devtools不会起作用,即应用不会restart 【实测:可以不配置】

<fork>true</fork></configuration> -->

</plugin>

</plugins>

</build>

</project>

1.3Application.java

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

@Configuration

// @SpringBootApplicationSpring 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.4mybatis相关配置类:集成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.5TODO 编写实体类、servicemappermapper.xml

1.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工程

项目结构:

wKiom1nNqZ3zPl2gAABMwU48hU0067.png

Application.java

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

@Configuration

// @SpringBootApplicationSpring 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.2pom.xml

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<!--spring boot项目的parent -->

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.4.RELEASE</version>

</parent>

<groupId>com.wyait.mybatis</groupId>

<artifactId>mybatis-spring-boot</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<dependencies>

<dependency>

<!--spring boot 引入Web模块。自动配置:tomcatspringmvcjackson -->

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<exclusions>

<exclusion>

<artifactId>spring-boot-starter-logging</artifactId>

<groupId>org.springframework.boot</groupId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<!--spring-boot整合mybatis-->

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<dependency>

<!--pageHelper分页插件 -->

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper-spring-boot-starter</artifactId>

<version>1.1.1</version>

</dependency>

<!-- MyBatis提供了拦截器接口,我们可以实现自己的拦截器,将其作为一个plugin装入到SqlSessionFactory中。 Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。

Github项目地址:https://github.com/pagehelper/Mybatis-PageHelper -->

<!--<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>4.1.0</version>

</dependency>-->

<!--tomcat 的支持. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<!--添加<scope>provided</scope>,因为provided表明该包只在编译和测试的时候用 -->

<scope>provided</scope>

</dependency>

<dependency>

<!--jsp页面支持 -->

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-log4j</artifactId>

<!--这里需要指定版本,否则报错【有可能是spring-boot-1.5.4.RELEASE版本没有管理log4j版本的原因】 -->

<version>1.3.2.RELEASE</version>

</dependency>

<!--spring boot集成Swagger2-->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.6.1</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.6.1</version>

</dependency>

<!--devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false或者视图解析器设置缓存为false来实现),

实现类文件热部署(类文件修改后不会立即生效,待编译后生效),实现对属性文件的热部署。devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<optional>true</optional>

<!--optional=true,依赖不会传递,该项目依赖devtools;之后依赖SpringBoot1项目的项目如果想要使用devtools,需要重新引入 -->

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<!--配置spring bootmaven插件 -->

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<!--<configuration> fork : 如果没有该项配置,devtools不会起作用,即应用不会restart 【实测:可以不配置】

<fork>true</fork></configuration> -->

</plugin>

</plugins>

</build>

</project>

2.3application.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.4UserMapper

TODO 详见项目

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

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

2.5启动,测试结果

wKiom1nNqazwtE-3AAC5wRqZyxM274.png

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

写法:

@Mapper

public interface UserMapperXML {

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

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

@Select("SELECT * FROMUSER")

public List<User>findAllUser();

/**

*

* @描述:更新用户信息

* @创建人:wyait

* @创建时间:2017629下午1:33:09

* @param user

* @return

*/

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

public int update(User user);

}

更多用法可进行百度。

4 总结

项目:mybatis-spring-boot整合了Mapper接口分离Sqlxml中的写法和注解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(十九

转载于:https://blog.51cto.com/wyait/1969626

spring boot 1.5.4 整合 mybatis(十二)相关推荐

  1. Spring Boot入门(08):整合Mybatis访问MySQL实现增删改查 | 超级详细,建议收藏

    1. 前言

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

    上一篇:spring boot 1.5.4 整合webService(十五) 1      Spring Boot整合redis和缓存 Spring Boot中除了对常用的关系型数据库提供了优秀的自动 ...

  3. mybatis 配置_配置Mybatis在Spring Boot工程中的整合

    配置Mybatis在Spring Boot工程中的整合包,设置mybatis的实体类别名,输出执行sql语句配置项. 分析: 添加启动器依赖: 配置Mybatis:实体类别名包,日志,映射文件等: 配 ...

  4. Spring Boot Cache使用与整合

    参考: 史上最全的Spring Boot Cache使用与整合 Spring Cache扩展:注解失效时间+主动刷新缓存 项目地址 使用本地Caffeine缓存 引入依赖包 <dependenc ...

  5. Spring Boot之基于Redis实现MyBatis查询缓存解决方案

    转载自 Spring Boot之基于Redis实现MyBatis查询缓存解决方案 1. 前言 MyBatis是Java中常用的数据层ORM框架,笔者目前在实际的开发中,也在使用MyBatis.本文主要 ...

  6. Spring Boot (八): Mybatis 增强工具 MyBatis-Plus

    1. 简介 在上一篇文章<Spring Boot (七): Mybatis极简配置> 中我们介绍了在 Spring Boot 中 Mybatis 的基础使用方式,其中有一部分美中不足的是 ...

  7. 响应式久草编程基础教程:久草Spring Boot 与 Lettuce 在线整合

    本文主要介绍响应式编程访问 Redis,以及 Spring Boot 与 Lettuce 的整合使用. Lettuce 是可扩展性线程安全的 Redis 客户端,用于同步.异步和响应式使用.如果多个线 ...

  8. Spring boot 搭建个人博客系统(二)——登录注册功能

    Spring boot 搭建个人博客系统(二)--登录注册功能 一直想用Spring boot 搭建一个属于自己的博客系统,刚好前段时间学习了叶神的牛客项目课受益匪浅,乘热打铁也主要是学习,好让自己熟 ...

  9. 轻量级数据库sqlite,spring boot+sqlite的配置详解 (二)

    轻量级数据库sqlite,spring boot+sqlite的配置详解 (二) 轻量级数据库sqlite,spring boot+sqlite的配置详解 (一) 首先,需要创建一个spring bo ...

最新文章

  1. 中国在国际上首次提出全液态量子器件与计算技术概念
  2. 按文件类型获取其图标
  3. React-Native系列Android——Native与Javascript通信原理(一)
  4. (*长期更新)软考网络工程师学习笔记——Section 10 网络安全
  5. 力扣438.找到字符串中所有字母异位词(JavaScript)
  6. 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
  7. Postgres 数据库大批量单表导入数据引发性能故障的处理
  8. np.array(image)的作用
  9. Unity移动的三种方式
  10. android 飞行模式 源代码,android 定时进入飞行模式 例子
  11. 用FFmpeg快捷加文字水印
  12. 安卓手机内外SD卡互换
  13. 公司规定所有接口都用 POST 请求,这是为什么?
  14. Hu矩的形状特征提取---matlab实现
  15. oracle opm系统,ORACLE EBS OPM标准功能培训资料-OPM库存-V1.0
  16. java游戏熊猫修牧场的_教你玩转牧场积分,角色练满25修不是梦想
  17. 一个球从100米落下,每次落到地弹上一半的路程,问n次经过的路程
  18. 又一智能汽车新赛道进入「量产期」,谁已率先分走这块蛋糕?
  19. 用一句话证明你是一名程序员——烫烫烫烫汤汤汤汤汤
  20. 百度网盘良心了!非会员轻松上30MB/s,且用且珍惜!

热门文章

  1. 浅谈数据库优化方案--表和SQL
  2. JavaEE 要懂的小事:二、图解 Cookie(小甜饼)
  3. 学习 .net 的一些主要网站
  4. vijos p1063(迎春舞会之集体舞)
  5. C# 读取Excel中的时间
  6. httpClient 处理SSL问题
  7. hadoop2.x常用端口及定义方法
  8. 神经网络与机器学习 笔记—小规模和大规模学习问题
  9. POJ1018贪心(多路归并的想法)
  10. 【错误记录】Android Gradle 配置报错 ( gradle.properties 配置到 BuildConfig 中需要注意类型转换 | 位置: 类 BuildConfig )