本節(jié)內(nèi)容擴展介紹下針對mybatis的增強工具mybatis-plus,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。
二話不多說,我們先寫編寫個簡單的例子,讓大家先初步的了解下mybatis-plus。
1.mybatis-plus初步實例
(1)創(chuàng)建一個spring boot web工程(具體創(chuàng)建過程就不再演示了,還不會的同學(xué)去看看spring boot專題第一節(jié)內(nèi)容)
(2)引入依賴
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
27
28
29
30
31
32
|
<!--web項目依賴--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!--validation表單校驗--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-validation</ artifactId > </ dependency > <!--mybatis-plus--> < dependency > < groupId >com.baomidou</ groupId > < artifactId >mybatis-plus-boot-starter</ artifactId > < version >3.4.0</ version > </ dependency > <!--mysql驅(qū)動--> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > </ dependency > <!--thymeleaf依賴包--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-thymeleaf</ artifactId > </ dependency > <!--lombok--> < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > |
(3)配置文件application.yml
1
2
3
4
5
6
7
|
# DataSource Config spring: datasource: username: root password: tx@mysql@2020 url: jdbc:mysql: //188 .131.233.55:3306 /spring_boot_topic driver-class-name: com.mysql.cj.jdbc.Driver(4) |
(4)實體類User
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.kinglead.demo.domain; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; ? @Data @TableName (value = "t_user" ) //指明數(shù)據(jù)庫表名 public class User { private Long id; private String name; private Integer age; private String email; } |
(5)創(chuàng)建Mapper接口
1
2
3
4
5
6
7
8
|
package com.kinglead.demo.mapper; ? import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.kinglead.demo.domain.User; ? //未來使用mybatis-plus的公共接口,必須繼承BaseMapper public interface UserMapper extends BaseMapper<User> { } |
(6)創(chuàng)建Service接口
UserService
1
2
3
4
5
6
|
package com.kinglead.demo.service; import com.kinglead.demo.domain.User; import java.util.List; public interface UserService { List<User> queryUserList(); } |
UserServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.kinglead.demo.service.impl; ? import com.kinglead.demo.domain.User; import com.kinglead.demo.mapper.UserMapper; import com.kinglead.demo.service.UserService; import org.springframework.stereotype.Service; ? import javax.annotation.Resource; import java.util.List; ? @Service public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public List<User> queryUserList() { //使用mybatis-plus公共查詢接口完成列表查詢 return userMapper.selectList( null ); } } |
(7)創(chuàng)建controller
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
27
|
package com.kinglead.demo.controller; ? import com.kinglead.demo.domain.User; import com.kinglead.demo.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; ? import javax.annotation.Resource; import java.util.List; ? @Controller @RequestMapping ( "/user" ) public class UserController { ? @Resource private UserService userService; ? @RequestMapping ( "/userList" ) public ModelAndView queryUserList(ModelAndView modelAndView){ List<User> userList = userService.queryUserList(); modelAndView.addObject( "userList" , userList); modelAndView.setViewName( "userList" ); return modelAndView; } ? } |
(8)用戶列表頁面
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<!DOCTYPE html> < html xmlns:th = "http://www.thymeleaf.org" > < head > < meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8" /> < title >用戶信息</ title > <!--<link rel="stylesheet" type="text/css" href="/css/common.css" rel="external nofollow" />--> < style type = "text/css" > table { border: 1px solid black; text-align: center; border-collapse: collapse; } table thead th { border: 1px solid black; } table tbody td { border: 1px solid black; } </ style > </ head > < body > < div > < h2 >用戶列表</ h2 > </ div > < table cellpadding = "0" cellspacing = "0" > < thead > < th >序號</ th > < th >編碼</ th > < th >用戶名</ th > </ thead > < tbody > < tr th:each = "entries,stat:${userList}" th:style = "' color: rgb(17, 119, 0);" >> < td th:text = "${stat.count}" ></ td > < td th:text = "${entries['id']}" ></ td > < td th:text = "${entries['name']}" ></ td > </ tr > </ tbody > </ table > </ body > </ html > |
(9)啟動類
1
2
3
4
5
6
7
8
9
10
11
|
package com.kinglead.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan ( "com.kinglead.demo.mapper" ) public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } } |
(10)測試訪問
小結(jié)
我們能看到mybatis-plus通過對mybatis的加強,能在不寫mapper.xml文件的情況下,完成簡單的CRUD的操作。看到這里,有的小伙伴可能會想,這和JPA有什么不一樣,直接用JPA不就好啦。
下面我們來對比下他們倆的區(qū)別:要說mybatis-plus,得先說mybatis。mybatis比較接近原生sql,要想使用的好,需要很好的sql基礎(chǔ),所有的數(shù)據(jù)庫操作都必須寫sql語句;JPA是對hibernate的封裝,提取了很多CRUD的公共方法,可以在不寫sql的情況下完成不復(fù)雜的CRUD;mybatis-plus是mybatis的增強工具,天生擁有mybatis的優(yōu)勢,也具備像JPA一樣擁有很多CRUD的公共方法,簡單的sql直接調(diào)用,不要編寫語句。
那我們該如何選擇他們呢?沒有哪個框架是最優(yōu)的,主要還是要根據(jù)實際項目情況而定。如果項目不復(fù)雜,涉及不到很多復(fù)雜的數(shù)據(jù)處理,那么建議考慮JPA。如果項目復(fù)雜,涉及到很多復(fù)雜的數(shù)據(jù)處理,那么建議考慮mybatis或mybatis-plus,在這基礎(chǔ)上,如果想簡化mybatis,可以考慮mybatis-plus。
本節(jié)內(nèi)容只是展示了mybatis-plus的冰山一角,它還有擁有很多特色功能,如支持主鍵字段生成、內(nèi)置分頁插件等。
源碼地址:https://github.com/kinglead2012/myblog
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/kinglead/p/13704678.html