ibatis3和hibernate3整合spring3(主要講解搭建項目時的配置文件) 一、定義了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> 二、定義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 三、配置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> 四、ibatis 4.一、配置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.二、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); } }