前言
今天學習下springboot集成mybatis,集成mybatis一般有兩種方式,一個是基于注解的一個是基于xml配置的。今天先了解下基于注解的mybatis集成。下面話不多說了,來一起看看詳細的介紹吧
一、引入依賴項
因為是mybatis嘛,肯定是要有mybatis相關的,同時用的是mysql,所以也需要引入mysql相關的。
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- https: //mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version> 1.3 . 2 </version> </dependency> <!-- https: //mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 8.0 . 11 </version> </dependency> |
二、創建model
這里創建了一個user的model,這樣方便與數據庫的表對照,這里在mysql中創建了一個名為mybatis的數據庫,里面創建了一個user的表.同時創建了枚舉類usersexenum.
1
2
3
4
5
6
7
|
create table `user` ( `id` int ( 11 ) not null auto_increment, `name` varchar( 20 ) default null , `age` int ( 11 ) default null , `sex` varchar( 20 ) default null , primary key (`id`) ) engine=innodb auto_increment= 9 default charset=utf8; |
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
|
package com.example.model; import java.io.serializable; public class user implements serializable{ @override public string tostring() { // todo auto-generated method stub return "user [id=" + id + ", name=" + name + ", age=" + age + "]" ; } public int getid() { return id; } public void setid( int id) { id = id; } public string getname() { return name; } public void setname(string name) { name = name; } public int getage() { return age; } public void setage( int age) { age = age; } private int id; private string name; private int age; private usersexenum sex; public usersexenum getsex() { return sex; } public void setsex(usersexenum sex) { sex = sex; } } |
1
2
3
4
5
|
package com.example.model; public enum usersexenum { man, woman } |
三、創建mapper
這里需要把model與操作數據庫的sql對照起來,用什么對照呢?那就需要創建一個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
|
package com.example.mapper; import java.util.list; import org.apache.ibatis.annotations.delete; import org.apache.ibatis.annotations.insert; import org.apache.ibatis.annotations.result; import org.apache.ibatis.annotations.results; import org.apache.ibatis.annotations.select; import org.apache.ibatis.annotations.update; import com.example.model.*;; public interface usermapper { @select ( "select * from user" ) @results ({ @result (property = "sex" , column = "sex" , javatype = usersexenum. class ), @result (property = "name" , column = "name" ) }) list<user> getall(); @select ( "select * from user where id = #{id}" ) @results ({ @result (property = "sex" , column = "sex" , javatype = usersexenum. class ), @result (property = "name" , column = "name" ) }) user getone( int id); @insert ( "insert into user(name,age,sex) values(#{name}, #{age}, #{sex})" ) void insert(user user); @update ( "update user set name=#{username},age=#{age} where id =#{id}" ) void update(user user); @delete ( "delete from user where id =#{id}" ) void delete( int id); } |
四、配置掃描
上面配置了mapper,那怎么讓系統知道mapper放在哪里呢?于是有了@mapperscan注解。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.example.demo; import org.mybatis.spring.annotation.mapperscan; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication @mapperscan ( "com.example.mapper" ) public class demoapplication { public static void main(string[] args) { springapplication.run(demoapplication. class , args); } } |
五、創建controller
這里創建了usercontroller,一個是顯示所有用戶,一個是新增一個用戶之后再顯示所有用戶。
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
|
package com.example.demo; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import com.example.mapper.usermapper; import com.example.model.user; import com.example.model.usersexenum; @controller @requestmapping ( "/user" ) public class usercontroller { @autowired private usermapper usermapper; @requestmapping (value = "/alluser.do" ,method = requestmethod.get) public string getallusers(model model) { list<user> users=usermapper.getall(); model.addattribute( "users" , users); return "userlist" ; } @requestmapping (value = "/insert.do" ,method = requestmethod.get) public string adduser(model model) { user user= new user(); user.setname( "cuiyw" ); user.setage( 27 ); user.setsex(usersexenum.man); usermapper.insert(user); list<user> users=usermapper.getall(); model.addattribute( "users" , users); return "userlist" ; } } |
六、數據庫配置
上面mapper也設置了,model也設置了,那要與數據庫交互,肯定要配置數據庫地址這些信息吧。這里在運行的時候還報了一個錯誤.nested exception is java.sql.sqlexception: the server time zone value 'öð¹ú±ê׼걼ä' is unrecognized or represents more than one time zone. you must configure either the server or jdbc driver (via the servertimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.在mysql中設置了下時區:set global time_zone='+8:00';
1
2
3
4
5
6
7
8
9
|
spring.mvc.view.prefix=/view/ spring.mvc.view.suffix=.jsp mybatis.type-aliases- package =com.example.model spring.datasource.driverclassname = com.mysql.cj.jdbc.driver spring.datasource.url = jdbc:mysql: //localhost:3306/mybatis spring.datasource.username = root spring.datasource.password = 123456 |
七、創建頁面顯示
這里還是按照上一博客用jsp顯示數據。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > <title>insert title here</title> </head> <body> <table> <tr><th>名字</th><th>年齡</th><th>性別</th></tr> <c:foreach items= "${users}" var= "item" > <tr><td>${item.name}</td><td>${item.age}</td><td>${item.sex}</td></tr> </c:foreach> </table> </body> </html> |
八、測試
這里先在瀏覽器打開http://localhost:8080/user/alluser.do,可以看到用戶列表,然后輸入http://localhost:8080/user/insert.do,就會看到列表顯示多了一行數據。
九、小結
使用基于注解的集成mybatis比較省事方便,但有利有弊,對于多表相連的可能就不太方便,使用基于xml配置的可能就更會好些。
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/5ishare/p/9292201.html