之前就写过springboot集成mybatis的案例,但是是通过注解来写sql语句的,也比较简单,所以在想怎么把最普遍的xml来集成到springboot项目中
翻阅了许多资料,这里我写一种最简便的方式。使用此方式,基本没有什么需要配置
1、在pom.xml中集成所需要的一些依赖
<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>1.4.2.RELEASE</version>
</parent>
<groupId>com.springboot</groupId>
<artifactId>spring_mybatis</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<mybatis.version>3.3.0</mybatis.version>
<mybatis-spring.version>1.2.3</mybatis-spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
对mybatis进行相关的配置
mybatis.type-aliases-package=com.boom.spring_mybatis.entity
mybatis.mapper-locations=classpath:com/boom/spring_mybatis/mapper/**.xml
mybatis.base-package=com.boom.spring_mybatis.mapper
数据源配置(根据自己数据库进行配置)
spring.datasource.url=jdbc:mysql://localhost:3306/boomOA
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.validation-query=SELECT 1
3 实体类的编写
public class Animal implements Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Mapper
public interface AnimalMapper {
int save(Animal animal);
}
5 repository的编写
@Repository
public class AnimalRepository {
@Autowired
private AnimalMapper animalMapper;
public int save(Animal animal){
animalMapper.save(animal);
return animal.getId();
}
}
6 service的编写
@Service
public class AnimalService {
@Autowired
private AnimalRepository animalRepository;
public int save(Animal animal){
return animalRepository.save(animal);
}
}
7 controller的编写
@Controller
@RequestMapping("/animal")
public class AnimalController {
@Autowired
private AnimalService animalService;
@RequestMapping("/save")
@ResponseBody
public String save(String name){
Animal animal = new Animal();
animal.setName(name);
int id = animalService.save(animal);
return String.valueOf(id);
}
}
8 在 resources目录下,建立com/boom/spring_mybatis/mapper,然后在此目录下建立 AnimalMapper.xml 在里面编写sql
<?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.boom.spring_mybatis.mapper.AnimalMapper">
<insert id="save" parameterType="com.boom.spring_mybatis.entity.Animal">
INSERT INTO boom_animal(
name
) VALUES(
#{name}
)
</insert>
</mapper>
此时运行项目 http://localhost:2016/animal/save?name=zzz
数据库便会插入一条信息了。