很久没写写东西了,去年底开始在做微服务改造,玩spring boot还有spring cloud。总结一下,免得脑子不好使,忘记事情。
springboot和spring jpa有很好的天然集成,但是如果要用mybatis的话,还是需要自己做一点点配置。刚开始弄springboot和mybatis集成的时候,一顿搜索,发现网上好多人写的都很复杂,属于纯手工集成的方式,自己配置datasource,自己配置扫描路径,自己配置transactionManager。
其实mybatis官方已经帮我们写好了MyBatis-Spring-Boot-Starter,我们只有把它声明到pom.xml即可。而且官方也有相关的demo。但是web工程集成mybatis的时候出现的问题,不知道是不是共性问题,就是必须自己用@Bean注解配置一个DataSource实例,不然springboot就是集成不了mybatis。
具体操作步骤如下:
新建一个mave工程springboot_mybatis
- 最后的工程如下:
配置pom.xml
- pom.xml配置如下:
<?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.lee</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_mybatis</name>
<description>Spring Boot Setup Demo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<mybatis-spring.version>1.2.3</mybatis-spring.version>
<mybatis.version>3.3.0</mybatis.version>
<h2.version>1.4.190</h2.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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.27</version>
</dependency>
<!-- h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Mapper
- 在
application.properties
声明mybatis相关的属性
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.jdbc-url=jdbc:h2:tcp://localhost/~/springbootmybatis
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.schema=classpath:import.sql
# 声明mapper所在路径
mybatis.mapper-locations=classpath:mapper/*.xml
这里用的是h2数据库,如果使用其他数据库,只要在pom添加相关的依赖和配置数据库的相关属性即可。
-
*Mapper.xml
的配置和普通的配置没有区别 -
CityMapper.java
接口
package com.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.domain.City;
/**
* Created by liyc02 on 2017/5/12.
*/
@Mapper
public interface CityMapper {
City selectCityById(int id);
}
特别注意的是使用的
@Mapper
注解而不是之前的@Repository
注解。
数据源配置
- 开始以为在
application.properties
中声明了spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
就可以自动配置了,结果不行,需要自己手动配置DataSourceConfig.java
package com.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DataSourceConfig {
@Autowired
private Environment env;
@Bean
public DataSource getDataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
ds.setUrl(env.getProperty("spring.datasource.jdbc-url"));
ds.setUsername(env.getProperty("spring.datasource.username"));
ds.setPassword(env.getProperty("spring.datasource.password"));
return ds;
}
}
以上,运行App.java
即可,访问浏览器,可以得到结果。