方式二:
pom文件导入maven依赖:
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.43.212:3306/houses?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
DruidConfig配置类(上一次采用的@ConfigurationProperties注解直接注入,这次采用@PropertySource注解):
@Configuration
@PropertySource(value = {"classpath:jdbc.properties"})//也可以使用@ConfigurationProperties直接注入属性
public class DataSourceConfig {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
/**
* 配置数据库
* @return
*/
@Bean(name = "dataSource")
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
// 数据库驱动
datasource.setDriverClassName(jdbcDriverClassName);
// 相应驱动的jdbcUrl
datasource.setUrl(jdbcUrl);
// 数据库的用户名
datasource.setUsername(jdbcUsername);
// 数据库的密码
datasource.setPassword(jdbcPassword);
// 每个分区最大的连接数
datasource.setMaxActive(20);
// 每个分区最小的连接数
datasource.setMinIdle(5);
return datasource;
}
}
配置mapperscanner配置类:
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.sun.houses.mapper");
return mapperScannerConfigurer;
}
}
MyBatis核心配置类(主要配置sqlsessionFactory):
@Configuration
public class MyBatisConfig {
@Autowired
private DataSource dataSource;
@Bean(name="sqlSessionFactory")
@ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象
public SqlSessionFactory sqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
try {
// 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
// 设置mybatis的主配置文件
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
//设置mybatis扫描的mapper.xml文件的路径(非常重要,否则找不到mapper.xml文件)
Resource[] mapperResources = resolver.getResources("classpath:mybatis/mapper/*.xml");
sqlSessionFactoryBean.setMapperLocations(mapperResources);
// 设置别名包,便于在mapper.xml文件中ParemeType和resultType不要写完整的包
sqlSessionFactoryBean.setTypeAliasesPackage("com.sun.houses.model");
return sqlSessionFactoryBean.getObject();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException();
}
}
}
mapper-config.xml文件放在resources下的mybatis下(配置mybatis的其他信息等):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="cacheEnabled" value="true" /><!-- 全局映射器启用缓存 -->
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
</configuration>