惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
小众软件
小众软件
J
Java Code Geeks
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
L
LangChain Blog
博客园 - 司徒正美
量子位
Y
Y Combinator Blog
C
Check Point Blog
T
Tailwind CSS Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
V
V2EX
阮一峰的网络日志
阮一峰的网络日志

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
springboot(3)整合mybatis
祈雨的笔记 · 2018-01-22 · via 祈雨的笔记

1、maven依赖

向pom.xml添加如下依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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)添加如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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&amp;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层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@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两种异常时回滚事物。所以实际开发需要指定为当发生任何异常时均回滚事务。

1
2
3
4
5
6
7
8
9
10
11
12
@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路径及子路径下。

这里写图片描述