在SpringBoot的官網好像沒有找到mybatis的整合指南,可能是國外比較流行使用JPA吧,所以寫下此文章來記錄整合過程的記錄,此過程包含簡單的CRUD以及后面進階的整合之前學過的Thymeleaf模板引擎,簡單做了一個查詢數據渲染頁面,相關的源代碼已上傳到Github上,連接放在文章末尾
整合的項目目錄結構
整合thymeleaf實現簡單查詢的頁面
第一步導入pom.xml依賴:Mybatis和Mysql兩個核心依賴,這里我額外導多一個lombok依賴(可選)
<!--mybatis依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--mysql依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
第二步編寫application.yml配置文件,寫上要連接的數據庫的配置信息
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
這里默認使用的SpringBoot內置的數據源,在2.0版本后使用的是HikariDataSource
這個數據源,mysql驅動在2.0版本后使用的mysql8.0驅動,對應的驅動為com.mysql.cj.jdbc.Driver
,還應注意連接的url要加上時區serverTimezone=UTC
,否則會報時區錯誤,除非你修改了mysql默認的時區
第三步創建數據庫的表,按照配置是在test數據庫下創建student表
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
第四步創建實體類Student映射到mysql的表student
@Data//使用這個注解就默認幫我們生成getter和setter以及toString等
@NoArgsConstructor //lombok生成無參數構造函數
public class Student {
private Integer id;
private String Name;
private Integer age;
}
這里就用到開頭的導入的lombok依賴了,需要配合IDEA的lombok插件,目的就是簡化實體類的getter和setter,constructor,toString方法的編寫
第五步編寫StudentMapper
@Mapper
public interface StudentMapper {
@Select("select * from student where id=#{id}")
Student findStudentById(Integer id);
@Insert("insert into student(name,age)values(#{name},#{age})")
void insertStudent(Student student);
@Update("update student set name=#{name} where id=#{id}")
void updateStudent(Student student);
@Delete("delete from student where id=#{id}")
void deleteStudentById(Integer id);
}
整合第六步編寫測試類來測試寫的方法邏輯是否正確
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class StudentMapperTest {
@Autowired
private StudentMapper studentMapper;
@Test
public void findStudentById() {
Student student = studentMapper.findStudentById(1);
Assert.assertEquals(20, student.getAge().intValue());
}
@Rollback
@Test
public void insertStudent() {
Student student = new Student();
student.setName("bobiStudent");
student.setAge(24);
studentMapper.insertStudent(student);
}
}
編寫測試類時,可以使用快捷鍵ctrl+shift+T
快速生成上面四個方法,在測試過程中加入@Transactional事務注解和@Rollback注解,為得是在測試過程中保證測試的數據不會被添加到數據庫的Student表中,這里如果想看到在Thymeleaf中有結果顯示,那么測試時候添加的數據就不要用上這個注解
在CRUD的基礎整合進階版提升
當我們完成一套流程的CRUD的時候,是否覺得太簡單呢?其實這些字段遠遠比不上實際開發過程的封裝字段的復雜程度,例如開發中常用Map來存儲實體類中的字段,又或者把多個對象封裝成列表的形式輸出,所以我自己在上面的CRUD基礎上,增加了兩個進階的方法。
進階優化一:查詢所有的學生
//查詢所有的學生的方法
@Select("select id,name, age from student")
List<Student> findAll();
進階優化二:通過Map的是形式插入學生類
@Insert("insert into student(name, age) values(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
int insertStudentByMap(Map<String, Object> map);
對應的測試方法
@Test
public void findAll(){
List<Student> studentList = studentMapper.findAll();
for(Student student : studentList){
System.out.println(student);
}
}
@Test
public void insertStudentByMap(){
HashMap<String, Object> userMap = new HashMap<>();
userMap.put("name","公眾號CodeLuoJay");
userMap.put("age",21);
int row = studentMapper.insertStudentByMap(userMap);
Assert.assertEquals(1,row);
}
進階優化三:編寫controller,查詢所有學生整合LayUI表格輸出到頁面
@Controller
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@GetMapping("/student/getStudent")
@ResponseBody
public List<Student> getUser(){
return studentMapper.findAll();
}
@GetMapping("/list")
public String index(){
return "list";
}
}
list.html中內容:
<table class="layui-hide" id="demo"></table>
<script src="https://www.layuicdn.com/layui/layui.js" charset="utf-8"></script>
<!-- 注意:如果你直接復制所有代碼到本地,上述js路徑需要改成你本地的 -->
<script>
layui.use('table', function(){
var table = layui.table;
//展示已知數據
table.render({
elem: '#demo'
,url:'/student/getStudent'
,cellMinWidth: 80 //全局定義常規單元格的最小寬度,layui 2.2.1 新增
,parseData: function (res) {
console.log(res);
return{
"code": 0,
"msg":"",
"count":10,
"data":res
}
}
,cols: [
[
{field:'id', width:100, title: 'id'},
{field:'name', width:240, title: '用戶名'}
,{field:'age', width:240, title: '性別'}
]
]
});
});
</script>
我在controller中對應寫了一個list頁面訪問請求數據,然后將查詢出來的數據以JSON格式輸出到list.html中,最后利用CDN加速下的layui表格渲染出數據,對應的頁面輸出結果:
整合Mybatis過程中遇到過的坑及解決辦法
坑1:spring boot + Thymeleaf + layui table 渲染報錯 Could not parse as expression:
原因: [[…]]之間的表達式在Thymeleaf被認為是內聯表達式
解決辦法:把兩個[[ ]]隔開相應的距離,不讓Thymeleaf解析為內聯表達式
參考鏈接:[[thymeleaf渲染layui.js的“ col:[]]”里面的內容失敗
坑2:layui,返回的數據不符合規范,正確的成功狀態碼 (code) 應為:0
原因:layui table數據綁定需要特定格式的字符串,需要在JSON對象中封裝如下格式
{
"code": 0,
"msg":"",
"count":10,
"data":result//你封裝的字符串
}
layui,返回的數據不符合規范,正確的成功狀態碼 (code) 應為:0
坑3:intellij idea報錯Could not autowire. No beans of 'UserMapper' type found.
原因:IDEA自動檢查bean注入誤報的錯誤
參考鏈接:intellij idea報錯Could not autowire. No beans of 'UserMapper' type found
坑4: 連接數據庫時報錯The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized
原因: 出錯的原因是mysql的時區值設置的不正確mysql默認的時區值是美國,中國的時區要比美國晚8小時,需要采用+8:00的格式。
參考鏈接:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized
如果不想改數據庫的時區的話,可以在url中加入使用的時區serverTimezone=UTC
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
文章配套源碼
🔨Github:springboot-mybatis
如果文章對你有用,請給star!