全文来自于https://itbaima.net/document
注册mybatis
最老版本
- config的配置
@Configuration
@ComponentScan("org.example.entity")
public class MainConfiguration {
//注册SqlSessionTemplate的Bean
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws IOException {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
return new SqlSessionTemplate(factory);
}
}
- 测试
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
SqlSessionTemplate template = context.getBean(SqlSessionTemplate.class);
TestMapper testMapper = template.getMapper(TestMapper.class);
System.out.println(testMapper.getStudent());
}
直接扫描
- mapper直接被当作bean扫描进来了
@Configuration
@ComponentScan("org.example.entity")
@MapperScan("org.example.mapper")
public class MainConfiguration {
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws IOException {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
return new SqlSessionTemplate(factory);
}
}
- 测试
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
TestMapper mapper = context.getBean(TestMapper.class);
System.out.println(mapper.getStudent());
}
数据库的替换(这里是用的HikariCP,德鲁伊一个原理)
@Configuration
@ComponentScan("org.example.entity")
@MapperScan("org.example.mapper")
public class MainConfiguration {
// @Bean //这个是mybatis的自带的 我们可以换成其他的
// public DataSource dataSource(){
// return new PooledDataSource("com.mysql.cj.jdbc.Driver",
// "jdbc:mysql://localhost:3306/study", "root", "123456");
// }
@Bean
public DataSource dataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/study");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ //直接参数得到Bean对象
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean;
}
}
事务的配置
- 事务的配置
- 我们需要在配置类上加上
@EnableTransactionManagement
注解 - 然后在配置类中注册一个
TransactionManager
的Bean 事务的传播
有很多配置 目前用默认的即可
- 我们需要在配置类上加上
@Configuration
@ComponentScan("org.example")
@MapperScan("org.example.mapper")
@EnableTransactionManagement
public class MainConfiguration {
@Bean
public TransactionManager transactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
- 应用
- 提一嘴为什么可以
@Resource
到mapper - 因为mapper也被当bean在config被@MapperScan 扫描了
- 提一嘴为什么可以
@Component
public class TestServiceImpl implements TestService {
@Resource
TestMapper mapper;
@Transactional //此注解表示事务,之后执行的所有方法都会在同一个事务中执行
public void test() {
mapper.getUser();
// if(true) throw new RuntimeException("我是测试异常!");
mapper.getUser();
}
}
public interface TestMapper {
@Select("select * from users where id = 1")
Users getUser();
@Select("select * from users where id = 2")
Users getUser2();
}
- 测试
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
TestService service = context.getBean(TestService.class);
service.test();
}
spring 集成的测试
- pom中导入对应的包以后可以直接在test文件夹下这样进行测试
@Test
下面那个直接就能运行
- 使用SpringTest模块提供的
@ContextConfiguration
注解来表示要加载哪一个配置文件,可以是XML文件也可以是类 @ExtendWith
是由JUnit提供的注解
@ExtendWith(SpringExtension.class) //写死的
@ContextConfiguration(classes = TestConfiguration.class)// 指定你要跑那个配置文件
public class TestMain {
@Autowired
TestService service;
@Test
public void test(){
service.test();
}
}
需要的pom
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.10</version>
</dependency>
<!-- 这两个依赖不用我说了吧 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<!-- 注意,对于Spring 6.0来说,版本需要在3.5以上 -->
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
<!-- Mybatis针对于Spring专门编写的支持框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Spring的JDBC支持框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>