spring+ibatis【基本配置及动态sql】

1、准备jar:commons-dbcp.jar、commons-logging.jar、commons-pool.jar、ibatis-common-2.jar、ibatis-sqlmap-2.jar、javax.servlet-5.1.12.jar、log4j-1.2.14.jar、spring-2.0.jar、spring-beans-2.0.6.jar、spring-context-2.0.6.jar、spring-core-2.0.6.jar、spring-web-2.0.6.jar、spring-webmvc.jar

JDBC jar:本文以oracle为例,ojdbc14.jar

2、工程目录:

spring+ibatis【基本配置及动态sql】_第1张图片

3、实体配置 Ibatis.xml 、Eq.xml

Ibatis.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 >
  <!-- 配置实体;user:相当于取了一个别名 -->
  <typeAlias type="com.tmall.bean.Ibatis" alias="user"/>
  <!-- 设置resultMap,column是数据表的列,property实际是实体bean中的属性 -->
  <resultMap id="ibatisTest" class="user" >
    <result column="id" property="id" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
  </resultMap>
  <!-- id:为这个sql取一个别名;resultMap:返回值类型 -->
  <!-- 获得全查询列表 -->
  <select id="getAllUsers" resultMap="ibatisTest">
    select * from Ibatis
  </select>
  
   <!-- 根据用户名获得用户对象 -->
   <select id="getUsersByName" resultMap="ibatisTest">
     select * from Ibatis where name=#value#
  </select> 
  
  <!-- 根据id获得用户对象 -->
    <select id="getUsersById" resultMap="ibatisTest">
     select * from Ibatis where id=#value#
  </select> 
   <!-- 根据id和name获得用户对象 -->
    <select id="getUsersByIdAndName" resultMap="ibatisTest" parameterClass="java.util.Map" >
     select * from Ibatis where id=#id# and name=#name#
  </select> 
  
   <!-- 新增用户对象 -->
   <insert id="insertUsers" parameterClass="user" >
      insert into ibatis (id,name) values (#id#,#name#)
   </insert>
   
   <!-- 删除用户对象 -->
   <delete id="deleteUsers">
     delete from ibatis where id=#value#
   </delete>
   
   <!-- 更新用户对象 -->
   <delete id="updateUsers" parameterClass="user">
      update ibatis set name=#name# where id=#id#
   </delete>
</sqlMap>

Eq.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>
	<!-- 配置实体;eqinfo:相当于取了一个别名 -->
	<typeAlias type="com.tmall.bean.Eq" alias="eqinfo" />
	<!-- 设置resultMap -->
	<resultMap id="eqInfo" class="eqinfo">
		<result column="eq_id" property="eq_id" jdbcType="VARCHAR" />
		<result column="eq_name" property="eq_name" jdbcType="VARCHAR" />
	</resultMap>
	<!-- 根据id获得用户对象 -->
	<select id="getEqInfoById" resultMap="eqInfo">
		select eq_id,eq_name from eq where eq_id=#value#
	</select>
	<!-- 根据id或者名称获得用户对象 -->
	<select id="getEqInfoByIdAadName" resultMap="eqInfo"  parameterClass="java.util.Map">
		select eq_id,eq_name from eq 
		<!-- 动态添加sql -->
		<dynamic prepend="where">
		    <!-- 不为空就添加条件 -->
		    <isNotEmpty prepend="and" property="id">
		    	 eq_id=#id#
		    </isNotEmpty>
		    <isNotEmpty prepend="or" property="name">
		    	eq_name like '%$name$%'
		    </isNotEmpty>
		</dynamic>
	</select>
</sqlMap>

4、sql映射文件:sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE sqlMapConfig 
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" 
"http://www.ibatis.com/dtd/sql-map-config-2.dtd"> 
<sqlMapConfig> 
<!-- 配置实体xml文件路径,支持多个实体 -->
<sqlMap resource="Ibatis.xml" /> 
<sqlMap resource="Eq.xml" />  
</sqlMapConfig>

5、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.0.xsd">

<!-- 数据库jdbc配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName">
    <value>oracle.jdbc.OracleDriver</value>
  </property>
  <property name="username">
    <value>test</value>
  </property>
  <property name="password">
    <value>test123</value>
  </property>
  <property name="url">
    <value>jdbc:oracle:thin:@127.0.0.1:1521:test</value>
  </property>
</bean>
 <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
 <!-- org.springframework.orm.ibatis.SqlMapClientFactoryBean spring文件 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation">
  	<!-- sqlMap配置文件路径 -->
     <value>sqlMapConfig.xml</value>
  </property>

</bean>
 <!-- 注入javabean -->
<bean id="DaoImp" class="com.tmall.dao.DaoImp">
   <property name="dataSource">
   <ref bean="dataSource"/>
 </property>
  <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
  </property> 
</bean>

</beans>

6、javaBean  Eq、Ibatis

package com.tmall.bean;

public class Ibatis {
	private String id;
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 需要一个空的构造函数
	 */
	public Ibatis() {

	}

	public Ibatis(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
}


 

package com.tmall.bean;

public class Eq {
	private String eq_id;
	private String eq_name;

	public Eq() {
		super();
	}

	public Eq(String eq_id, String eq_name) {
		super();
		this.eq_id = eq_id;
		this.eq_name = eq_name;
	}

	public String getEq_id() {
		return eq_id;
	}

	public void setEq_id(String eq_id) {
		this.eq_id = eq_id;
	}

	public String getEq_name() {
		return eq_name;
	}

	public void setEq_name(String eq_name) {
		this.eq_name = eq_name;
	}

	@Override
	public String toString() {
		return "Eq [eq_id=" + eq_id + ", eq_name=" + eq_name + "]";
	}

}


7、Dao、DaoImp

package com.tmall.dao;

import java.util.List;

import com.tmall.bean.Eq;
import com.tmall.bean.Ibatis;

public interface Dao {
	public List<Ibatis> getList();

	public Ibatis getByName(String name);

	public Ibatis getById(String id);

	public void insert(Ibatis ibatis);

	public void delete(String id);

	public void update(Ibatis ibatis);

	public Ibatis getByNameAndId(String name, String id);

	public Eq getEqInfoById(String id);

	public List<Eq> getEqList(String name, String id);

}


 

package com.tmall.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.tmall.bean.Eq;
import com.tmall.bean.Ibatis;

public class DaoImp extends SqlMapClientDaoSupport implements Dao {

	public void delete(String id) {
		getSqlMapClientTemplate().delete("deleteUsers", id);
	}

	public Ibatis getById(String id) {
		return (Ibatis) getSqlMapClientTemplate().queryForObject(
				"getUsersById", id);
	}

	public Ibatis getByName(String name) {

		return (Ibatis) getSqlMapClientTemplate().queryForObject(
				"getUsersByName", name);
	}

	@SuppressWarnings("unchecked")
	public List<Ibatis> getList() {
		return getSqlMapClientTemplate().queryForList("getAllUsers", null);
	}

	public void insert(Ibatis ibatis) {
		getSqlMapClientTemplate().insert("insertUsers", ibatis);
	}

	public void update(Ibatis ibatis) {
		getSqlMapClientTemplate().update("updateUsers", ibatis);
	}

	@Override
	public Ibatis getByNameAndId(String name, String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "1");
		map.put("id", "1");
		return (Ibatis) getSqlMapClientTemplate().queryForObject(
				"getUsersByIdAndName", map);
	}

	@Override
	public Eq getEqInfoById(String eq_id) {
		return (Eq) getSqlMapClientTemplate().queryForObject("getEqInfoById",
				eq_id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Eq> getEqList(String name, String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", name);
		map.put("id", id);
		return getSqlMapClientTemplate().queryForList("getEqInfoByIdAadName",
				map);
	}

}


8、好了完成以上操作,下面测试调用了

package com.tamll.test;

import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tmall.bean.Eq;
import com.tmall.bean.Ibatis;
import com.tmall.dao.DaoImp;

public class Test {
	public static void main(String[] args) {
		 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");  
		 DaoImp dao = (DaoImp)context.getBean("DaoImp");
		 Iterator<Ibatis> iterator =  dao.getList().iterator();
		 while(iterator.hasNext()){
			 Ibatis i = iterator.next();
			 System.out.println(i.getId()+i.getName());
		 }
		 Ibatis ibatis = dao.getByNameAndId("1", "1");
		 System.out.println(ibatis.getId()+ibatis.getName());
		 Eq eq = dao.getEqInfoById("test");
		 System.out.println(eq.getEq_name());
		 List<Eq> eqs = dao.getEqList("test","test");
		 Iterator<Eq> i = eqs.iterator();
		 while(i.hasNext()){
			 Eq e = i.next();
			 System.out.println(e.toString());
		 }
	}
}

以上简单学习之后整的一个测试,包含基本配置以及动态sql的配置。

ibatis的优缺点:
ibatis 是一种“半 自动化”的ORM实现,可以自定义返回结果。ibatis是一种半自动化的ORM, 需要手工编写sql ;hibernate不需要手工编写sql,完全由hibernate实现反射。

具体可以查阅一下:http://blog.sina.com.cn/s/blog_6d44cb28010113cw.html

你可能感兴趣的:(spring,ibatis)