软文街源码:mybatis、ibatis、spring各种整合方式

来源:百度文库 编辑:中财网 时间:2024/04/29 05:17:33
mybatis是ibatis的升级版,spring也有自带mybatis的orm。所以,搭建ibatis的框架也会有多种方式(我这里mybatis是3.0的,ibatis是2.3的,spring是3.0的,数据库是mysql)。下面介绍3中方式

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配置文件:

 

其中属性是数据源环境配置,可以配置多个数据源配置。每个属性代表一种配置方式。

加载事务配置 和数据源配置

  有两种配置方式,分别是JDBC和MANAGED,详细说明见配置注释。

有三种配置方式,分别是UNPOOLED、POOLED、JNDI,详细说明见配置注释。

定义别名,使用指定的别名来定义。

注:关于JNDI的配置,见 tomcat的几种JNDI配置方法

3)mybatis的sql映射配置文件:

URL_ID as urlId,url,moduleId,state,mark SELECT FLOOR(1 + (RAND() * 1000000)); insert into table values(xx,xx);  

 

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)测试:

public void testSelect() { ApplicationContext tx = new ClassPathXmlApplicationContext("applicationContext.xml"); PageAccessURLManager page = (PageAccessURLManager)tx.getBean("pageAccessURLManager"); page.getPageAccessURL(123456); }


 

第三种方式(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映射配置文件:

URL_ID as urlId,url,moduleId,state,mark SELECT FLOOR(1 + (RAND() * 1000000)); insert into table values(xx,xx);


5)配置DAO层:

public class PageAccessURLManager { private SqlMapClient sqlMapClient ; public void setSqlMapClient(SqlMapClient sqlMapClient) { this.sqlMapClient = sqlMapClient; } public void getPageAccessURL(int urlId) throws SQLException{ PageAccessURL page = (PageAccessURL)this.sqlMapClient.queryForObject("selectPageAccessURL", urlId); System.out.println(page.getUrl()); } }


注意:请仔细对比mybatis和ibatis的配置区别。

 

6)测试:

同方式二的测试;