纯注解Spring+Mybatis编程
01 基础配置02 Mapper03 Mapper.xml04 使用05 MapperLocations 编码时通配的写法06 耦合问题
6.1 创建配置文件6.2 新建一个类6.3 配置类 纯注解Spring+Mybatis编程 01 基础配置
连接池SqlSessionFactoryBeanMapperScannerConfigure
@Configuration@ComponentScan("com.frame.mybatis")@MapperScan(basePackages = ("com.frame.mybatis"))public class AppConfig { @Bean public DruidDataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/chat?serverTimezone=UTC"); dataSource.setUsername("root"); dataSource.setPassword("123456"); return dataSource; } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(){ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); sqlSessionFactoryBean.setTypeAliasesPackage("com.frame.mybatis"); sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserMapper.xml")); return sqlSessionFactoryBean; }}
02 Mapperpublic interface UserMapper { public List
<?xml version="1.0" encoding="UTF-8" ?>
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);UserMapper userMapper = (UserMapper) ac.getBean("userMapper");List
sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserMapper.xml"));// 参数实际上就是Resource的可变长参数 public void setMapperLocations(Resource..、mapperLocations) { this.mapperLocations = mapperLocations; }// 通配的写法ResourcePatternResolver pattern = new PathMatchingResourcePatternResolver();Resource[] resources = pattern.getResources("com.frame.mybatis/*Mapper.xml");sqlSessionFactoryBean.setMapperLocations(resources);
06 耦合问题 6.1 创建配置文件mybatis.driverClassName=com.mysql.cj.jdbc.Drivermybatis.url=jdbc:mysql://localhost:3306/chat?serverTimezone=UTCmybatis.username=rootmybatis.password=123456mybatis.mapperLocations=com.frame.mybatis/*Mapper.xmlmybatis.typeAliasesPackages=com.frame.mybatis
6.2 新建一个类package com.frame.mybatis;import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@PropertySource("mybatis.properties")@Datapublic class MybatisProperties { @Value("${mybatis.driverClassName}") private String driverClassName; @Value("${mybatis.url}") private String url; @Value("${mybatis.username}") private String username; @Value("${mybatis.password}") private String password; @Value("${mybatis.typeAliasesPackages}") private String typeAliasesPackages; @Value("${mybatis.mapperLocations}") private String mapperLocations;}
6.3 配置类@Configuration@ComponentScan("com.frame.mybatis")@MapperScan(basePackages = ("com.frame.mybatis"))@PropertySource("mybatis.properties")public class AppConfig { @Autowired private MybatisProperties mybatisProperties; @Bean public DruidDataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(mybatisProperties.getDriverClassName()); dataSource.setUrl(mybatisProperties.getUrl()); dataSource.setUsername(mybatisProperties.getUsername()); dataSource.setPassword(mybatisProperties.getPassword()); return dataSource; } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); sqlSessionFactoryBean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackages()); ResourcePatternResolver pattern = new PathMatchingResourcePatternResolver(); Resource[] resources = pattern.getResources(mybatisProperties.getMapperLocations()); sqlSessionFactoryBean.setMapperLocations(resources); return sqlSessionFactoryBean; }}