首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring ibatis调整

2012-11-01 
spring ibatis整合文章分为2大部分,第1部分谈Spring对ibatis的整合,第2部分谈谈Spring事务管理,第一部分成

spring ibatis整合
  文章分为2大部分,第1部分谈Springibatis的整合,第2部分谈谈Spring事务管理,第一部分成功实现了,第二部分在实现的时候出现了问题,希望各位高手能多多指点。
  一 spring整合ibatis
  1 创建ibatis功能类
  由于ibatis是基于半自动ORM框架的,对于每个DAO都需要手动编写功能类和xml配置文件,写起来是很费事的。所以,我们选用ibatis的代码自动生成器去生成具体的功能类。
下面是用代码自动生成器生成的功能类:
  abatorConfig.xml文件,这个配置文件是ibatis的代码自动生成器需要配置的文件。

 <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" ><abatorConfiguration >  <abatorContext >    <jdbcConnection driverconnectionURL="jdbc:sqlserver://localhost:1433;databaseName=mydb" userId="sa" password="841026" >      <classPathEntry location="F:/Program Files/workspace/SpringTranslateDemo/driver/sqljdbc.jar" />    </jdbcConnection>    <javaModelGenerator targetPackage="com.whpu.computershop.ibatis.pojo" targetProject="SpringTranslateDemo" />    <sqlMapGenerator targetPackage="com.whpu.upanddown.ibatis.config" targetProject="SpringTranslateDemo" />    <daoGenerator targetPackage="com.whpu.upanddown.ibatis.dao" targetProject="SpringTranslateDemo" type="GENERIC-CI" />    <table schema="dbo" tableName="student" >     </table>  </abatorContext></abatorConfiguration>  

  自动生成的pojo:
package com.whpu.computershop.ibatis.pojo;

public class Student {    /**     * This field was generated by Abator for iBATIS.     * This field corresponds to the database column dbo.student.stuid     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    private Integer stuid;    /**     * This field was generated by Abator for iBATIS.     * This field corresponds to the database column dbo.student.stuname     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    private String stuname;    /**     * This field was generated by Abator for iBATIS.     * This field corresponds to the database column dbo.student.stusex     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    private String stusex;    /**     * This field was generated by Abator for iBATIS.     * This field corresponds to the database column dbo.student.stuage     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    private Integer stuage;    /**     * This field was generated by Abator for iBATIS.     * This field corresponds to the database column dbo.student.stuclass     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    private String stuclass;    /**     * This method was generated by Abator for iBATIS.     * This method returns the value of the database column dbo.student.stuid     *     * @return the value of dbo.student.stuid     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public Integer getStuid() {        return stuid;    }    /**     * This method was generated by Abator for iBATIS.     * This method sets the value of the database column dbo.student.stuid     *     * @param stuid the value for dbo.student.stuid     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public void setStuid(Integer stuid) {        this.stuid = stuid;    }    /**     * This method was generated by Abator for iBATIS.     * This method returns the value of the database column dbo.student.stuname     *     * @return the value of dbo.student.stuname     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public String getStuname() {        return stuname;    }    /**     * This method was generated by Abator for iBATIS.     * This method sets the value of the database column dbo.student.stuname     *     * @param stuname the value for dbo.student.stuname     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public void setStuname(String stuname) {        this.stuname = stuname;    }    /**     * This method was generated by Abator for iBATIS.     * This method returns the value of the database column dbo.student.stusex     *     * @return the value of dbo.student.stusex     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public String getStusex() {        return stusex;    }    /**     * This method was generated by Abator for iBATIS.     * This method sets the value of the database column dbo.student.stusex     *     * @param stusex the value for dbo.student.stusex     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public void setStusex(String stusex) {        this.stusex = stusex;    }    /**     * This method was generated by Abator for iBATIS.     * This method returns the value of the database column dbo.student.stuage     *     * @return the value of dbo.student.stuage     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public Integer getStuage() {        return stuage;    }    /**     * This method was generated by Abator for iBATIS.     * This method sets the value of the database column dbo.student.stuage     *     * @param stuage the value for dbo.student.stuage     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public void setStuage(Integer stuage) {        this.stuage = stuage;    }    /**     * This method was generated by Abator for iBATIS.     * This method returns the value of the database column dbo.student.stuclass     *     * @return the value of dbo.student.stuclass     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public String getStuclass() {        return stuclass;    }    /**     * This method was generated by Abator for iBATIS.     * This method sets the value of the database column dbo.student.stuclass     *     * @param stuclass the value for dbo.student.stuclass     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public void setStuclass(String stuclass) {        this.stuclass = stuclass;    }}

  自动生成的studao接口和studaoimpl实现类:
 package com.whpu.upanddown.ibatis.dao;import com.whpu.computershop.ibatis.pojo.Student;import com.whpu.computershop.ibatis.pojo.StudentExample;import java.sql.SQLException;import java.util.List;public interface StudentDAO {    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    void insert(Student record) ;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    int updateByPrimaryKey(Student record) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    int updateByPrimaryKeySelective(Student record) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    List selectByExample(StudentExample example) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    Student selectByPrimaryKey(Integer stuid) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    int deleteByExample(StudentExample example) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    int deleteByPrimaryKey(Integer stuid) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    int countByExample(StudentExample example) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    int updateByExampleSelective(Student record, StudentExample example) throws SQLException;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    int updateByExample(Student record, StudentExample example) throws SQLException;        List selectByname(String name) throws SQLException;        List selectByclassname(String classname) throws SQLException;   }

  
 
 package com.whpu.upanddown.ibatis.dao;import com.ibatis.sqlmap.client.SqlMapClient;import com.whpu.computershop.ibatis.pojo.Student;import com.whpu.computershop.ibatis.pojo.StudentExample;import java.sql.SQLException;import java.util.List;public class StudentDAOImpl implements StudentDAO {    /**     * This field was generated by Abator for iBATIS.     * This field corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    private SqlMapClient sqlMapClient;    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public StudentDAOImpl(SqlMapClient sqlMapClient) {        super();        this.sqlMapClient = sqlMapClient;    }    public StudentDAOImpl(){        super();    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public void insert(Student record)  {        try {sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);} catch (SQLException e) {e.printStackTrace();}    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public int updateByPrimaryKey(Student record) throws SQLException {        int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKey", record);        return rows;    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public int updateByPrimaryKeySelective(Student record) throws SQLException {        int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKeySelective", record);        return rows;    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public List selectByExample(StudentExample example) throws SQLException {        List list = sqlMapClient.queryForList("dbo_student.abatorgenerated_selectByExample", example);        return list;    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public Student selectByPrimaryKey(Integer stuid) throws SQLException {        Student key = new Student();        key.setStuid(stuid);        Student record = (Student) sqlMapClient.queryForObject("dbo_student.abatorgenerated_selectByPrimaryKey", key);        return record;    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public int deleteByExample(StudentExample example) throws SQLException {        int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByExample", example);        return rows;    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public int deleteByPrimaryKey(Integer stuid) throws SQLException {        Student key = new Student();        key.setStuid(stuid);        int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByPrimaryKey", key);        return rows;    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public int countByExample(StudentExample example) throws SQLException {        Integer count = (Integer)  sqlMapClient.queryForObject("dbo_student.abatorgenerated_countByExample", example);        return count.intValue();    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public int updateByExampleSelective(Student record, StudentExample example) throws SQLException {        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);        int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExampleSelective", parms);        return rows;    }    /**     * This method was generated by Abator for iBATIS.     * This method corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    public int updateByExample(Student record, StudentExample example) throws SQLException {        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);        int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExample", parms);        return rows;    }    /**     * This class was generated by Abator for iBATIS.     * This class corresponds to the database table dbo.student     *     * @abatorgenerated Thu Feb 11 21:34:33 CST 2010     */    private static class UpdateByExampleParms extends StudentExample {        private Object record;        public UpdateByExampleParms(Object record, StudentExample example) {            super(example);            this.record = record;        }        public Object getRecord() {            return record;        }    }        public List selectByname(String stuname) throws SQLException    {    List list=sqlMapClient.queryForList("dbo_student.selectbystuname", stuname);    return list;        }    public List selectByclassname(String classname) throws SQLException    {    List list=sqlMapClient.queryForList("dbo_student.selectbyclassname", classname);    return list;        }public void setSqlMapClient(SqlMapClient sqlMapClient) {this.sqlMapClient = sqlMapClient;}         }
 

  自动生成的studao配置文件,当然SqlMapConfig.xml文件需要手动去编写的。
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" ><sqlMap namespace="dbo_student" >  <resultMap id="abatorgenerated_StudentResult" >    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    <result column="stuid" property="stuid" jdbcType="INTEGER" />    <result column="stuname" property="stuname" jdbcType="VARCHAR" />    <result column="stusex" property="stusex" jdbcType="VARCHAR" />    <result column="stuage" property="stuage" jdbcType="INTEGER" />    <result column="stuclass" property="stuclass" jdbcType="VARCHAR" />  </resultMap>  <sql id="abatorgenerated_Example_Where_Clause" >    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    <iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" >      <isEqual property="oredCriteria[].valid" compareValue="true" >        (        <iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" >          $oredCriteria[].criteriaWithoutValue[]$        </iterate>        <iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and" >          $oredCriteria[].criteriaWithSingleValue[].condition$            #oredCriteria[].criteriaWithSingleValue[].value#        </iterate>        <iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" >          $oredCriteria[].criteriaWithListValue[].condition$          <iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," >            #oredCriteria[].criteriaWithListValue[].values[]#          </iterate>        </iterate>        <iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" >          $oredCriteria[].criteriaWithBetweenValue[].condition$          #oredCriteria[].criteriaWithBetweenValue[].values[0]# and          #oredCriteria[].criteriaWithBetweenValue[].values[1]#        </iterate>        )      </isEqual>    </iterate>  </sql>  <select id="abatorgenerated_selectByPrimaryKey" resultMap="abatorgenerated_StudentResult" parameter>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    select stuid, stuname, stusex, stuage, stuclass    from dbo.student    where stuid = #stuid:INTEGER#  </select>  <select id="abatorgenerated_selectByExample" resultMap="abatorgenerated_StudentResult" parameter>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    select stuid, stuname, stusex, stuage, stuclass    from dbo.student    <isParameterPresent >      <include refid="dbo_student.abatorgenerated_Example_Where_Clause" />      <isNotNull property="orderByClause" >        order by $orderByClause$      </isNotNull>    </isParameterPresent>  </select>  <delete id="abatorgenerated_deleteByPrimaryKey" parameter>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    delete from dbo.student    where stuid = #stuid:INTEGER#  </delete>  <delete id="abatorgenerated_deleteByExample" parameter>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    delete from dbo.student    <include refid="dbo_student.abatorgenerated_Example_Where_Clause" />  </delete>  <insert id="abatorgenerated_insert" parameter>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    insert into dbo.student (stuid, stuname, stusex, stuage, stuclass)    values (#stuid:INTEGER#, #stuname:VARCHAR#, #stusex:VARCHAR#, #stuage:INTEGER#,      #stuclass:VARCHAR#)  </insert>  <update id="abatorgenerated_updateByPrimaryKey" parameter>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    update dbo.student    set stuname = #stuname:VARCHAR#,      stusex = #stusex:VARCHAR#,      stuage = #stuage:INTEGER#,      stuclass = #stuclass:VARCHAR#    where stuid = #stuid:INTEGER#  </update>  <update id="abatorgenerated_updateByPrimaryKeySelective" parameter>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    update dbo.student    <dynamic prepend="set" >      <isNotNull prepend="," property="stuname" >        stuname = #stuname:VARCHAR#      </isNotNull>      <isNotNull prepend="," property="stusex" >        stusex = #stusex:VARCHAR#      </isNotNull>      <isNotNull prepend="," property="stuage" >        stuage = #stuage:INTEGER#      </isNotNull>      <isNotNull prepend="," property="stuclass" >        stuclass = #stuclass:VARCHAR#      </isNotNull>    </dynamic>    where stuid = #stuid:INTEGER#  </update>  <select id="abatorgenerated_countByExample" parameterresult>    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    select count(*) from dbo.student    <include refid="dbo_student.abatorgenerated_Example_Where_Clause" />  </select>  <update id="abatorgenerated_updateByExampleSelective" >    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    update dbo.student    <dynamic prepend="set" >      <isNotNull prepend="," property="record.stuid" >        stuid = #record.stuid:INTEGER#      </isNotNull>      <isNotNull prepend="," property="record.stuname" >        stuname = #record.stuname:VARCHAR#      </isNotNull>      <isNotNull prepend="," property="record.stusex" >        stusex = #record.stusex:VARCHAR#      </isNotNull>      <isNotNull prepend="," property="record.stuage" >        stuage = #record.stuage:INTEGER#      </isNotNull>      <isNotNull prepend="," property="record.stuclass" >        stuclass = #record.stuclass:VARCHAR#      </isNotNull>    </dynamic>    <isParameterPresent >      <include refid="dbo_student.abatorgenerated_Example_Where_Clause" />    </isParameterPresent>  </update>  <update id="abatorgenerated_updateByExample" >    <!--      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.      This element was generated on Thu Feb 11 21:34:33 CST 2010.    -->    update dbo.student    set stuid = #record.stuid:INTEGER#,      stuname = #record.stuname:VARCHAR#,      stusex = #record.stusex:VARCHAR#,      stuage = #record.stuage:INTEGER#,      stuclass = #record.stuclass:VARCHAR#    <isParameterPresent >      <include refid="dbo_student.abatorgenerated_Example_Where_Clause" />    </isParameterPresent>  </update>  <select id="selectbystuname" resultMap="abatorgenerated_StudentResult" parameter>  select stuid, stuname, stusex, stuage, stuclass    from dbo.student where stuname=#name#  </select>  <select id="selectbystuclass" resultMap="abatorgenerated_StudentResult" parameter>  select * from dbo.student as stu where stu.stuclass=#stuclass#  </select></sqlMap>
 

  配置spring的配置文件去整合IBatis的功能类
 
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 利用spring的DriverManagerDataSource类配置数据源 --><bean id="datasource"value="sa"></property><property name="password" value="841026"></property></bean><!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂--><bean id="sqlMapClient"/></property><property name="configLocation"value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml"></property></bean><bean id="transactionManager"/></property></bean><bean id="studentdaoimpl"/></property></bean><!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->    <!--<bean id="studao"/></property><property name="target"><ref local="studentdaoimpl" /></property><property name="transactionAttributes"><props><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="update*">PROPAGATION_REQUIRED</prop></props></property></bean>--><bean id="stu1" value="23"></property><property name="stuage" value="12"></property><property name="stuclass" value="1班"></property><property name="stusex" value="男"></property></bean><bean id="stu2" value="1"></property><property name="stuage" value="13"></property><property name="stuclass" value="2班"></property><property name="stusex" value="女"></property></bean></beans>

  上面注释掉的部分是spring的事务管理,本来想用spring去控制ibatis的事务管理,但没用成功,希望各位高手能看看,问题出在哪里?
   测试类:
  
package com.hjy.SpringTranslateDemo.Test;import java.sql.SQLException;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ibatis.sqlmap.client.SqlMapClient;import com.whpu.computershop.ibatis.pojo.Student;import com.whpu.upanddown.ibatis.dao.StudentDAO;import com.whpu.upanddown.ibatis.dao.StudentDAOImpl;public class Test {/** * @param args */public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");SqlMapClient sqlmap = (SqlMapClient) ac.getBean("sqlMapClient");// StudentDAO studao = (StudentDAO) ac.getBean("studao");// studentdaoimplStudentDAO studao = (StudentDAO) ac.getBean("studao");List list;try {list = studao.selectByname("张三");Student stu = (Student) list.iterator().next();System.out.println(stu.getStuname());System.out.println(stu.getStusex());System.out.println(stu.getStuclass());System.out.println(stu.getStuage());} catch (SQLException e) {e.printStackTrace();}}}

下面是测试结果:
  张三
  男
  1班
  20 

  二 Spring事务管理
  Spring事务管理可以让我们的事务在失败时能自动回滚,Spring的事务管理主要有两种实现方式,编程式和声明式。对于编程式,可以用统一的模版去处理事务,但个人感觉违背了侵入式的编程思想。声明式是很现在用的最多的事务处理方式,只需要配置spring的配置文件就可以了,很方便。spring2.x和spring1.x的配置方式是不一样的。
  我用的是spring1.x的配置方式,但在实现时出了问题,也不知道为什么,希望高手指出。
  Spring application.xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 利用spring的DriverManagerDataSource类配置数据源 --><bean id="datasource"value="sa"></property><property name="password" value="841026"></property></bean><!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂--><bean id="sqlMapClient"/></property><property name="configLocation"value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml"></property></bean><bean id="transactionManager"/></property></bean><bean id="studentdaoimpl"/></property></bean><!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->   <bean id="studao"/></property><property name="target"><ref local="studentdaoimpl" /></property><property name="transactionAttributes"><props><prop key="insert*">PROPAGATION_REQUIRED</prop><prop key="update*">PROPAGATION_REQUIRED</prop></props></property></bean><bean id="stu1" value="23"></property><property name="stuage" value="12"></property><property name="stuclass" value="1班"></property><property name="stusex" value="男"></property></bean><bean id="stu2" value="1"></property><property name="stuage" value="13"></property><property name="stuclass" value="2班"></property><property name="stusex" value="女"></property></bean></beans>

在成功插入第一条数据后,插入第二条会失败,我想失败后事务自动回滚,但第一条的数据依然成功插入了。感觉是配置文件有问题,但具体也不太清楚。
 
 

1 楼 mumian0417 2012-09-24   stu1,stu2,需要继承studao

热点排行