mybatis-plus(简称mp)是一个 mybatis 的增强工具,在 mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
中文文档 :http://baomidou.oschina.io/mybatis-plus-doc/#/
本文介绍包括
1)如何搭建
2)代码生成(controller、service、mapper、xml)
3)单表的crud、条件查询、分页 基类已经为你做好了
一、如何搭建
1. 首先我们创建一个 springboot 工程 --> https://start.spring.io/
2. maven 依赖
1
2
3
4
5
6
7
8
9
10
11
|
<dependency> <groupid>com.baomidou</groupid> <artifactid>mybatis-plus-boot-starter</artifactid> <version> 2.3 </version> </dependency> <!-- velocity 依赖,用于代码生成 --> <dependency> <groupid>org.apache.velocity</groupid> <artifactid>velocity-engine-core</artifactid> <version> 2.0 </version> </dependency> |
3. 配置(因为感觉太啰嗦,这里省略了数据源的配置)
application.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
mybatis-plus.mapper-locations=classpath:/mapper/*mapper.xml mybatis-plus.typealiasespackage=com.taven.web.springbootmp.entity mybatis-plus.global-config.id-type= 3 mybatis-plus.global-config.field-strategy= 2 mybatis-plus.global-config.db-column-underline= true mybatis-plus.global-config.key-generator=com.baomidou.mybatisplus.incrementer.oraclekeygenerator mybatis-plus.global-config.logic-delete-value= 1 mybatis-plus.global-config.logic-not-delete-value= 0 mybatis-plus.global-config.sql-injector=com.baomidou.mybatisplus.mapper.logicsqlinjector #这里需要改成你的类 mybatis-plus.global-config.meta-object-handler=com.taven.web.springbootmp.mymetaobjecthandler mybatis-plus.configuration.map-underscore-to-camel- case = true mybatis-plus.configuration.cache-enabled= false mybatis-plus.configuration.jdbc-type- for - null = null |
配置类 mybatisplusconfig
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import org.mybatis.spring.annotation.mapperscan; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import com.baomidou.mybatisplus.incrementer.h2keygenerator; import com.baomidou.mybatisplus.incrementer.ikeygenerator; import com.baomidou.mybatisplus.mapper.isqlinjector; import com.baomidou.mybatisplus.mapper.logicsqlinjector; import com.baomidou.mybatisplus.mapper.metaobjecthandler; import com.baomidou.mybatisplus.plugins.paginationinterceptor; import com.baomidou.mybatisplus.plugins.performanceinterceptor; import com.taven.web.springbootmp.mymetaobjecthandler; @enabletransactionmanagement @configuration @mapperscan ( "com.taven.web.springbootmp.mapper" ) public class mybatisplusconfig { /** * mybatis-plus sql执行效率插件【生产环境可以关闭】 */ @bean public performanceinterceptor performanceinterceptor() { return new performanceinterceptor(); } /* * 分页插件,自动识别数据库类型 多租户,请参考官网【插件扩展】 */ @bean public paginationinterceptor paginationinterceptor() { return new paginationinterceptor(); } @bean public metaobjecthandler metaobjecthandler() { return new mymetaobjecthandler(); } /** * 注入主键生成器 */ @bean public ikeygenerator keygenerator() { return new h2keygenerator(); } /** * 注入sql注入器 */ @bean public isqlinjector sqlinjector() { return new logicsqlinjector(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import com.baomidou.mybatisplus.mapper.metaobjecthandler; import org.apache.ibatis.reflection.metaobject; import org.slf4j.logger; import org.slf4j.loggerfactory; /** * 注入公共字段自动填充,任选注入方式即可 */ //@component public class mymetaobjecthandler extends metaobjecthandler { protected final static logger logger = loggerfactory.getlogger(application. class ); @override public void insertfill(metaobject metaobject) { logger.info( "新增的时候干点不可描述的事情" ); } @override public void updatefill(metaobject metaobject) { logger.info( "更新的时候干点不可描述的事情" ); } } |
二、代码生成
执行 junit 即可生成controller、service接口及实现、mapper及xml
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import org.junit.test; import com.baomidou.mybatisplus.generator.autogenerator; import com.baomidou.mybatisplus.generator.config.datasourceconfig; import com.baomidou.mybatisplus.generator.config.globalconfig; import com.baomidou.mybatisplus.generator.config.packageconfig; import com.baomidou.mybatisplus.generator.config.strategyconfig; import com.baomidou.mybatisplus.generator.config.rules.dbtype; import com.baomidou.mybatisplus.generator.config.rules.namingstrategy; /** * <p> * 测试生成代码 * </p> * * @author k神 * @date 2017/12/18 */ public class generatorserviceentity { @test public void generatecode() { string packagename = "com.taven.web.springbootmp" ; boolean servicenamestartwithi = false ; //user -> userservice, 设置成true: user -> iuserservice generatebytables(servicenamestartwithi, packagename, "cable" , "station" ); //修改为你的表名 } private void generatebytables( boolean servicenamestartwithi, string packagename, string... tablenames) { globalconfig config = new globalconfig(); string dburl = "jdbc:mysql://localhost:3306/communicate" ; datasourceconfig datasourceconfig = new datasourceconfig(); datasourceconfig.setdbtype(dbtype.mysql) .seturl(dburl) .setusername( "root" ) .setpassword( "root" ) .setdrivername( "com.mysql.jdbc.driver" ); strategyconfig strategyconfig = new strategyconfig(); strategyconfig .setcapitalmode( true ) .setentitylombokmodel( false ) .setdbcolumnunderline( true ) .setnaming(namingstrategy.underline_to_camel) .setinclude(tablenames); //修改替换成你需要的表名,多个表名传数组 config.setactiverecord( false ) .setenablecache( false ) .setauthor( "殷天文" ) .setoutputdir( "e:\\dev\\stsdev\\spring-boot-mp\\src\\main\\java" ) .setfileoverride( true ); if (!servicenamestartwithi) { config.setservicename( "%sservice" ); } new autogenerator().setglobalconfig(config) .setdatasource(datasourceconfig) .setstrategy(strategyconfig) .setpackageinfo( new packageconfig() .setparent(packagename) .setcontroller( "controller" ) .setentity( "entity" ) ).execute(); } // private void generatebytables(string packagename, string... tablenames) { // generatebytables(true, packagename, tablenames); // } } |
到这一步搭建已经基本完成了,下面就可以开始使用了!
三、使用 mybatis-plus
首先我们执行 上面的 generatecode() 会为我们基于 表结构 生成以下代码(xml是我手动移到下面的),service 和 mapper 已经继承了基类,为我们封装了很多方法,下面看几个简单的例子。
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
/** * <p> * 前端控制器 * </p> * * @author 殷天文 * @since 2018-05-31 */ @controller @requestmapping ( "/cable" ) public class cablecontroller { @autowired private cableservice cableservice; /** * list 查询测试 * */ @requestmapping ( "/1" ) @responsebody public object test1() { // 构造实体对应的 entitywrapper 对象,进行过滤查询 entitywrapper<cable> ew = new entitywrapper<>(); ew.where( "type={0}" , 1 ) .like( "name" , "王" ) .and( "core_number={0}" , 24 ) .and( "is_delete=0" ); list<cable> list = cableservice.selectlist(ew); list<map<string, object>> maps = cableservice.selectmaps(ew); system.out.println(list); system.out.println(maps); return "ok" ; } /** * 分页 查询测试 */ @requestmapping ( "/2" ) @responsebody public object test2() { // 构造实体对应的 entitywrapper 对象,进行过滤查询 entitywrapper<cable> ew = new entitywrapper<>(); ew.where( "type={0}" , 1 ) // .like("name", "王") .and( "core_number={0}" , 24 ) .and( "is_delete=0" ); page<cable> page = new page<>( 1 , 10 ); page<cable> pagerst = cableservice.selectpage(page, ew); return pagerst; } /** * 自定义查询字段 */ @requestmapping ( "/3" ) @responsebody public object test3() { object vl = null ; // 构造实体对应的 entitywrapper 对象,进行过滤查询 entitywrapper<cable> ew = new entitywrapper<>(); ew.setsqlselect( "id, `name`, " + "case type\n" + "when 1 then '220kv'\n" + "end typename" ) .where( "type={0}" , 1 ) // .like("name", "王") .where( false , "voltage_level=#{0}" , vl); //当vl 为空时,不拼接 page<cable> page = new page<>( 1 , 10 ); page<cable> pagerst = cableservice.selectpage(page, ew); return pagerst; } /** * insert */ @requestmapping ( "/4" ) @responsebody public object test4() { cable c = new cable(); c.setname( "测试光缆" ); cableservice.insert(c); return "ok" ; } /** * update */ @requestmapping ( "/5" ) @responsebody public object test5() { cable c = cableservice.selectbyid(22284l); c.setname( "测试光缆2222" ); c.settype( 1 ); cableservice.updatebyid(c); return "ok" ; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/8fbd22c3e981