最近在項目中應用到springboot與mybatis,在進行整合過程中遇到一些坑,在此將其整理出來,便于以后查閱與復習。
項目運行環(huán)境為:eclispe+jdk1.8+maven
搭建spring boot環(huán)境
首先建立maven project,在生成的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
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 2 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> </dependency> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version> 1.3 . 0 </version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <!--分頁插件--> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper-spring-boot-starter</artifactid> <version> 1.2 . 1 </version> </dependency> <!-- alibaba的druid數(shù)據(jù)庫連接池 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version> 1.0 . 29 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <!-- mybatis generator 自動生成代碼插件 --> <plugin> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-maven-plugin</artifactid> <version> 1.3 . 2 </version> <configuration> <configurationfile>${basedir}/src/main/resources/generator/generatorconfig.xml</configurationfile> <overwrite> true </overwrite> <verbose> true </verbose> </configuration> </plugin> </plugins> </build> |
配置好依賴后進行maven install,此時注意的坑:
坑一:啟動maven install報錯:
[error] failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.release:repackage (default) on project springboot-mybatis: execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.release:repackage failed: unable to find main class -> [help 1]
[error]
[error] to see the full stack trace of the errors, re-run maven with the -e switch.
[error] re-run maven using the -x switch to enable full debug logging.
[error]
[error] for more information about the errors and possible solutions, please read the following articles:
[error] [help 1] http://cwiki.apache.org/confluence/display/maven/pluginexecutionexception
報錯原因是沒有啟動類
解決方法:編寫啟動類main.java正常運行!
1
2
3
4
5
6
|
@springbootapplication public class main { public static void main(string[] args) { springapplication.run(main. class , args); } } |
坑二:啟動maven install報錯:
[error] failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project springboot-mybatis: compilation failure
[error] no compiler is provided in this environment. perhaps you are running on a jre rather than a jdk?
[error] -> [help 1]
[error]
[error] to see the full stack trace of the errors, re-run maven with the -e switch.
[error] re-run maven using the -x switch to enable full debug logging.
[error]
[error] for more information about the errors and possible solutions, please read the following articles:
[error] [help 1] http://cwiki.apache.org/confluence/display/maven/mojofailureexception
報錯原因:項目的java環(huán)境與電腦環(huán)境不符合,例如我的新建項目運行環(huán)境為j2se-1.5,報錯是在我將其改為jre1.8之后
解決方案:
右鍵項目——build path——configure build path——libraries——雙擊jre system libraries如下圖所示:
選擇alternate jre 如2處的下拉框只有jre,點擊3處的install jres,依次經(jīng)過add——standard vm——next——directory,選擇本機的jdk位置點擊finish
在install jres位置將默認勾選更改jdk,如下圖所示,并保存。
再次maven install項目正常。如下所示:
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 3.351 s
[info] finished at: 2018-09-05t21:20:48+08:00
[info] final memory: 23m/181m
[info] ------------------------------------------------------------------------
在src/main/resources新建文件:application.yml,內(nèi)容如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
server: port: 8080 spring: datasource: name: test url: jdbc:mysql: //localhost:3306/test username: root password: 123456 driver- class -name: com.mysql.jdbc.driver ## 該配置節(jié)點為獨立的節(jié)點 mybatis: mapper-locations: classpath:mapper/*.xml #pagehelper分頁插件 pagehelper: helperdialect: mysql reasonable: true supportmethodsarguments: true params: count=countsql |
至此springboot環(huán)境搭建完畢!
逆向工程應用
首先應該注意到在pom文件中有配置逆向工程xml文件的位置:src/main/resources/generator/generatorconfig.xml
因而在src/main/resources下新建generator文件夾,并建立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
|
<?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> <!-- 數(shù)據(jù)庫驅動:選擇你的本地硬盤上面的數(shù)據(jù)庫驅動包--> <classpathentry location= "e:\plugins\maven\repo\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar" /> <context id= "db2tables" targetruntime= "mybatis3" > <commentgenerator> <property name= "suppressdate" value= "true" /> <!-- 是否去除自動生成的注釋 true :是 : false :否 --> <property name= "suppressallcomments" value= "true" /> </commentgenerator> <!--數(shù)據(jù)庫鏈接url,用戶名、密碼 --> <jdbcconnection driverclass= "com.mysql.jdbc.driver" connectionurl= "jdbc:mysql://localhost/test" userid= "root" password= "123456" > </jdbcconnection> <javatyperesolver> <property name= "forcebigdecimals" value= "false" /> </javatyperesolver> <!-- 生成pojo類的位置--> <javamodelgenerator targetpackage= "com.luis.entity" targetproject= "src/main/java" > <property name= "enablesubpackages" value= "true" /> <property name= "trimstrings" value= "true" /> </javamodelgenerator> <!-- 生成映射文件的包名和位置--> <sqlmapgenerator targetpackage= "mapper" targetproject= "src/main/resources" > <!-- enablesubpackages 是否讓schema作為包的后綴--> <property name= "enablesubpackages" value= "false" /> </sqlmapgenerator> <!-- 生成mapper接口的位置--> <javaclientgenerator type= "xmlmapper" targetpackage= "com.luis.mapper" targetproject= "src/main/java" > <!-- enablesubpackages 是否讓schema作為包的后綴--> <property name= "enablesubpackages" value= "false" /> </javaclientgenerator> <!-- 指定數(shù)據(jù)庫表 --> <table schema= "" tablename= "user" ></table> </context> </generatorconfiguration> |
根據(jù)個人環(huán)境將配置文件中的配置進行更改,如數(shù)據(jù)庫密碼,包名,對應數(shù)據(jù)庫表
所用的數(shù)據(jù)庫表如下:
1
2
3
4
5
6
7
8
9
10
11
|
drop table if exists `user`; create table `user` ( `id` bigint( 20 ) not null , `name` varchar( 255 ) not null , `age` int ( 4 ) not null , primary key (`id`) ) engine=innodb default charset=utf8; insert into `user` values ( '1' , 'wanger' , '22' ); insert into `user` values ( '2' , 'zhangsan' , '18' ); insert into `user` values ( '3' , 'lisi' , '23' ); insert into `user` values ( '4' , 'wangwu' , '21' ); |
配置完成后,右鍵項目,選擇run as——maven build——在下面兩處分別填入:
goals: mybatis-generator:generate -e
profiles: generatorconfig.xml
如下圖所示:
出現(xiàn)如下所示,代碼生成成功,刷新項目即可。
[info] generating example class for table user
[info] generating record class for table user
[info] generating mapper interface for table user
[info] generating sql map for table user
[info] saving file usermapper.xml
[info] saving file userexample.java
[info] saving file user.java
[info] saving file usermapper.java
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
需要注意的是:逆向工程生成的代碼不會覆蓋,因而不能重復多次生成。
此處也有個小坑,逆向工程的代碼生成后,啟動項目會報如下錯誤:
***************************
application failed to start
***************************description:
field usermapper in com.luis.service.impl.userserviceimpl required a bean of type 'com.luis.mapper.usermapper' that could not be found.
action:
consider defining a bean of type 'com.luis.mapper.usermapper' in your configuration.
解決方案:
1、給生成的mapper接口文件前加注解:@mapper 即可解決。但需要給每一個mapper文件前加,繁瑣,因而有第二種類
解決辦法
2、在啟動類前加@mapperscan({"com.luis.mapper"}),其中com.luis.mapper為mapper文件的所在位置。
參考自:http://412887952-qq-com.iteye.com/blog/2392672
分頁應用
逆向工程已經(jīng)生成了entity類,及dao層的mapper接口與*mapper.xml文件,因而/只用編寫service層與web層。
首先在userservice中編寫接口,代碼如下:
1
2
3
4
|
public interface userservice { user selectbyname(string name); list<user> findalluser( int pagenum, int pagesize); } |
在userserviceimpl文件進行實現(xiàn),代碼如下所示:
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
|
@service public class userserviceimpl implements userservice { @autowired private usermapper usermapper; @override public user selectbyname(string name) { userexample example = new userexample(); criteria criteria = example.createcriteria(); criteria.andnameequalto(name); list<user> users = usermapper.selectbyexample(example); if (users != null && users.size() > 0 ) { return users.get( 0 ); } return null ; } /** * pagenum 開始頁數(shù) * pagesize 每頁顯示的數(shù)據(jù)條數(shù) */ @override public list<user> findalluser( int pagenum, int pagesize) { //將參數(shù)傳給方法實現(xiàn)分頁 pagehelper.startpage(pagenum, pagesize); userexample example = new userexample(); list<user> list = usermapper.selectbyexample(example); return list; } } |
最后在controller層對查詢結果進行接收,usercontroller代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@controller @restcontroller public class usercontroller { @autowired private userservice userservice; @requestmapping ( "/test" ) public user queruserbyname() { user user = userservice.selectbyname( "luis" ); system.out.println(user.tostring()); return user; } @requestmapping ( "/list" ) public list<user> queruser() { list<user> list = userservice.findalluser( 1 , 2 ); //獲取分頁信息 pageinfo<user> pageinfo = new pageinfo<>(list); system.out.println( "total:" + pageinfo.gettotal()); system.out.println( "pages:" + pageinfo.getpages()); system.out.println( "pagesize:" + pageinfo.getpagesize()); return list; } } |
測試
此前在項目編寫過程中已經(jīng)對可能出現(xiàn)的錯誤進行了總結,最后,對項目的功能進行測試,通過加@restcontroller注解將數(shù)據(jù)傳輸?shù)綖g覽器中。
測試mybatis與springboot,瀏覽器輸入http://localhost:8080/test,瀏覽器輸出:
{"id":1,"name":"wanger","age":22}
分頁測試,瀏覽器輸入http://localhost:8080/list,瀏覽器輸出:
[{"id":1,"name":"wanger","age":22},{"id":2,"name":"zhangsan","age":18}]
eclipse輸出:
total:4
pages:2
pagesize:2
項目搭建完畢,具體代碼參見github
總結
以上所述是小編給大家介紹的mybatis逆向工程與分頁在springboot中的應用,希望對大家有所幫助,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:https://www.cnblogs.com/liuyi6/archive/2018/09/06/9595856.html