Spring Boot 整合Mybatis(基于注解)
发表于|更新于
|阅读量:
基于注解的Spring Boot 整合Mybatis
<Excerpt in index | 首页摘要>
<The rest of contents | 余下全文>
项目结构

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
| <?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> </parent> <groupId>com.caicai</groupId> <artifactId>spring-boot-mybatis-annotation</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-mybatis-annotation</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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>
|
UserMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.caicai.springbootmybatisannotation.mapper;
import com.caicai.springbootmybatisannotation.pojo.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper;
@Mapper public interface UserMapper { @Insert("insert into users(name,age) values(#{name},#{age})") public void addUser(User user);
}
|
User.java
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
| package com.caicai.springbootmybatisannotation.pojo;
public class User { private Integer id; private String name; private Integer age;
public User() { }
public User(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; } }
|
application.yml
1 2 3 4 5 6
| spring: datasource: password: 123456 username: root driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm
|
SpringBootMybatisAnnotationApplicationTests.java
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.caicai.springbootmybatisannotation;
import com.caicai.springbootmybatisannotation.mapper.UserMapper; import com.caicai.springbootmybatisannotation.pojo.User; 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;
@RunWith(SpringRunner.class) @SpringBootTest public class SpringBootMybatisAnnotationApplicationTests { @Autowired UserMapper userMapper; @Test public void addUser(){ User user = new User(); user.setAge(10); user.setName("caicai"); userMapper.addUser(user); }
}
|
对比xml文件配置
1 2
| @Insert("insert into users(name,age) values(#{name},#{age})") public void addUser(User user);
|
@Insert注解的配置就相当于在UserMapper.xml中配置: