Ibatis與Spring搭建過程

muyuqinchen發表於2014-04-15

1、spring的applicationContext.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">
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/hammer?useUnicode=true&amp;characterEncoding=utf8</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>root</value>
		</property>
	</bean>
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation">
			<value>sqlMapConfig.xml</value>
		</property>
	</bean>
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
	</bean>
	<bean id="studentDAO" class="com.hammer.dao.StudentDAOImpl">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
		<property name="sqlMapClient">
			<ref local="sqlMapClient"/>
		</property>
	</bean>
	<bean id="studentDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref bean="transactionManager"/>
		</property>
		<property name="target">
			<ref local="studentDAO"/>
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>
</beans>

 

2、sqlMapConfig.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
 <!-- 配置ibatis相關引數 -->
 <settings useStatementNamespaces="true"/><!-- 是否啟用SQL指令碼配置中的namespace -->
 <sqlMap resource="com/hammer/sqlmap/student_SqlMap.xml" />
</sqlMapConfig>

 3、student_SqlMap.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="student" >
  <resultMap id="abatorgenerated_StudentResult" class="com.hammer.model.Student" >
    <!--
      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
      This element was generated on Mon Apr 14 18:43:27 CST 2014.
    -->
    <result column="id" property="id" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="SMALLINT" />
    <result column="address" property="address" jdbcType="VARCHAR" />
  </resultMap>
  <select id="abatorgenerated_getAllUsers" resultMap="abatorgenerated_StudentResult">
    select id, name, age, address
    from student
  </select>
  <select id="abatorgenerated_selectByPrimaryKey" resultMap="abatorgenerated_StudentResult" parameterClass="com.hammer.model.Student" >
    <!--
      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
      This element was generated on Mon Apr 14 18:43:27 CST 2014.
    -->
    select id, name, age, address
    from student
    where id = #id:VARCHAR#
  </select>
  <select id="abatorgenerated_selectByName" resultMap="abatorgenerated_StudentResult" parameterClass="com.hammer.model.Student" >
    <!--
      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
      This element was generated on Mon Apr 14 18:43:27 CST 2014.
    -->
    select id, name, age, address
    from student
    where name = #name:VARCHAR#
  </select>
  <delete id="abatorgenerated_deleteByPrimaryKey" parameterClass="com.hammer.model.Student" >
    <!--
      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
      This element was generated on Mon Apr 14 18:43:27 CST 2014.
    -->
    delete from student
    where id = #id:VARCHAR#
  </delete>
  <insert id="abatorgenerated_insert" parameterClass="com.hammer.model.Student" >
    <!--
      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
      This element was generated on Mon Apr 14 18:43:27 CST 2014.
    -->
    insert into student (id, name, age, address)
    values (#id:VARCHAR#, #name:VARCHAR#, #age:SMALLINT#, #address:VARCHAR#)
  </insert>
  <update id="abatorgenerated_updateByPrimaryKey" parameterClass="com.hammer.model.Student" >
    <!--
      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
      This element was generated on Mon Apr 14 18:43:27 CST 2014.
    -->
    update student
    set name = #name:VARCHAR#,
      age = #age:SMALLINT#,
      address = #address:VARCHAR#
    where id = #id:VARCHAR#
  </update>
  <update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.hammer.model.Student" >
    <!--
      WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
      This element was generated on Mon Apr 14 18:43:27 CST 2014.
    -->
    update student
    <dynamic prepend="set" >
      <isNotNull prepend="," property="name" >
        name = #name:VARCHAR#
      </isNotNull>
      <isNotNull prepend="," property="age" >
        age = #age:SMALLINT#
      </isNotNull>
      <isNotNull prepend="," property="address" >
        address = #address:VARCHAR#
      </isNotNull>
    </dynamic>
    where id = #id:VARCHAR#
  </update>

</sqlMap>

 4、model.Student類

public class Student {
	
	public Student(){
	}
	
	public Student(String id,String name,Integer age,String address){
		this.id=id;
		this.name=name;
		this.age=age;
		this.address=address;
	}
	
    /**
     * This field was generated by Abator for iBATIS.
     * This field corresponds to the database column student.id
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    private String id;

    /**
     * This field was generated by Abator for iBATIS.
     * This field corresponds to the database column student.name
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    private String name;

    /**
     * This field was generated by Abator for iBATIS.
     * This field corresponds to the database column student.age
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    private Integer age;

    /**
     * This field was generated by Abator for iBATIS.
     * This field corresponds to the database column student.address
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    private String address;

    /**
     * This method was generated by Abator for iBATIS.
     * This method returns the value of the database column student.id
     *
     * @return the value of student.id
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public String getId() {
        return id;
    }

    /**
     * This method was generated by Abator for iBATIS.
     * This method sets the value of the database column student.id
     *
     * @param id the value for student.id
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * This method was generated by Abator for iBATIS.
     * This method returns the value of the database column student.name
     *
     * @return the value of student.name
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public String getName() {
        return name;
    }

    /**
     * This method was generated by Abator for iBATIS.
     * This method sets the value of the database column student.name
     *
     * @param name the value for student.name
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * This method was generated by Abator for iBATIS.
     * This method returns the value of the database column student.age
     *
     * @return the value of student.age
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public Integer getAge() {
        return age;
    }

    /**
     * This method was generated by Abator for iBATIS.
     * This method sets the value of the database column student.age
     *
     * @param age the value for student.age
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * This method was generated by Abator for iBATIS.
     * This method returns the value of the database column student.address
     *
     * @return the value of student.address
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public String getAddress() {
        return address;
    }

    /**
     * This method was generated by Abator for iBATIS.
     * This method sets the value of the database column student.address
     *
     * @param address the value for student.address
     *
     * @abatorgenerated Mon Apr 14 18:43:27 CST 2014
     */
    public void setAddress(String address) {
        this.address = address;
    }
}

 5、StudentDAO介面定義

import com.hammer.model.Student;
import java.util.List;

public interface StudentDAO {
	public List getList();

	public Student getByName(String name);

	public Student getById(String id);

	public void save(Student student);

	public void delete(String id);

	public void update(Student student);
}

 6、StudentDAOImpl具體實現呼叫SQL語句

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.hammer.model.Student;

public class StudentDAOImpl extends SqlMapClientDaoSupport implements StudentDAO {

	public void delete(String id) {
		Student key = new Student();
        key.setId(id);
        getSqlMapClientTemplate().delete("student.abatorgenerated_deleteByPrimaryKey", key);
	}

	public Student getById(String id) {
		Student key = new Student();
        key.setId(id);
        Student record = (Student) getSqlMapClientTemplate().queryForObject("student.abatorgenerated_selectByPrimaryKey", key);
        return record;
	}

	public Student getByName(String name) {
		Student student = new Student();
		student.setName(name);
        Student record = (Student) getSqlMapClientTemplate().queryForObject("student.abatorgenerated_selectByName", student);
        return record;
	}

	public List getList() {
		List list = getSqlMapClientTemplate().queryForList("student.abatorgenerated_getAllUsers", null);
        return list;
	}

	public void save(Student student) {
		getSqlMapClientTemplate().insert("student.abatorgenerated_insert", student);
	}

	public void update(Student student) {
		getSqlMapClientTemplate().update("student.abatorgenerated_updateByPrimaryKey", student);
	}
	
}

 

相關文章