ibatis3和hibernate3整合spring3(主要讲解搭建项目时的配置文件)
1、定义了spring-datasource-jdbc.xml
<?xml version="1.0" encoding="GBK"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${msdLibrary.db.driverClass}"></property>
<property name="jdbcUrl" value="${msdLibrary.db.jdbcUrl}"></property>
<property name="user" value="${msdLibrary.db.username}"></property>
<property name="password" value="${msdLibrary.db.password}"></property>
<property name="initialPoolSize"><value>10</value></property>
<property name="minPoolSize"><value>5</value></property>
<property name="maxPoolSize"><value>30</value></property>
<property name="acquireIncrement"><value>5</value></property>
<property name="maxIdleTime"><value>10</value></property>
<property name="maxStatements"><value>0</value></property>
</bean>
</beans>
2、定义jdbc配置属性值jdbc.properties
msdLibrary.db.driverClass=com.mysql.jdbc.Driver
msdLibrary.db.jdbcUrl=jdbc:mysql://localhost:3306/stu
msdLibrary.db.username=root
msdLibrary.db.password=root
msdLibrary.db.idleConnectionTestPeriod=240
msdLibrary.db.idleMaxAge=60
msdLibrary.db.maxConnectionsPerPartition=20
msdLibrary.db.minConnectionsPerPartition=52
msdLibrary.db.partitionCount=2
msdLibrary.db.acquireIncrement=2
msdLibrary.db.statementsCacheSize=0
msdLibrary.db.releaseHelperThreads=3
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.autocommit=flase
3、配置applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"
default-autowire="byName">
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:annotation-config />
<context:component-scan base-package="com.stu" />
<!-- 导入Spring配置文件 -->
<import resource="spring-datasource-jdbc.xml" />
<!-- 定义受环境影响易变的变量 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<!--可以配置多个资源文件 -->
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- Hibernate配置 -->
<bean id="sessionFactoryBase"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.connection.autocommit">${hibernate.autocommit}</prop>
</props>
</property>
<!-- 对使的annotation对象扫描 -->
<property name="packagesToScan">
<list>
<value>com.stu.entity</value>
</list>
</property>
</bean>
<bean id="sessionFactory" parent="sessionFactoryBase">
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置ibatis的数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:spring_ibaits_mapping.xml" />
</bean>
<!--通过扫描的模式,扫描目录在com/hoo/mapper目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.stu.mapper" />
<property name="markerInterface" value="com.stu.mapper.SqlMapper" />
</bean>
<!-- 单独配置一个Mapper; 这种模式就是得给每个mapper接口配置一个bean -->
<!-- <bean id="stuMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="mapperInterface"
value="com.stu.mapper.MStudentDao" /> </bean> -->
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.stu.*.service..*Impl.*(..))"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="getVerify" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
4、ibatis
4.1、配置ibatis的配置文件
spring_ibaits_mapping.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="userInfoDO" type="com.stu.entity.UserInfoDO"/> //配置实体与数据库映射,注:实体属性名必须与数据库一致
</typeAliases>
<mappers>
<mapper resource="userInfo_sqlmap_mapping.xml"/>
</mappers>
</configuration>
userInfo_sqlmap_mapping.xml sql映射文件常用的增、删、改和分页
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.stu.mapper.UserInfoMapper">
<insert id="saveUserInfo" parameterType="userInfoDO">
insert into userinfo
(userId,email,province,sex,city,passWord,brithday,nickName,loginCount,userKey,status)
values
(#{userId},#{email},#{province},#{sex},#{city},#{passWord},#{brithday},#{nickName},#{loginCount},#{userKey},#{status})
</insert>
<update id="updateUserInfo" parameterType="userInfoDO">
update userinfo
set
<if test="email != null">
email=#{email},
</if>
<if test="email != null">
province=#{province},
</if>
<if test="email != null">
sex=#{sex},
</if>
<if test="email != null">
city=#{city},
</if>
<if test="email != null">
passWord=#{passWord},
</if>
<if test="email != null">
brithday=#{brithday},
</if>
<if test="email != null">
nickName=#{nickName},
</if>
<if test="email != null">
loginCount=#{loginCount},
</if>
<if test="email != null">
userKey=#{userKey},
</if>
<if test="email != null">
status=#{status}
</if>
where userId=#{userId}
</update>
<delete id="delUserInfo" parameterType="String">
delete from userinfo
where
userId=#{userId}
</delete>
<select id="getUserInfo" parameterType="userInfoDO" resultMap="userInfoDO">
select email as emailt from userinfo where 1=1
<if test="userId != null">
and userId =#{userId}
</if>
<if test="email != null">
and email =#{email}
</if>
<if test="passWord != null">
and passWord =#{passWord}
</if>
</select>
<select id="queryUserInfo" parameterType="map" resultMap="userInfoDO">
select *
from userinfo
limit #{pageIndex},#{pageSize}
</select>
<select id="queryCountUserInfo" parameterType="map" resultType="int">
select count(*) from userinfo
</select>
</mapper>
4.2、dao只需要接口无需实现接口,方法必须与sql映射文件中的id名相同
UserInfoMapper
package com.stu.mapper;
import java.util.List;
import java.util.Map;
import com.stu.entity.UserInfoDO;
public interface UserInfoMapper extends SqlMapper{ //继承SqlMapper只是一个空接口
/**
* 添加用户信息
* @param userInfoDO
* @return
*/
public void saveUserInfo(UserInfoDO userInfoDO);
/**
* 修改用户信息
* @param userInfoDO
*/
public void updateUserInfo(UserInfoDO userInfoDO);
/**
* 根据userId删除用户信息
* @param userId
*/
public void delUserInfo(String userId);
/**
* 根据userId,email,password,三个条件查询信息
* @param userInfoDO
* @return
*/
public UserInfoDO getUserInfo(UserInfoDO userInfoDO);
/**
* 查询用户信息
* @param userInfoDO
* @return
*/
public List<UserInfoDO> queryUserInfo(Map map);
/**
* 查询条数
* @param map
* @return
*/
public int queryCountUserInfo(Map map);
}
业务层
UserInfoServiceImpl 实现业务层接口类
package com.stu.system.service.impl;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.stu.commons.Exception.ServiceException;
import com.stu.entity.UserInfoDO;
import com.stu.mapper.UserInfoMapper;
import com.stu.system.service.UserInfoService;
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
@Inject
private UserInfoMapper userMapper; //自动注入
@Override
public void saveUserInfo(UserInfoDO userInfoDO) throws ServiceException{
// TODO Auto-generated method stub
userMapper.saveUserInfo(userInfoDO);
}
@Override
public void updateUserInfo(UserInfoDO userInfoDO) throws ServiceException {
// TODO Auto-generated method stub
userMapper.updateUserInfo(userInfoDO);
}
@Override
public void delUserInfo(String userId) throws ServiceException {
// TODO Auto-generated method stub
userMapper.delUserInfo(userId);
}
@Override
public UserInfoDO getUserInfo(UserInfoDO userInfoDO)
throws ServiceException {
// TODO Auto-generated method stub
return userMapper.getUserInfo(userInfoDO);
}
@Override
public List<UserInfoDO> queryUserInfo(Map map) throws ServiceException {
// TODO Auto-generated method stub
return userMapper.queryUserInfo(map);
}
@Override
public int queryCountUserInfo(Map map) throws ServiceException {
// TODO Auto-generated method stub
return userMapper.queryCountUserInfo(map);
}
}
1、定义了spring-datasource-jdbc.xml
<?xml version="1.0" encoding="GBK"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${msdLibrary.db.driverClass}"></property>
<property name="jdbcUrl" value="${msdLibrary.db.jdbcUrl}"></property>
<property name="user" value="${msdLibrary.db.username}"></property>
<property name="password" value="${msdLibrary.db.password}"></property>
<property name="initialPoolSize"><value>10</value></property>
<property name="minPoolSize"><value>5</value></property>
<property name="maxPoolSize"><value>30</value></property>
<property name="acquireIncrement"><value>5</value></property>
<property name="maxIdleTime"><value>10</value></property>
<property name="maxStatements"><value>0</value></property>
</bean>
</beans>
2、定义jdbc配置属性值jdbc.properties
msdLibrary.db.driverClass=com.mysql.jdbc.Driver
msdLibrary.db.jdbcUrl=jdbc:mysql://localhost:3306/stu
msdLibrary.db.username=root
msdLibrary.db.password=root
msdLibrary.db.idleConnectionTestPeriod=240
msdLibrary.db.idleMaxAge=60
msdLibrary.db.maxConnectionsPerPartition=20
msdLibrary.db.minConnectionsPerPartition=52
msdLibrary.db.partitionCount=2
msdLibrary.db.acquireIncrement=2
msdLibrary.db.statementsCacheSize=0
msdLibrary.db.releaseHelperThreads=3
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.autocommit=flase
3、配置applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"
default-autowire="byName">
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:annotation-config />
<context:component-scan base-package="com.stu" />
<!-- 导入Spring配置文件 -->
<import resource="spring-datasource-jdbc.xml" />
<!-- 定义受环境影响易变的变量 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<!--可以配置多个资源文件 -->
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- Hibernate配置 -->
<bean id="sessionFactoryBase"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.connection.autocommit">${hibernate.autocommit}</prop>
</props>
</property>
<!-- 对使的annotation对象扫描 -->
<property name="packagesToScan">
<list>
<value>com.stu.entity</value>
</list>
</property>
</bean>
<bean id="sessionFactory" parent="sessionFactoryBase">
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置ibatis的数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:spring_ibaits_mapping.xml" />
</bean>
<!--通过扫描的模式,扫描目录在com/hoo/mapper目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.stu.mapper" />
<property name="markerInterface" value="com.stu.mapper.SqlMapper" />
</bean>
<!-- 单独配置一个Mapper; 这种模式就是得给每个mapper接口配置一个bean -->
<!-- <bean id="stuMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="mapperInterface"
value="com.stu.mapper.MStudentDao" /> </bean> -->
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.stu.*.service..*Impl.*(..))"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="getVerify" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
4、ibatis
4.1、配置ibatis的配置文件
spring_ibaits_mapping.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="userInfoDO" type="com.stu.entity.UserInfoDO"/> //配置实体与数据库映射,注:实体属性名必须与数据库一致
</typeAliases>
<mappers>
<mapper resource="userInfo_sqlmap_mapping.xml"/>
</mappers>
</configuration>
userInfo_sqlmap_mapping.xml sql映射文件常用的增、删、改和分页
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.stu.mapper.UserInfoMapper">
<insert id="saveUserInfo" parameterType="userInfoDO">
insert into userinfo
(userId,email,province,sex,city,passWord,brithday,nickName,loginCount,userKey,status)
values
(#{userId},#{email},#{province},#{sex},#{city},#{passWord},#{brithday},#{nickName},#{loginCount},#{userKey},#{status})
</insert>
<update id="updateUserInfo" parameterType="userInfoDO">
update userinfo
set
<if test="email != null">
email=#{email},
</if>
<if test="email != null">
province=#{province},
</if>
<if test="email != null">
sex=#{sex},
</if>
<if test="email != null">
city=#{city},
</if>
<if test="email != null">
passWord=#{passWord},
</if>
<if test="email != null">
brithday=#{brithday},
</if>
<if test="email != null">
nickName=#{nickName},
</if>
<if test="email != null">
loginCount=#{loginCount},
</if>
<if test="email != null">
userKey=#{userKey},
</if>
<if test="email != null">
status=#{status}
</if>
where userId=#{userId}
</update>
<delete id="delUserInfo" parameterType="String">
delete from userinfo
where
userId=#{userId}
</delete>
<select id="getUserInfo" parameterType="userInfoDO" resultMap="userInfoDO">
select email as emailt from userinfo where 1=1
<if test="userId != null">
and userId =#{userId}
</if>
<if test="email != null">
and email =#{email}
</if>
<if test="passWord != null">
and passWord =#{passWord}
</if>
</select>
<select id="queryUserInfo" parameterType="map" resultMap="userInfoDO">
select *
from userinfo
limit #{pageIndex},#{pageSize}
</select>
<select id="queryCountUserInfo" parameterType="map" resultType="int">
select count(*) from userinfo
</select>
</mapper>
4.2、dao只需要接口无需实现接口,方法必须与sql映射文件中的id名相同
UserInfoMapper
package com.stu.mapper;
import java.util.List;
import java.util.Map;
import com.stu.entity.UserInfoDO;
public interface UserInfoMapper extends SqlMapper{ //继承SqlMapper只是一个空接口
/**
* 添加用户信息
* @param userInfoDO
* @return
*/
public void saveUserInfo(UserInfoDO userInfoDO);
/**
* 修改用户信息
* @param userInfoDO
*/
public void updateUserInfo(UserInfoDO userInfoDO);
/**
* 根据userId删除用户信息
* @param userId
*/
public void delUserInfo(String userId);
/**
* 根据userId,email,password,三个条件查询信息
* @param userInfoDO
* @return
*/
public UserInfoDO getUserInfo(UserInfoDO userInfoDO);
/**
* 查询用户信息
* @param userInfoDO
* @return
*/
public List<UserInfoDO> queryUserInfo(Map map);
/**
* 查询条数
* @param map
* @return
*/
public int queryCountUserInfo(Map map);
}
业务层
UserInfoServiceImpl 实现业务层接口类
package com.stu.system.service.impl;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.stu.commons.Exception.ServiceException;
import com.stu.entity.UserInfoDO;
import com.stu.mapper.UserInfoMapper;
import com.stu.system.service.UserInfoService;
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
@Inject
private UserInfoMapper userMapper; //自动注入
@Override
public void saveUserInfo(UserInfoDO userInfoDO) throws ServiceException{
// TODO Auto-generated method stub
userMapper.saveUserInfo(userInfoDO);
}
@Override
public void updateUserInfo(UserInfoDO userInfoDO) throws ServiceException {
// TODO Auto-generated method stub
userMapper.updateUserInfo(userInfoDO);
}
@Override
public void delUserInfo(String userId) throws ServiceException {
// TODO Auto-generated method stub
userMapper.delUserInfo(userId);
}
@Override
public UserInfoDO getUserInfo(UserInfoDO userInfoDO)
throws ServiceException {
// TODO Auto-generated method stub
return userMapper.getUserInfo(userInfoDO);
}
@Override
public List<UserInfoDO> queryUserInfo(Map map) throws ServiceException {
// TODO Auto-generated method stub
return userMapper.queryUserInfo(map);
}
@Override
public int queryCountUserInfo(Map map) throws ServiceException {
// TODO Auto-generated method stub
return userMapper.queryCountUserInfo(map);
}
}
来源:oschina
链接:https://my.oschina.net/u/149316/blog/17551