发表于: 2020-06-23 23:00:13
1 1539
今天做了什么
1.在服务器上用maven命令行跑单元测试。
2.测试DB中断后能捕获异常。
3.在spring和mybatis项目中使用注解配置数据库
单元测试
命令:mvn test -Dtest=测试类名称#要测试的方法 -DfailIfNoTests=false
测试trycatch
注解配置数据库
@Configuration //表示配置文件类
@ComponentScan("service") //扫描的包,代替<context:component-scan base-package="service"/>
@PropertySource(value = {"classpath:db.properties"}, ignoreResourceNotFound = true) //添加配置文件
//开启自动扫描mapper包
@MapperScan(basePackages = {"dao"}, sqlSessionFactoryRef = "sqlSessionFactory") //需要注入数据源的包路径
public class AnnotationToDataSource {
@Value("${driver}")
private String driverClass;
@Value("${url}")
private String jdbcUrl;
@Value("${user}")
private String user;
@Value("${password}")
private String password;
@Resource
private DataSource dataSource;
@Bean
public DataSource dataSource() throws PropertyVetoException {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("driverClass");
druidDataSource.setUrl("jdbcUrl");
druidDataSource.setUsername("user");
druidDataSource.setPassword("password");
return druidDataSource;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new org.mybatis.spring.SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
//获取路径的工具类
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactoryBean.setTypeAliasesPackage("pojo");
//获得扫描的路径
sessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:Mapper/*Mapper.xml"));
return sessionFactoryBean;
}
}
明天的计划:
1.深入探索一下spring的注解。
2.做任务一的深度思考。
3.检查一下自己的代码是否符合规范。
遇到的问题:使用注解是配置一个类出来了,但是还不知道怎么使用。
收获:学会了用注解配置一个配置文件类
评论