前言
今天学习下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