ibatis 和 spring 集成
ApplicationContext.xml 配置
<bean id="sqlMapClient">
<property name="configLoaction">
<value>classpath:sqlMapConfig</value>
</property>
</bean>
----dao
<bean id="ibatisdao">
<property name="sqlMapClent"ref="sqlMapClient"/>
</bean>
----datasource
<bean id="xxx">
<propertyname="loactions">
<list>
<value>jdbc.propertes</value>
</list>
</property>
</bean>
<bean id="dataSource">
<propertyname="driverClass"><value>${jdbc.driverClass}</value></property>
</bean>
----jdbc.propertes
jdbc.driverClass=com.mysql.jdbc.Driver
......
ibatis----sqlMapConfig.xml
<sqlMapConfig>
<sqlMapresource="com/spring/bean/Book.xml">
</sqlMapConfig>
-----------
spring+ibatis 事务管理配置
<bean id="txManager">
<property ref="datasource"ref="datasource">
</bean>
<tx:advice id="txAdvice"transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="bizMethod"execution="*.com.company.service.*.*(..)"/>
<aop:advisoradvice-ref="txAdvice" pointcut-ref="bizMethod"/>
</aop:config>
---------------------------------------
ibatis 2.0配置模板
数据库配置文件:
sqlMap.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=zhanqiong
password=zhanqiong
属性文件:
student.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEsqlMapConfig
PUBLIC"-//ibatis.apache.org//DTD SQL Map2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
</sqlMap>
总配置文件:
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEsqlMapConfig
PUBLIC"-//ibatis.apache.org//DTD SQL Map Config2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="false"enhancementEnabled="true"
lazyLoadingEnabled="false" errorTracingEnabled="true"maxRequests="400"
maxSessions="40" maxTransactions="32" useStatementNamespaces="true"/>
<propertiesresource="SqlMap.properties"/>
<transactionManager type="JDBC"commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"value="${driver}"/>
<property name="JDBC.ConnectionURL"value="${url}"/>
<property name="JDBC.Username"value="${username}"/>
<property name="JDBC.Password"value="${password}"/>
</dataSource>
</transactionManager>
<sqlMapresource="com/mydomain/data/Student.xml"/>
</sqlMapConfig>
-----ibatis 3.0 配置模板
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEconfiguration
PUBLIC "-//ibatis.apache.org//DTD Config3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<propertiesresource="SqlMap.properties"/>
<typeAliases>
<typeAlias type="com.johe.entity.Userinfo"alias="test"/>
</typeAliases>
<environmentsdefault="development">
<environmentid="development">
<transactionManagertype="JDBC"/>
<dataSourcetype="POOLED">
<property name="driver"value="${driver}"/>
<property name="url"value="${url}"/>
<property name="username"value="${username}"/>
<property name="password"value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapperresource="com/johe/entity/Userinfo.xml"/>
</mappers>
entity mapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEmapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mappernamespace="bin">
<select id="selectAllStudent"resultType="test">
select*
fromuserinfo
</select>
</mapper>
--------------------------------
自己写的一个ibatis 3.1 的 demo
package com.johe.entity;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class ISessionFactory {
public SqlSessionFactorybuildSqlSessionFactory(){
try{
String resource ="SqlMapConfig.xml";
Reader reader =Resources.getResourceAsReader(resource);
return newSqlSessionFactoryBuilder().build(reader);
} catch (Exception e){
System.out.println("failed to buildSqlSessionFactory");
e.printStackTrace();
returnnull;
}
}
public SqlSessiongetSession(){
returnbuildSqlSessionFactory().openSession();
}
}
package com.johe.entity;
public class Userinfo implements java.io.Serializable{
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.johe.entity;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
public class Test extends ISessionFactory {
public static void main(String[] args) {
Test t = new Test();
SqlSession session =t.getSession();
//Li st list =session.selectList("selectAllStudent");
Userinfo user =(Userinfo)session.selectList("selectStudentById", 6).get(0);
System.out.println(user.getPassword());
}
}
个配置文件基于上边 3.0 配置 properties配置
sqlMap.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=root