Spring整合iBatis之二:从iBatis角度看两框架整合



    总的来说,iBatis功能上基本不变、结构不变, iBatis的VO(也就是model)不变,配置文件数量和作用基本上不变。

    

    有变化的地方:

    1. 在DAO的实现上(DAOImpl)不用过SqlMapClient执行SQL语句,改用spring提供的SqlMapClientTemplate

    2.SqlMapConfig.xml中,数据库的配置不再由iBatis处理,交给spring的配置文件统一管理

    3.事务处理完全交给spring


一:需要修改的文件

1:StudentDAOImpl.java

StudentDAOImpl除了要继承IDAO接口,还要继承与spring提供的SqlMapClientDaoSupport

之后,不用再手动从配置文件加载配置,交给spring注入iBatis配置

执行上都用类似getSqlMapClientTemplate().insert("Stu_namespace.insertStudent",stu);的方法执行

package com.rt.sidemo.dao;

import java.util.List;
import com.rt.sidemo.model.Student;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;//spring支持iBatis的包

public class StudentDAOImpl extends SqlMapClientDaoSupport implements IStudentDAO
{
	/*这儿不用手动初始化了,等着spring注入完直接用
	 * spring会往service里,注入一个带dataSource和ibatis配置的dao
	 * 
	private static SqlMapClient smc = null;//SqlMapClient带有很多增删改查的方法
	static//静态初始化一次就够了
	{
		try {
			Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader("SqlMapConfig.xml");//借助Reader读入xml配置,注意位置
			smc = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();//不再需要Reader了,关之	
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	*/
	
	//重写的方法中,用SqlMapClient的地方都换成getSqlMapClientTemplate()
	@Override
	public void addStudent(Student stu) {
		getSqlMapClientTemplate().insert("Stu_namespace.insertStudent",stu);
		System.out.println("插入时返回的对象=》"+stu.getStudentid());//打印返回的值	
	}

	@Override
	public void addStudentBySequence(Student stu) {
		getSqlMapClientTemplate().insert("Stu_namespace.insertStudentBySequence",stu);
		System.out.println("插入时返回的对象=》"+stu.getStudentid());//打印返回的值
		
	}

	@Override
	public void delStudentById(int id) {
		int deletedCount = 0;
		deletedCount = getSqlMapClientTemplate().delete("Stu_namespace.deleteStudentById",id);
		System.out.println("deleteCount=>"+deletedCount);
	}

	@Override
	public void updateStudent(Student stu) {
		int updatedCount = 0;
		updatedCount = getSqlMapClientTemplate().update("Stu_namespace.updateStudent", stu);
		System.out.println("updatedCount=>"+updatedCount);
	}

	@Override
	public Student selectStudentById(int id) {
		Student stu = null;
		stu =(Student) getSqlMapClientTemplate().queryForObject("Stu_namespace.selectStudentById",id);
		return stu;
	}

	@Override
	public List<Student> selectStudentByName(String name) {
		List<Student> stus = null;
		stus =getSqlMapClientTemplate().queryForList("Stu_namespace.selectStudentByName",name);
		return stus;
	}

	@Override
	public List<Student> selectAll() {
		List<Student> stus = null;
		stus =getSqlMapClientTemplate().queryForList("Stu_namespace.selectAllStudent");
		return stus;
	}

	
}



2:sqlMapConfig.xml

datasource不用管了,事务处理完全委托给spring

等于只负责命名空间具体映射

<?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的命名空间,整合后仍然归此文件管 -->
  <!-- 但是dataSource配置归spring管了 -->
  <settings useStatementNamespaces="true"/> 

  <!-- 数据库配置都交给applicationContext-common.xml文件管理了
  <transactionManager type="JDBC">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
      <property name="JDBC.Username" value="scott"/>
      <property name="JDBC.Password" value="890307"/>
    </dataSource>
  </transactionManager>
  -->

  <!-- 各个表和类对应的配置,这个不变 -->
  <!-- 具体的配置,也不变,还用ibatis原来的配置 -->
  <sqlMap resource="Student.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>




二:不用修改的文件:

1.IStudentDAO.java

package com.rt.sidemo.dao;

import java.util.List;
import com.rt.sidemo.model.Student;

public interface IStudentDAO 
{
	//增
	public void addStudent(Student stu);
	public void addStudentBySequence(Student stu);//主键序列自增
	//删
	public void delStudentById(int id);
	//改
	public void updateStudent(Student stu);
	
	//查
	public Student selectStudentById(int id);
	public List<Student> selectStudentByName(String name);//用name模糊查询
	public List<Student> selectAll();//查寻全部
}



2.student.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">

<!-- 和spring整合后也不用改 -->
<sqlMap namespace="Stu_namespace">

  <!-- 别名,起别名以后class里就不用每次都写包了 -->
  <typeAlias alias="Student" type="com.rt.sidemo.model.Student"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties 
       exactly.-->
  <resultMap id="StudentResult" class="Student">
    <result property="studentid" column="studentid"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
  </resultMap>
	
  <!-- 1.查找全部,官方推荐用resultMap,暂时用简单的resultClass代替 -->
  <select id="selectAllStudent" resultClass="Student">
    SELECT * 
    FROM Student
  </select>

  <!-- 2.查询主键,多了一个接收参数的类型parameterClass,这里井号中是占位符 -->
  <select id="selectStudentById" parameterClass="int" resultClass="Student">
    SELECT
      studentid,
      name,
      age
    FROM student
    WHERE studentid = #id#
  </select>

  <!-- 3.模糊查找,占位符必须用$ -->
  <select id="selectStudentByName" parameterClass="String" resultClass="Student">
    SELECT
      studentid,
      name,
      age
    FROM student
    WHERE name LIKE '%$name$%'
  </select>
  
  <!-- 4.删除操作 -->
  <delete id="deleteStudentById" parameterClass="int">
    DELETE FROM student WHERE studentid = #studentid#
  </delete>
  
  <!-- 5.增加指定 -->
  <insert id="insertStudent" parameterClass="Student">
    INSERT into Student (studentid,name,age)
    VALUES  (#studentid#, #name#, #age#)
  </insert>
  
  <!-- 6.序列增长,要指定自增长的主键字段名 -->
  <!-- selectKey相当于查询一次,把int类型的结果赋值给:传参的studentid: -->
  <insert id="insertStudentBySequence" parameterClass="Student">
   <selectKey resultClass="int" keyProperty="studentid">
	 SELECT studentPKSequence.nextVal AS studentid
	 FROM dual
   </selectKey>
   	
   	INSERT into Student (studentid,name,age)
    VALUES  (#studentid#, #name#, #age#)
  </insert>

  <!-- 7.按主键更新 -->
  <update id="updateStudent" parameterClass="Student">
    update Student set
      studentid = #studentid#,
      name = #name#,
      age = #age#
    where
      studentid = #studentid#
  </update>
  
</sqlMap>


3.Student.java

package com.rt.sidemo.model;

public class Student 
{
	private int studentid;
	private String name;
	private int age;
	
	public String toString()
	{
		return " id=>"+studentid+" name=>"+name+" age=>"+age;
	}
	
	//setter&getter
	public int getStudentid() {
		return studentid;
	}
	public void setStudentid(int studentid) {
		this.studentid = studentid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}


补个文件结构:


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值