12.SpringBoot+MyBatis(XML)+Druid
转自:https://www.cnblogs.com/MaxElephant/p/8108342.html
主要是在Spring Boot中集成MyBatis,可以选用基于注解的方式,也可以选择xml文件配置的方式。官方推荐使用xml文件配置。
springboot+mybatis+druid
1. 引入依赖
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!-- Mybatis --><dependency> <groupid>org.mybatis.spring.boot</groupid> mybatis-spring-boot-starter</artifactid> <version>1.1.1</version> <!-- 请不要使用1.0.0版本,因为还不支持拦截器插件 --></dependency><!-- druid阿里巴巴数据库连接池 --><dependency> <groupid>com.alibaba</groupid> druid</artifactid> <version>1.0.20</version></dependency><!-- MySql数据库驱动 --><dependency> <groupid> mysql</groupid> mysql-connector-java</artifactid> <version> 5.0.5</version></dependency> |
2. 在Mysql中创建Users表
Users表中包含id(BIGINT)、name(INT)、age(VARCHAR)字段。
3. 创建接口Mapper(不是类)和对应的XML文件
User实体类:
|
1
2
3
4
5
6
7
8
|
public class User { private long id; private String name; private Integer age; // 省略相应的 getter 与 setter 方法} |
UserDao接口:实现插入和查询操作
注意必须加上@Mapper的注解,不然@Autowired将注入失败。
|
1
2
3
4
5
6
7
|
<code><code>@Mapperpublic interface UserDao{ int insertUser(@Param("user") User user); User findByName(String name);}</code></code> |
@Mapper注解标记这个接口作为一个映射接口。真正实现映射的方法(XML文件)需要源对象作为参数,并返回目标对象。
UserMapper.xml文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<code><code><code><!--?xml version="1.0" encoding="UTF-8" ?--><mapper namespace="qg.fangrui.boot.dao.UserDao"> <!--目的:为Dao接口方法提供SQL语句--> <!--映射实体对象--> <resultmap id="UserResultMap" type="qg.fangrui.boot.model.User"> <id column="id" property="id"> <result column="name" property="name"> <result column="age" property="age"> </result></result></id></resultmap> <insert id="insertUser"> INSERT INTO users(name, age) VALUES (#{user.name}, #{user.age}) </insert> <select id="findByName" resulttype="User"> SELECT * FROM users WHERE name = #{name} </select></mapper></code></code></code> |
4. 配置文件
application.properties:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<code><code><code># 驱动配置信息spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.url = jdbc:mysql://127.0.0.1:3306/myboot?useUnicode=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456spring.datasource.driverClassName = com.mysql.jdbc.Driver#连接池的配置信息spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20spring.datasource.maxWait=60000spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20spring.datasource.filters=stat,wall,log4jspring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# MyBatis 配置mybatis.mapper-locations=classpath:mapper/*.xmlmybatis.type-aliases-package=qg.fangrui.boot.model</code></code></code> |
5. 调用测试:
一般情况下,我是用Controller层调用Service层,Service层调用Dao层。测试案例比较简单,我就不列了,只是简单展示一下相应的Controller。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<code><code><code>@RestController@RequestMapping("/test")public class TestController { @Autowired private UserService userService; @RequestMapping("/add") public String add(User user){ return String.valueOf(userService.add(user)); }}</code></code></code> |
效果图:
Postman测试图:
Druid监控图:
附录
补充
mybatis-spring-boot-starter的依赖树:
Mybatis 在 SpringBoot 中的配置:
* mybatis.mapper-locations:xml文件扫描位置
* mybatis.type-aliases-package:Model包扫描位置
* mybatis.config:mybatis-config.xml配置文件的路径
* mybatis.typeHandlersPackage:扫描typeHandlers的包
* mybatis.checkConfigLocation:检查配置文件是否存在
* mybatis.executorType:设置执行模式(SIMPLE, REUSE, BATCH),默认为SIMPLE
12.SpringBoot+MyBatis(XML)+Druid的更多相关文章
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- 搭建Springboot+mybatis+redis+druid
2019独角兽企业重金招聘Python工程师标准>>> 准备工作 JDK:1.8 使用技术:SpringBoot.Dubbo.Mybatis.Druid 开发工具:Intelj ID ...
- shardingsphere多数据源(springboot + mybatis+shardingsphere+druid)
org.springframeword.boot:spring-boot-starer-web: 2.0.4release io.shardingsphere:sharding-jdbc-spring ...
- 项目转移时发生的错误<springboot+mybatis(xml逆向工程自动生成)>
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'app ...
- (二十二)SpringBoot之使用Druid连接池以及SQL监控和spring监控
一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- SpringBoot之使用Druid连接池以及SQL监控和spring监控
一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- 3分钟搞定SpringBoot+Mybatis+druid多数据源和分布式事务
文章来自: https://blog.csdn.net/qq_29242877/article/details/79033287 在一些复杂的应用开发中,一个应用可能会涉及到连接多个数据源,所谓多数据 ...
- springboot+mybatis+druid+atomikos框架搭建及测试
前言 因为最近公司项目升级,需要将外网数据库的信息导入到内网数据库内.于是找了一些springboot多数据源的文章来看,同时也亲自动手实践.可是过程中也踩了不少的坑,主要原因是我看的文章大部分都是s ...
- SpringBoot:整合Druid、MyBatis
目录 简介 JDBC 导入依赖 连接数据库 CRUD操作 自定义数据源 DruidDataSource Druid 简介 配置数据源 配置 Druid 数据源监控 配置 Druid web 监控 fi ...
随机推荐
- web——前后端通信
前端向后台传输数据: 传输方法:post get 区别: (1)get:用于从服务器获取数据,将参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看 ...
- 【Henu ACM Round#14 A】Vitaly and Night
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 连续两个如果不全是0就递增cnt [代码] #include <bits/stdc++.h> using namespa ...
- 零基础学python-7.6 字符串格式化表达式
字符串格式化同意在一个单个的步骤中对一个字符串运行多个特定类型的替换 特别是给用户提示的时候,格式化很方便 实现方法: 1.格式化表达式,类似于c语音的printf 在表达式中,我们使用%二进制操作符 ...
- 最小生成树-并查集-Kruskal-zoj-2048-special judge
Highways description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a ...
- jquery14 on() trigger() : 事件操作的相关方法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 11.Axis客户端接收不同参数类型
转自:http://www.guance.com/708.html axis只支持简单类型的返回值.在这里逐一介绍axis的各种返回值接受. 1. axis接受基本类型,如int, string等 引 ...
- vue的指令在webstrom下报错
Preferences -> Editor -> Inspections找到XML,把 Unbound XML namespace prefix的勾去掉
- css3的新特性选择器-------属性选择器
自己学css的时候比较乱,这次趁着复习把css3的新特性选择器和css2以前不怎么用的选择器做一个总结 <div id="parent"> <p>I'm a ...
- 41.内存函数实现(memcpy,memset,memmove,memicmp,memchr.memccpy)
memcpy #include <stdio.h> #include <stdlib.h> #include <memory.h> void * mymemcpy( ...
- nohup---将程序以忽略挂起信号的方式运行起来
nohup nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. 在缺省情况下该作业的所 ...