1、Junit测试错误截图:
2、Junit测试书写代码:无问题
public class DBInfoTest extends BaseTest {
@Resource
private DBInfoService dbInfoService;
@Test
public void addDBInfo() {
DBInfo dbInfo = new DBInfo();
dbInfo.setDbName("mysql");
dbInfo.setDbUser("root");
dbInfo.setDbPwd("iesapp");
System.err.println(dbInfoService.addDBInfo(dbInfo));
}
}
3、Service
public interface DBInfoService {
int addDBInfo(DBInfo dbInfo);
}
4、Service实现
@Transactional(value = "baseDataSourceTransactionManager",
propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,
timeout=36000,
rollbackFor=Exception.class)
@Service("dbInfoService")
public class DBInfoServiceImpl implements DBInfoService {
@Autowired
private DBInfoMapper dbInfoDao;
@Override
public int addDBInfo(DBInfo dbInfo) {
dbInfoDao.addDBInfo(dbInfo);
return 1;
}
}
5、Dao 也就是MyBatis Mapper 映射
@Mapper
public interface DBInfoMapper {
void addDBInfo(DBInfo dbInfo);
}
6、Application.yml Spirngboot属性配置
spring:
datasource: #数据源
base:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.01:3306/xx?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123
initialSize: 50
maxActive: 200
maxIdle: 10
maxWait: 80
validationQuery: SELECT 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 7200000
removeAbandoned: true
removeAbandonedTimeout: 1800
defaultAutoCommit: true
logAbandoned: true
mybatis:
mapper-locations: classpath:mapper/**/*Mapper.xml
7、Mybatis Mapper映射配置文件
insert into dbinfo ( `dbName`, `dbUser`, `dbPwd`, `dbClass`, `dbType` ) values ( #{dbName}, #{dbUser}, #{dbPwd}, #{dbClass}, #{dbType} )
8、数据库连接配置类
@Configuration
@MapperScan(basePackages="cn.aa.bb.mapper", sqlSessionFactoryRef="baseSqlSessionFactory")
public class DataXDataSource {
@Bean("baseDataSource")
@ConfigurationProperties(prefix="spring.datasource.base")
@Primary
public DataSource baseDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("baseSqlSessionFactory")
@Primary
public SqlSessionFactory baseSqlSessionFactory(@Qualifier("baseDataSource") DataSource baseDataSource) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(baseDataSource);
try {
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*Mapper.xml"));
} catch (IOException e1) {
e1.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = null;
try {
sqlSessionFactory = bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
@Bean("baseDataSourceTransactionManager")
@Primary
public DataSourceTransactionManager baseDataSourceTransactionManager(@Qualifier("baseDataSource") DataSource baseDataSource) {
return new DataSourceTransactionManager(baseDataSource);
}
@Bean("baseSqlSessionTemplate")
@Primary
public SqlSessionTemplate baseSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory baseSqlSessionFactory) {
return new SqlSessionTemplate(baseSqlSessionFactory);
}
}
9、写到这里似乎没有人看到有问题
在网上也查找了很多的资料,都说命名空间不对、或者配置的参数类型不对
但是都不是。
10、上面是测试成功的,相信也都看到了,有一句话是
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*Mapper.xml"));
,绿色是我漏掉的,加上这句,然后这句话的路径不能有错,这个坑就在这里记录吧!
微信公众号:ucissoftcom
博客:http://andot.org
Github:https://github.com/andotorg