the weekend bella复合:mybatis、ibatis、spring各种整合方式
来源:百度文库 编辑:偶看新闻 时间:2024/10/02 17:08:09
1,只是用mybatis3。
2,使用mybatis3+spring3(使用mybatis的SqlSessionFactory )。
3,使用ibatis2.3+spring(使用spring自带的ibatis)
spring的orm包中只有ibatis,没有mybatis。而mybatis和ibatis还是有些区别的,比如配置文件属性不同。
第一种方式(只使用mybatis):
1)jar包:
cglib-2.2.jar
asm-3.1.jar
mysql-connector-java-3.1.13.jar
mybatis-3.0.5.jar
junit.jar
2)mybatis配置文件:
其中
加载事务配置
注:关于JNDI的配置,见 tomcat的几种JNDI配置方法
3)mybatis的sql映射配置文件:
4)测试方法:
public void testSelect(){ try { Reader reader = Resources.getResourceAsReader("mybatis-config-mappings.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader,"development1"); SqlSession session = sqlSessionFactory.openSession(); PageAccessURL pageAccessURL =(PageAccessURL)session.selectOne("selectPageAccessURL2", 70001);// // PageAccessURL page = new PageAccessURL();// page.setUrlId("70001");// page.setUrl("warrantAndCbbcInfo.jsp");// PageAccessURL pageAccessURL =(PageAccessURL)session.selectOne("selectPageAccessURLByClass",page); session.close(); reader.close(); System.out.println(pageAccessURL.getUrl()); } catch (IOException e) { System.out.println(e); } }SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader,"development1"); 后面的development1是前面讲的数据源配置方式之一。如果要测试jndi的,则要通过容器加载后进行。
第二种方式(mybatis3.0+spring3.0,spring自带的orm中,只有ibatis的,没有mybatis,所以使用mybatis3和spring整合的话只能用SqlSessionFactory 了);
1)jar包:
mybatis-3.0.5.jar
mysql-connector-java-3.1.13.jar
cglib-2.2.jar
asm-3.1.jar
aopalliance-1.0.jar
commons-logging-1.1.1.jar
hsqldb-1.8.0.10.jar
jstl-1.2.jar
log4j-1.2.16.jar
mybatis-spring-1.0.1.jar
spring-aop-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
spring-tx-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
stripes-1.5.6.jar
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
junit.jar
2)spring配置文件:
applicationContext.xml
applicationContext.xml
关于spring的其他数据源配置,这里就不写了。
4)mybatis的配置文件:
使用了spring管理的话,这里就不用设置数据源等其他配置了
5)mybatis的sql映射文件配置:
同方式一配置的sql映射文件配置
6)配置DAO层:
public class PageAccessURLManager { private SqlSessionFactory sqlSessionFactory ; public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } public PageAccessURL getPageAccessURL(int url_id){ PageAccessURL page = (PageAccessURL)sqlSessionFactory.openSession().selectOne("selectPageAccessURL",url_id); System.out.println(page.getUrl()); return page; }}
7)测试:
第三种方式(ibatis2.3+spring3):
1)jar包:
mysql-connector-java-3.1.13.jar
log4j-1.2.16.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
commons-logging-1.1.1.jar
spring-asm-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
spring-tx-3.0.5.RELEASE.jar
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
ibatis-2.3.0.677.jar
junit.jar
2)spring配置文件:
applicationContext.xml
applicationContext-dao.xml
3)ibatis配置文件:
4)ibatis的sql映射配置文件:
5)配置DAO层:
注意:请仔细对比mybatis和ibatis的配置区别。
6)测试:
同方式二的测试;