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配置文件:
[html] view plain copy print?
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
<typeAliases>
<typeAlias alias="pageAccessURL" type="com.lgm.mybatis.model.PageAccessURL" />
</typeAliases>
<environments default="development">
<environment id="development1">
<environment id="development2">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/appdb"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="poolMaximumActiveConnections" value="10"/>
<property name="poolMaximumIdleConnections" value="5"/>
<property name="poolMaximumCheckoutTime" value="20000"/>
<property name="poolTimeToWait" value="20000"/>
<property name="poolPingQuery" value="NO PING QUERY SET"/>
<property name="poolPingEnabled" value="false"/>
<property name="poolPingConnectionsNotUsedFor" value="0"/>
</dataSource>
</environment>
<environment id="development3">
<transactionManager type="JDBC"/>
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jndi/mybatis"/>
<property name="env.encoding" value="UTF8"/>
<mappers>
<mapper resource="com/lgm/mybatis/config/pageAccessURL.xml"/>
</mappers>
</configuration>
其中
加载事务配置
注:关于JNDI的配置,见tomcat的几种JNDI配置方法
3)mybatis的sql映射配置文件:
[html] view plain copy print?
<mapper namespace="pageAccessURL" >
<select id="selectPageAccessURL" parameterType="int" resultType="pageAccessURL" >
select * from tables where URL_ID = #{id}
</select>
<select id="selectPageAccessURLByClass" parameterType="pageAccessURL" resultType="pageAccessURL">
select * from tables where URL_ID = #{urlId} and URL = #{url}
</select>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
<typeAliases>
<typeAlias alias="pageAccessURL" type="com.lgm.mybatis.model.PageAccessURL" />
</typeAliases>
<mappers>
<mapper resource="com/lgm/mybatis/config/pageAccessURL.xml"/>
</mappers>
</configuration>
使用了spring管理的话,这里就不用设置数据源等其他配置了
5)mybatis的sql映射文件配置:
同方式一配置的sql映射文件配置
6)配置DAO层:
[html] view plain copy print?
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)测试:
[html] view plain copy print?
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
[html] view plain copy print?
<beans>
<import resource="applicationContext-dao.xml" />
</beans>
applicationContext-dao.xml
[html] view plain copy print?
<beans>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:mybatis-config-mappings.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/appdb" />
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="5" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="timeBetweenEvictionRunsMillis" value="120000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnReturn" value="true" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="pageAccessURLManager" class="com.lgm.mybatis.manager.PageAccessURLManager">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
</beans>
3)ibatis配置文件:
[html] view plain copy print?
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="false" />
<typeAlias alias="pageAccessURL" type="com.lgm.mybatis.model.PageAccessURL" />
<sqlMap resource="com/lgm/mybatis/config/pageAccessURL.xml"/>
</sqlMapConfig>
4)ibatis的sql映射配置文件:
[html] view plain copy print?
<sqlMap namespace="pageAccessURL">
<cacheModel id="productCache" type="LRU">
<flushInterval hours="24"/>
<property name="size" value="1000" />
</cacheModel>
<select id="selectPageAccessURL" parameterClass="int" resultClass="pageAccessURL" cacheModel="productCache">
select * from PAGE_ACCESS_URL where URL_ID = #id#
</select>
<select id="selectPageAccessURLByClass" parameterClass="pageAccessURL" resultClass="pageAccessURL">
select * from PAGE_ACCESS_URL where URL_ID = #urlId# and URL = #url#
</select>
<sql id="usercolumns">URL_ID as urlId,url,moduleId,state,mark</sql>
<select id="selectPageAccessURL2" parameterClass="int" resultClass="pageAccessURL">
select <include refid="usercolumns" />
from PAGE_ACCESS_URL where URL_ID = #id#
</select>
<insert id="insertTest" >
<selectKey keyProperty="id" resultClass="int" >
SELECT FLOOR(1 + (RAND() * 1000000));
</selectKey>
insert into table values(xx,xx);
</insert>
</sqlMap>
5)配置DAO层:
[html] view plain copy print?
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)测试:
同方式二的测试;
关于注解方式的我不是很喜欢,所以...
over....