一、添加所需依赖,当前完整的pom文件如下:
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
|
<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 6 .release</version> </parent> <groupid>org.rcddup</groupid> <artifactid>rcddup-custom</artifactid> <version> 1.0 . 0 </version> <properties> <java.version> 1.8 </java.version> <mybatis.version> 1.3 . 1 </mybatis.version> <mapper.version> 3.4 . 3 </mapper.version> <mybatis-generator.version> 1.3 . 3 </mybatis-generator.version> <mybatis-tk.version> 1.1 . 4 </mybatis-tk.version> <pagehelper.version> 1.2 . 1 </pagehelper.version> </properties> <dependencies> <!-- springboot - web --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!-- springboot - mybatis --> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>${mybatis.version}</version> </dependency> <!-- springboot - mybatis 逆向工程 --> <dependency> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-core</artifactid> <version>${mybatis-generator.version}</version> </dependency> <!-- mybatis 通用 mapper --> <dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper-spring-boot-starter</artifactid> <version>${mybatis-tk.version}</version> </dependency> <!-- mysql --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> </dependencies> <build> <plugins> <!-- mybatis 逆向工程 插件 --> <plugin> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-maven-plugin</artifactid> <version>${mybatis.generator.version}</version> <dependencies> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>${mysql.version}</version> </dependency> <dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper</artifactid> <version>${mapper.version}</version> </dependency> </dependencies> <configuration> <!-- 允许移动生成的文件 --> <verbose> true </verbose> <!-- 是否覆盖 --> <overwrite> true </overwrite> <!-- 配置文件 --> <configurationfile> ${basedir}/src/main/resources/generator/generatorconfig.xml </configurationfile> </configuration> </plugin> </plugins> </build> </project> |
mybtais 结合通用 mapper 非常好使,接下来看逆向工程的配置。
有关通用mapper的信息,请查看:https://github.com/abel533/mapper
二、generatorconfig.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
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype generatorconfiguration public "-//mybatis.org//dtd mybatis generator configuration 1.0//en" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorconfiguration> <!-- 引入 application.properties --> <properties resource= "application.properties" /> <!-- mybatis3simple:不生成 example相关类及方法 --> <context id= "mysql" targetruntime= "mybatis3simple" defaultmodeltype= "flat" > <property name= "beginningdelimiter" value= "`" /> <property name= "endingdelimiter" value= "`" /> <!-- 指定生成 mapper 的继承模板 --> <plugin type= "tk.mybatis.mapper.generator.mapperplugin" > <property name= "mappers" value= "${generator.mappers}" /> </plugin> <!-- 生成 javabean 对象重写 tostring方法 --> <plugin type= "org.mybatis.generator.plugins.tostringplugin" /> <!-- 生成 javabean 对象继承 serializable 类 --> <plugin type= "org.mybatis.generator.plugins.serializableplugin" /> <!-- 生成 javabean 对象重写 equals 和 hashcode 方法 --> <!-- <plugin type= "org.mybatis.generator.plugins.equalshashcodeplugin" /> --> <!-- jdbc 连接配置 --> <jdbcconnection driverclass= "${spring.datasource.driver-class-name}" connectionurl= "${spring.datasource.url}" userid= "${spring.datasource.username}" password= "${spring.datasource.password}" > </jdbcconnection> <javamodelgenerator targetpackage= "${generator.javamodel-targetpackage}" targetproject= "${generator.targetproject}" /> <sqlmapgenerator targetpackage= "${generator.sqlmap-targetpackage}" targetproject= "${generator.targetproject}" /> <javaclientgenerator targetpackage= "${generator.javaclient-targetpackage}" targetproject= "${generator.targetproject}" type= "xmlmapper" /> <!-- mysql 配置 --> <!-- <generatedkey column= "id" sqlstatement= "mysql" identity= "true" /> --> <!-- oracle 配置 --> <!-- <generatedkey column= "id" sqlstatement= "select seq_{1}.nextval from dual" identity= "false" type= "pre" /> --> <!-- tablename:数据库表名,domainobjectname:生成文件名 ,schema:数据源--> <table tablename= "user" > <generatedkey column= "id" sqlstatement= "mysql" identity= "true" /> </table> </context> </generatorconfiguration> |
我们将对应的信息都在 application.properties 中定义好,在这直接使用,application.properties如下:
1
2
3
4
5
6
|
# generator generator.targetproject=src/main/java generator.mappers=org.rcddup.app.common.base.ibasemapper generator.javamodel-targetpackage=org.rcddup.app.domain generator.sqlmap-targetpackage=org.rcddup.app.dao generator.javaclient-targetpackage=org.rcddup.app.dao |
三、编写生成mapper的集成接口ibasemapper
1
2
3
4
5
|
package org.rcddup.app.common.base; import tk.mybatis.mapper.common.mapper; import tk.mybatis.mapper.common.mysqlmapper; public interface ibasemapper<t> extends mapper<t>, mysqlmapper<t> { } |
集成的 mapper 以及 mysqlmapper 几乎包含了你所有的 crud方法。
四、使用逆向工程生成我们的第一个文件
右键项目 --> maven --> maven build --> goals: mybtais-generator:generate,点击 run.....
我们的usermapper以及usermapper.xml就生成了。
(1)usermapper.java
1
2
3
4
5
|
package org.rcddup.app.dao; import org.rcddup.app.common.base.ibasemapper; import org.rcddup.app.domain.user; public interface usermapper extends ibasemapper<user> { } |
(2)usermapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace= "org.rcddup.app.dao.usermapper" > <resultmap id= "baseresultmap" type= "org.rcddup.app.domain.user" > <!-- warning - @mbg .generated --> <id column= "id" jdbctype= "bigint" property= "id" /> <result column= "name" jdbctype= "varchar" property= "name" /> <result column= "age" jdbctype= "tinyint" property= "age" /> <result column= "status" jdbctype= "tinyint" property= "status" /> </resultmap> </mapper> |
是不是发现xml中都没有配置任何crud方法。
五、重新编写 userservice.java
我们原先编写的 userservice 现在我们用新生成的 usermapper 来重新编写。
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
|
package org.rcddup.app.service.impl; import java.util.list; import org.rcddup.app.dao.usermapper; import org.rcddup.app.domain.user; import org.rcddup.app.service.iuserservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import tk.mybatis.mapper.entity.example; @service public class userservice implements iuserservice{ @autowired private usermapper usermapper; @override public list<user> listuser() { return usermapper.selectall(); } @override public user getuserbyid( long id) { // 方法一:适用于任何字段的查询 // example example = new example(user.class); // example.createcriteria().andequalto("id", id); // list<user> list = usermapper.selectbyexample(example); // if (list != null && list.size() > 0) { // return list.get(0); // } else { // return null; // } // 方法二:只适用于主键字段 return usermapper.selectbyprimarykey(id); } } |
六、启动 app.java
输入:localhost:8080/user/get?id=1
输入:localhost:8080/user/list
好了,到这我们整合了mybatis逆向工程以及通用mapper。
总结
以上所述是小编给大家介绍的springboot整合mybatis逆向工程及 mybatis通用mapper实例详解 ,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
原文链接:http://www.cnblogs.com/rcddup/archive/2017/09/16/7530401.html