纯注解Spring+Mybatis编程
01 基础配置
- 连接池
- SqlSessionFactoryBean
- MapperScannerConfigure
@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 Mapper
public interface UserMapper {
public List<User> getAllUser();
}
03 Mapper.xml
<?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.frame.mybatis.UserMapper">
<select id="getAllUser" resultType="user">
select * from user
</select>
</mapper>
04 使用
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
List<User> allUser = userMapper.getAllUser();
05 MapperLocations 编码时通配的写法
sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserMapper.xml"));
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.Driver
mybatis.url=jdbc:mysql://localhost:3306/chat?serverTimezone=UTC
mybatis.username=root
mybatis.password=123456
mybatis.mapperLocations=com.frame.mybatis/*Mapper.xml
mybatis.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")
@Data
public 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;
}
}