最近研究了一下Spring + iBATIS。发现看别人的例子是一回事,自己写一个完整的应用又是另外一回事。自己受够了网上贴的一知半解的代码。
iBATIS是一个持久化框架,封面了sql过程,虽然sql语句需要自己写。另外,我觉得对于初学者来说,完整的示例真的很重要,不然不知道文件是如果放置的。
所有的第三包都需要加上,spring、ibatis.-2.3.3.720.jar、sqlijdbc.jar、oscache-2.4.jar、commons-pool-1.3.jar、commons-dbcp-1.4.jar,mysql-connector-5.0.5.jar。不然运行的时候都要报错。
xml文件的位置也很关键,因为访问配置文件需要确定他的路径。
有三个包:
1.bean。
bean包封装了POJO对象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;
- }
- }
2. Dao包
Dao里面有一个Dao和DaoImp。Dao是一个接口,DaoImp实现了Dao的接口。
Dao.java如下:
- package com.tmall.dao;
- import java.util.List;
- 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);
- }
DaoImp.java如下
- package com.tmall.dao;
- import java.util.List;
- import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
- 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);
- }
- }
除此之外,关键的问题还有三个配置文件。applicationcontext.xml、ibatis.xml、sqlMapConfig.xml。
applicationContext.xml文件是spring的配置文件,如下:
- <?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">
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>123456</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://localhost:3306/test</value>
- </property>
- </bean>
- <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
- <property name="configLocation">
- <value>sqlMapConfig.xml</value>
- </property>
- </bean>
- <bean id="DaoImp" class="com.tmall.dao.DaoImp">
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- <property name="sqlMapClient">
- <ref bean="sqlMapClient"/>
- </property>
- </bean>
- </beans>
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 >
- <typeAlias type="com.tmall.bean.Ibatis" alias="user"/>
- <resultMap id="ibatisTest" class="user" >
- <result column="id" property="id" jdbcType="VARCHAR" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </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>
- <!-- 新增用户对象 -->
- <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>
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>
- <sqlMap resource="Ibatis.xml" />
- </sqlMapConfig>
测试语句
- package com.tmall.test;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.tmall.bean.Ibatis;
- import com.tmall.dao.Dao;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- Dao dao = (Dao)context.getBean("DaoImp");
- dao.insert(new Ibatis("3","new3"));
- Ibatis ibatis3 = dao.getById("2");
- ibatis3.setName("new7");
- dao.update(ibatis3);
- // testDAOImpl.delete("3");
- System.out.println("获得全查询列表");
- List<Ibatis> result=new ArrayList<Ibatis>();
- result = dao.getList();
- for (Iterator<Ibatis> iter = result.iterator(); iter.hasNext();) {
- Ibatis element = (Ibatis) iter.next();
- System.out.println(element.getName());
- }
- }
- }
别忘了在mysql语句中建立一个表,然后通过mysql query这个工具来看结果。