Spring整合Mybatis
Mybatis程序核心对象分析
//1.创建sqlsessionfactorybuilder对象
SqlSessionFactoryBuildersqlSessionFactoryBuilder=newSqlSessionFactoryBuilder();
//2.加载sqlmapconfig.xml配置文件
InputStreaminputStream=Resources.getResourceAsStream("SeqMapConfig.xml");
//3.创建SqlSessionFactory对象
SqlSessionFactorysqlSessionFactory=sqlSessionFactoryBuilder.build(inputStream);
//4.获取SqlSession
Sqlsessionsqlsession=sqlSessionFactory.openSession();
//5.执行SqlSession对象执行查询,获取结果User
AccountDaoaccountDao=sqlsession.getMapper(AccountDao.class);
Accountac=accountDao.findById(2);
System.out.println(ac);
//6.释放资源
sqlsession.close();
<configuration>
<propertiesresource="jdbc.properties"></properties>
<typeAliases>
<packagename = "com.wangxh.domin"/>
</typeAliases>
<environmentsdefault="mysql">
<environmentid="mysql">
<transactionManagertupe="JDBC">
<dataSourcetype="POOLED">
<propertiesname="driver"value="${jdbc.driver}"></properties>
<propertiesname="url"value="${jdbc.url}"></properties>
<propertiesname="name"value="${jdbc.name}"></properties>
<propertiesname="password"value="${jdbc.password}"></properties>
</dataSource>
</transactionManager>
</environment>
</environments>
</configuration>
<mappers>
<packagename="com.wangxh.dao"></package>
</mappers>
整合junit
- 使用spring整合junit专用的类加载器
@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration(classes=SpirngConfig.class)
publicclassBookServiceTest{
@Autowired
privateBookServicebookService;
@Test
publicvoidtestSave(){
bookService.save();
}
}