Spring Boot集成Mybatis的实例代码(简洁版)

时间:2022-01-06 15:03:51

概述

现在互联网应用中,大部分还是使用mybatis来操作数据库的,本文介绍一下spring boot中如何集成mybatis。

上篇介绍了spring boot 直接用jar运行项目的方法,需要的朋友点击查看。

创建spring boot工程

spring boot 开篇-创建和运行 一文中有一个小节介绍了如何使用spring boot的组件来创建工程。如果要集成mybatis,只需要把mysql和mybatis这两个组件勾选一下即可。

Spring Boot集成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