我们这一一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件。然后再使用阿里巴巴提供的开源连接池druid,这个连接池的好处我就不说了,集合了所有连接池的好处,并且还提供了监控等功能,加大了可扩展性等等。
1.创建一个springboot项目:
2.可以看到的是我们除了引入web依赖之外还引入了三个依赖,分别是MySQL,JDBC,Mybatis,我们如下观看一下完整的依赖情况:
1
2
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
5
6 com.example
7 springboot-mybatis02
8 0.0.1-SNAPSHOT
9 jar
10
11 springboot-mybatis02
12 Demo project for Spring Boot
13
14
15 org.springframework.boot
16 spring-boot-starter-parent
17 2.0.1.RELEASE
18
19
20
21
22 UTF-8
23 UTF-8
24 1.8
25
26
27
28
29 org.springframework.boot
30 spring-boot-starter-jdbc
31
32
33
34
35 org.springframework.boot
36 spring-boot-starter-web
37
38
39
40
41 org.mybatis.spring.boot
42 mybatis-spring-boot-starter
43 1.3.2
44
45
46
47
48 mysql
49 mysql-connector-java
50 runtime
51
52
53
54
55
56 com.alibaba
57 druid-spring-boot-starter
58 1.1.1
59
60
61
62
63 org.springframework.boot
64 spring-boot-devtools
65
66
67
68
69 org.springframework.boot
70 spring-boot-starter-thymeleaf
71
72
73
74
75
76 com.github.pagehelper
77 pagehelper-spring-boot-starter
78 4.1.6
79
80
81
82 org.springframework.boot
83 spring-boot-starter-test
84 test
85
86
87
88
89
90
91 org.springframework.boot
92 spring-boot-maven-plugin
93
94
95
96
97 org.mybatis.generator
98 mybatis-generator-maven-plugin
99 1.3.2
100
101 ${basedir}/src/main/resources/generator/generatorConfig.xml
102 true
103 true
104
105
106
107
108
109
110
3.依赖引入完成以后我们再对application.properties配置文件进行编辑:
1 #设置访问端口2 server.port=80
3
4 #thymeleaf配置,这里是可以省略的,因为默认配置已经足够5 #关闭缓存,及时刷新页面,这一点很重要6 spring.thymeleaf.cache=false
7 #注释的部分是Thymeleaf默认的配置,如有其它需求可以自行更改8 #spring.thymeleaf.prefix=classpath:/templates/
9 #spring.thymeleaf.suffix=.html10 #spring.thymeleaf.mode=HTML511 #spring.thymeleaf.encoding=UTF-8
12 #spring.thymeleaf.servlet.content-type=text/html13
14
15 #设置热部署16 #开启热部署17 spring.devtools.restart.enabled=true
18 #重启范围19 spring.devtools.restart.additional-paths=src/main/java20
21 #设置数据源22 #数据库连接用户名23 spring.datasource.username=root24 #数据库连接密码25 spring.datasource.password=123
26 #驱动27 spring.datasource.driver-class-name=com.mysql.jdbc.Driver28 #数据库连接路径29 spring.datasource.url=jdbc:mysql://localhost:3306/bysj
30 #连接池类型31 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource32
33 #连接池配置,因为springboot默认是开启了连接池的,它有默认配置,这一段可以忽略34 # 初始化大小,最小,最大35 spring.datasource.initialSize=5
36 spring.datasource.minIdle=5
37 spring.datasource.maxActive=20
38 # 配置获取连接等待超时的时间39 spring.datasource.maxWait=60000
40 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒41 spring.datasource.timeBetweenEvictionRunsMillis=60000
42 # 配置一个连接在池中最小生存的时间,单位是毫秒43 spring.datasource.minEvictableIdleTimeMillis=300000
44 spring.datasource.validationQuery=SELECT 1FROM DUAL45 spring.datasource.testWhileIdle=true
46 spring.datasource.testOnBorrow=false
47 spring.datasource.testOnReturn=false
48 # 打开PSCache,并且指定每个连接上PSCache的大小49 spring.datasource.poolPreparedStatements=true
50 spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
51 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙52 spring.datasource.filters=stat,wall,log4j53 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录54 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
55
56 #配置mybatis57 mybatis.mapper-location=classpath:mapping/*.xml58 #全局的映射,不用在xml文件写实体类的全路径59 mybatis.type-aliases-package=com.zsl.pojo60 #开启驼峰映射
mybatis.configuration.map-underscore-to-camel-case=true
61 #配置分页插件62 #pagehelper分页插件pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
4.配置generator自动生成代码:文件放置的位置就在pom.xml文件里面一如插件的位置${basedir}/src/main/resources/generator/generatorConfig.xml
1
2 /p>
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
5.创建数据库以及表格
CREATE DATABASE bysj;
CREATE TABLE user(
user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(255) NOT NULL ,
password VARCHAR(255) NOT NULL ,
phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
6. 操作IDEA生成代码:Run-->Edit Configurations
回到下图所示页面的时候点击三角即可:
7.查看项目情况以及生成代码:
7.1:生成的mapper:
7.2:生成的pojo实体类:
7.3生成的mapper的xml文件:在这里务必要注意namespace是否正确,请注意查看这一点
1
2
3
4
5
6
7
8
9
10
11 user_id, user_name, password, phone12
select
from user;
13
14 select15
16 from user17 where user_id = #{userId,jdbcType=INTEGER}18
19
20 delete from user21 where user_id = #{userId,jdbcType=INTEGER}22
23
24 insert into user (user_id, user_name, password,25 phone)26 values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},27 #{phone,jdbcType=VARCHAR})28
29
30 insert into user31
32
33 user_id,34
35
36 user_name,37
38
39 password,40
41
42 phone,43
44
45
46
47 #{userId,jdbcType=INTEGER},48
49
50 #{userName,jdbcType=VARCHAR},51
52
53 #{password,jdbcType=VARCHAR},54
55
56 #{phone,jdbcType=VARCHAR},57
58
59
60
61 update user62
63
64 user_name = #{userName,jdbcType=VARCHAR},65
66
67 password = #{password,jdbcType=VARCHAR},68
69
70 phone = #{phone,jdbcType=VARCHAR},71
72
73 where user_id = #{userId,jdbcType=INTEGER}74
75
76 update user77 set user_name = #{userName,jdbcType=VARCHAR},78 password = #{password,jdbcType=VARCHAR},79 phone = #{phone,jdbcType=VARCHAR}80 where user_id = #{userId,jdbcType=INTEGER}81
82
7.4项目的总体结构:
7.5 自己用于测试分页,驼峰映射,实体类映射等配置的controller,service
1 packagecom.zsl.controller;2
3 importcom.zsl.pojo.User;4 importcom.zsl.service.impl.UserServiceImpl;5 importorg.springframework.stereotype.Controller;6 importorg.springframework.web.bind.annotation.RequestMapping;7 importorg.springframework.web.bind.annotation.ResponseBody;8
9 importjavax.annotation.Resource;10 importjava.util.List;11
12 @Controller13 public classUserontroller {14
15 @Resource16 privateUserServiceImpl userService;17
18 //增加用户
19 @ResponseBody20 @RequestMapping("/insertUser")21 publicString insertUser(User user){22 returnuserService.insertUser(user);23 }24
25 //查询所有的用户
26 @ResponseBody27 @RequestMapping("/selectAllUser")28 publicString getAllUser(){29 List list =userService.selectAllUser();30 System.out.println(list.size());31 System.out.println(list);32 System.out.println(list.get(1).getUserId());33 return null;34 }35 }
1 packagecom.zsl.service.impl;2
3 importcom.zsl.mapper.UserMapper;4 importcom.zsl.pojo.User;5 importcom.zsl.service.UserService;6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Service;8
9 importjava.util.List;10
11 @Service12 public class UserServiceImpl implementsUserService {13
14 @Autowired15 private UserMapper userMapper;//报错不影响
16 /*
17 * 增加/修改用户18 **/
19 @Override20 publicString insertUser(User user) {21 //增加用户
22 if(user.getUserId() == null){23 Integer i =userMapper.insert(user);24 if(i != 0 && i != null){25 return "success";26 }27 }28 return null;29 }30
31 @Override32 public ListselectAllUser() {33 //PageHelper.startPage(1, 3);
34 List list =userMapper.selectAllUser();35 returnlist;36 }37 }
注意:在实验的时候务必要在启动类上加上MapperScna注解,指定扫描mapper文件,或者可以在每个mapper文件上加mapper注解,不过这样太麻烦了: