概述
現在互聯網應用中,大部分還是使用mybatis來操作數據庫的,本文介紹一下spring boot中如何集成mybatis。
上篇介紹了spring boot 直接用jar運行項目的方法,需要的朋友點擊查看。
創建spring boot工程
在 spring boot 開篇-創建和運行 一文中有一個小節介紹了如何使用spring boot的組件來創建工程。如果要集成mybatis,只需要把mysql和mybatis這兩個組件勾選一下即可。
當然也可以不通過這種方式,直接在pom.xml文件中添加依賴也是可以的。我選擇的是直接在pom.xml文件中直接添加依賴這種方式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version> 1.3 . 1 </version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 34 </version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version> 1.1 . 7 </version> </dependency> |
數據源使用阿里的druid。完整的pom.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
|
<?xml version= "1.0" encoding= "utf-8" ?> <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> <groupid>com.springboot</groupid> <artifactid>study</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>study</name> <description>demo project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 10 .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.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version> 1.3 . 1 </version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 34 </version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version> 1.1 . 7 </version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version> 1.2 . 45 </version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
創建table
1
2
3
4
5
|
create table `user` ( `id` bigint( 20 ) not null auto_increment, `name` varchar( 30 ) not null default '' , primary key (`id`) ) engine=innodb auto_increment= 8 default charset=utf8 comment= 'user信息' ; |
創建entity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.springboot.entity; public class user { private long id; private string name; public long getid() { return id; } public void setid( long id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } @override public string tostring() { return "user{" + "id=" + id + ", name='" + name + '\ '' + '}' ; } } |
創建mybatis映射文件和repo
userrepo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.springboot.repo; import com.springboot.entity.user; import org.apache.ibatis.annotations.mapper; import org.springframework.stereotype.component; import java.util.list; @component @mapper public interface userrepo { int insert(user user); user selectbyprimarykey( long id); int updatebyprimarykey(user user); int deletebyprimarykey( long id); list<user> selectall(); } |
usermapper.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
|
<?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= "com.springboot.repo.userrepo" > <resultmap id= "baseresultmap" type= "com.springboot.entity.user" > <id column= "id" property= "id" jdbctype= "bigint" /> <result column= "name" property= "name" jdbctype= "varchar" /> </resultmap> <sql id= "base_column_list" > id, name </sql> <select id= "selectbyprimarykey" resultmap= "baseresultmap" parametertype= "java.lang.long" > select <include refid= "base_column_list" /> from user where id = #{id,jdbctype=bigint} </select> <select id= "selectall" resultmap= "baseresultmap" > select <include refid= "base_column_list" /> from user </select> <update id= "updatebyprimarykey" parametertype= "com.springboot.entity.user" > update user <set> < if test= "name != null" > `name`= #{name,jdbctype=varchar}, </ if > </set> where id = #{id,jdbctype=bigint} </update> <delete id= "deletebyprimarykey" parametertype= "java.lang.long" > delete from user where id = #{id,jdbctype=bigint} </delete> <insert id= "insert" parametertype= "com.springboot.entity.user" usegeneratedkeys= "true" keyproperty= "id" > insert into user <trim prefix= "(" suffix= ")" suffixoverrides= "," > name </trim> <trim prefix= "values (" suffix= ")" suffixoverrides= "," > #{name,jdbctype=varchar} </trim> </insert> </mapper> |
編寫application.properties
在spring boot為我們生成的application.properties文件中添加如下內容:
1
|
spring.datasource.name=spring_boot_study spring.datasource.url=jdbc:mysql: //localhost:3306/test spring.datasource.username=root spring.datasource.password=xxxxxx spring.datasource.driver-class-name=com.mysql.jdbc.driver spring.datasource.type=com.alibaba.druid.pool.druiddatasource mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.springboot.entity |
單元測試
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
|
package com.springboot; import com.springboot.entity.user; import com.springboot.repo.userrepo; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springrunner; import java.util.list; @runwith (springrunner. class ) @springboottest public class usertest { @autowired private userrepo userrepo; @test public void testinsert() { user user = new user(); user.setname( "test2" ); userrepo.insert(user); } @test public void testupdate() { user user = new user(); user.setid(6l); user.setname( "test3" ); userrepo.updatebyprimarykey(user); } @test public void testdelete() { userrepo.deletebyprimarykey(6l); } @test public void testselectbyprimarykey() { user user = userrepo.selectbyprimarykey(7l); system.out.println(user); } @test public void testselectall() { list<user> userlist = userrepo.selectall(); system.out.println(userlist); } } |
總結
以上所述是小編給大家介紹的spring boot集成mybatis的實例代碼(簡潔版),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/linsongbin1/article/details/79260946