

springboot(3)整合mybatis
source link: https://wakzz.cn/2018/01/22/springboot/(3)%E6%95%B4%E5%90%88mybatis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1、maven依赖
向pom.xml添加如下依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>8.5.23</version>
</dependency>
2、配置springboot
向application.properties(或者.yml)添加如下配置
#mybaits配置
mybatis.config-location=classpath:mybatis.xml
#等效于mybaits.xml的别名配置
#mybatis.type-aliases-package=com.example.demo.dao.entity
#等效于mybaits.xml的mapper扫描配置
#mybatis.mapper-locations=classpath:com/example/demo/dao/mapper/*.xml
#数据源配置,指定为mysql的c3p0数据源
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://192.168.100.25:6660/TXSMS?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
3、Dao层
@Repository
public class TestUserDao {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public TXDBUser getUserById(Integer id) {
return sqlSessionTemplate.selectOne("com.wxtx.dao.TestUserDao.selectUserById", id);
}
public List<TXDBUser> getUserList(TXDBUser user, PageBounds pageBounds) {
return sqlSessionTemplate.selectList("com.wxtx.dao.TestUserDao.selectUserList", user, pageBounds);
}
public List<TXDBUser> getPrinterList() {
return sqlSessionTemplate.selectList("com.wxtx.dao.TestUserDao.selectPrinter");
}
public void updateUser(TXDBUser user) {
sqlSessionTemplate.update("com.wxtx.dao.TestUserDao.updateUser", user);
}
}
注:此处的SqlSessionTemplate为springboot自动注册的bean,不需要开发者自己注册实现。
4、事物管理
通过@Transactional注解添加事务,@Transactional注解默认只在发生RuntimeException and Error两种异常时回滚事物。所以实际开发需要指定为当发生任何异常时均回滚事务。
@Service
@Transactional(rollbackFor=Throwable.class) //当发生任何异常时回滚事务
public class TestService {
@Autowired
private TestUserDao userDao;
public List<TXDBUser> getUserList(){
return userDao.getUserList(new TXDBUser(),new PageBounds());
}
}
5、注意事项
所有的xml文件都不能放在src/main/java路径下,否则打包部署的时候会找不到该路径下的所有xml文件。所有非java文件例如mybatis的mapper映射文件均放在src/main/resources路径及子路径下。
Recommend
-
116
本作品采用 知识共享署名 4.0 国际许可协议 进行许可。 本文作者:低调小熊猫 文章链接:
-
171
本博客猫叔的博客,转载请申明出处 本系列教程为 HMStrange项目 附带。 Auth: HMStrange-TIAN e-ma...
-
66
前言 说实话,这章本来不打算讲的,因为配置多数据源的网上有很多类似的教程。但是最近因为项目要用到分库分表,所以让我研究一下看怎么实现。我想着上一篇博客讲了多环境的配置,不同的环境调用不同的数据库,那接下...
-
30
1、创建工程环境 勾选Web、Mybatis、MySQL,如下 依赖如下 <dependency> <group...
-
19
SpringBoot系列(五)Mybatis整合 1. Mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用...
-
7
SpringBoot整合Mybatis增删改查 - 乐心湖's Blog | 技术小白的技术博客 乐心湖's Blog...
-
10
SpringBoot 整合 MybatisSpring Boot 2.4.5、MybatisSpringBoot 整合 Mybatis新建一个 Spring Initializr 项目创建项目的文件...
-
4
AutoConfiguredMapperScannerRegistrar://// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package com.baomidou.mybatisplus.spring.boot.star...
-
0
接上回 上一篇我们简单介绍了基于SpringBoot实现简单的Web开发,本节来看Web开发中必不可少的内容——数据持久化 先看项目结构:
-
4
SpringBoot中整合Shiro+Mybatis+Thymeleaf框架实现用户登录以及权限的验证(超级详细过程、效果演示) 精选 原创
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK