springboot 整合Mybaites
上一節(jié)講到springboot 如何使用druid數(shù)據(jù)源連接mysql并使用JPA進行CRUD操作,這一節(jié)繼續(xù)關(guān)注持久化層,看看springboot 是如何整合Mybatise
00 新建一個maven java 項目,pom.xml 初始導(dǎo)入以下依賴:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot 熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--thymeleaf 模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--阿里數(shù)據(jù)源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!--mysql 依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>
01 以上是前一節(jié)中所使用到的依賴斯碌,pom.xml 導(dǎo)入mybaties-springboot依賴:
<!--mybaties-springboot 依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
02 通過mybaties逆向工程生成實體類炒嘲、dao亏钩,并移植到項目中:
person.jack
bean
Dept
DeptMapper.xml
Emp
EmpMapper.xml
dao
DeptMapper
EmpMapper
03 配置resources/application.properties,配置數(shù)據(jù)源
spring.thymeleaf.cache=false
logging.level.root=info
#### 配置默認數(shù)據(jù)源厉熟,并使用 阿里連接池 ###
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
######JPA 配置#####
# 聲明數(shù)據(jù)庫
spring.jpa.database=mysql
# 是否顯示SQL語句
spring.jpa.show-sql = true
# Hibernate 自動DDL 操作
# create 每次加載hibernate時都會刪除上一次的生成的表
# create-drop 每次加載hibernate時根據(jù)model類生成表汽抚,但是sessionFactory一關(guān)閉,表就自動刪除
# update 最常用的屬性儒士,第一次加載hibernate時根據(jù)model類會自動建立起表的結(jié)構(gòu)(前提是先建立好數(shù)據(jù)庫)
spring.jpa.hibernate.ddl-auto=update
#配置方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
04 在application.properties 配置mybatis *Mapper.xml 文件的位置:
### mybaties 配置 ###
#*Mapper.xml文件位置
mybatis.mapper-locations=classpath:/person/jack/bean/*.xml
05 在啟動類中使用@MapperScan 配置Mapper接口的位置:
@SpringBootApplication
/**配置 Mapper 接口的位置*/
@MapperScan("person.jack.dao")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
06 添加測試依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
07 編寫測試類:
@RunWith(SpringJUnit4ClassRunner.class) /*添加SpringJUnit支持,引入Spring-Test框架*/
@SpringBootTest(classes = App.class) /*指定Springboot啟動類啟動*/
public class TestMybatis {
@Autowired
private EmpMapper empMapper;
@Test
public void test(){
Emp emp = empMapper.selectByPrimaryKey(7788);
System.out.println(emp);
}
}
08 重寫實體類 Emp toString() 方法:
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
09 運行測試方法test():
#后臺打印
Emp{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=Sun Apr 19 00:00:00 CDT 1987, sal=3000, comm=null, deptno=20}
# 主鍵為 7788 的員工成功查詢,SpringBoot 成功整合Mybatis
10 Controller 測試花鹅,在EmpMapper.xml 中添加查詢,對應(yīng)添加到Dao:
#EmpMapper.xml
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from emp
</select>
#person/jack/dao/EmpMapper.java 添加抽象方法
List<Emp> selectAll();
11 編寫 person/jack/controller/EmpController
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpMapper empMapper;
@RequestMapping("/empList")
public String empList(Map map){
map.put("empList", empMapper.selectAll());
return "empList";
}
}
12 編寫empList.html 模板枫浙,使用th:each 迭代刨肃,#dates.format格式化輸出日期:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>員工列表</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td>序號</td>
<td>編號</td>
<td>姓名</td>
<td>工作</td>
<td>工資</td>
<td>獎金</td>
<td>入職日期</td>
</tr>
<tr th:each="emp,status:${empList}">
<td th:text="${status.count}"/>
<td th:text="${emp.empno} "/>
<td th:text="${emp.ename}"/>
<td th:text="${emp.job}"/>
<td th:text="${emp.sal}"/>
<td th:text="${emp.comm}"/>
<td th:text="${#dates.format(emp.hiredate, 'yyyy-MM-dd')}"/>
</tr>
</table>
</body>
</html>
13 瀏覽器訪問:http://localhost:8080/emp/empList,頁面打印
序號 | 編號 | 姓名 | 工作 | 工資 | 獎金 | 入職日期 |
---|---|---|---|---|---|---|
1 | 7369 | SMITH | CLERK | 800 | 1980-12-17 | |
2 | 7499 | ALLEN | SALESMAN | 1600 | 300 | 1981-02-20 |
3 | 7521 | WARD | SALESMAN | 1250 | 500 | 1981-02-22 |
4 | 7566 | JONES | MANAGER | 2975 | 1981-04-02 | |
5 | 7654 | MARTIN | SALESMAN | 1250 | 1400 | 1981-09-28 |
6 | 7698 | BLAKE | MANAGER | 2850 | 1981-05-01 | |
7 | 7782 | CLARK | MANAGER | 2450 | 1981-06-09 | |
8 | 7788 | SCOTT | ANALYST | 3000 | 1987-04-19 | |
9 | 7839 | KING | PRESIDENT | 5000 | 1981-11-17 | |
10 | 7844 | TURNER | SALESMAN | 1500 | 0 | 1981-09-08 |
11 | 7876 | ADAMS | CLERK | 1100 | 1987-05-23 | |
12 | 7900 | JAMES | CLERK | 950 | 1981-12-03 | |
13 | 7902 | FORD | ANALYST | 3000 | 1981-12-03 | |
14 | 7934 | MILLER | CLERK | 1300 | 1982-01-23 |
14 測試成功箩帚!
總結(jié)Springboot 整合mybaties 可分為以下幾步:
- 導(dǎo)入依賴 mybatis-spring-boot-starter
- application.properties 配置 application.properties
- 在啟動類中添加 @MapperScan 注解掃描Dao
- 注入Dao真友,使用!