首先,我将项目分解为了五个子模块,分别为core、model、mapper、service和web模块。其中mapper模块主要负责配置mybatis相关的mapper文件,web模块中配置controller相关的类,主配置文件application.properties放在了web模块下。mybatis-generator的配置文件配置在主模块下。项目结构如图所示:
首先,我们先将mybatis-generator配置完成,直接在主模块的build.gradle文件中加入以下代码:
dependencies {
//mybatis-generator插件依赖
compile 'org.mybatis.generator:mybatis-generator-core:1.3.2'
}
//mybatis generator plugin ------ start
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
}
}
apply plugin: "com.arenagod.gradle.MybatisGenerator"
configurations {
mybatisGenerator
}
mybatisGenerator {
verbose = true
configFile = 'src/main/resources/mybatis/generatorConfig.xml'
}
//mybatis generator plugin ------ end
然后,在主模块中编写generatorConfig.xml,代码如下:
至此,mybatis-generator就配置完成了。
如果只是配置了以上的配置,那么生成的mapper文件将不能注入成功。会报org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
这样的异常。此时表明自动注入并不能找到对应的Mapper配置文件。
此时需要在application.properties文件中配置Mapper配置文件的路径和datasource,但我们的Mapper.xml文件是在mapper模块下面的,而application.properties文件是在web模块之下的,然而,在application.properties文件中好像不能直接引用其他模块下的配置文件。我使用了spring.profiles.active属性来加载其他模块下的application-*.properties文件。其中,application.properties中的内容如下:
spring.profiles.active=mybatis
然后在mapper模块下新建一个application-mybatis.properties配置文件,内容如下:
#加载Mapper配置文件
mybatis.mapper-locations = mybatis/mappers/*Mapper.xml
#数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/garbage?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
这样Mapper就能自动注入成功了。
这种方法我是采用配置类的方式将配置信息注入到ioc容器中。在core模块中新增配置类文件,文件目录如下图所示:
首先将dataSource数据源bean配置在DataSourceConfiguration中,代码如下:package com.sqc.dustbin.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;
/**
* 配置DataSource到ioc容器中
*/
@Configuration
//配置mybatis mapper的扫描路径
@MapperScan("com.sqc.mapper")
@Component
@ConfigurationProperties(prefix = "jdbc")
public class DataSourceConfiguration {
private String driver;
private String url;
private String username;
private String password;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 配置与spring-dao对应的bean DataSource
*/
@Bean(name = "dataSource")
public DriverManagerDataSource createDataSource(){
//生成DataSource实例
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//配置DataSource
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
然后将SqlSessionFactory工厂bean配置在SqlSessionFactoryConfiguration中,代码如下:
package com.sqc.dustbin.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
public class SqlSessionFactoryConfiguration {
//mapper.xml路径
@Value("${mapper_path}")
private String mapperPath;
//实体类所在的路径
@Value("${type_alias_package}")
private String typeAliasesPackage;
@Autowired
private DataSource dataSource;
/**
* 创建SqlSessionFactoryBean实例
* 设置mapper映射路径和datasource数据源
*/
@Bean(name = "SqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//设置mapper.xml扫描路径
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
//配置所有类路径下的目录,其他模块也能被扫描到
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX+mapperPath;
sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
//设置typeAlias包扫描路径
sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
//设置DataSource
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
}
这样也可以实现Mapper的自动注入。