首先建立好数据库,序列和引擎,实现主键的自动增长,这个内容我已经在我的一篇文章中实现,这里我就不在重复。
下面来说一下我整合spring和Ibatis的过程
1、创建实体类,和数据库表的字段对应 (注意这里我写了一个无参的构造函数)
public class User {
private BigDecimal id;
private String user_name;
private String password;
/**
* 注意这里写一个无参的构造函数,因为后面我遇到了一个错误,所以这里必须留着
*/
public User() {
super();
}
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public User( String user_name, String password) {
this.user_name = user_name;
this.password = password;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public interface IDAO {
public void insertToTB(User user);
public List getList();
public User getById(BigDecimal id);
public void update(User user);
public void delete(BigDecimal id);
}
3、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 namespace="User_oracle">
<typeAlias alias="user" type="com.spring.ibatis.User" />
<resultMap class="user" id="ibatisTest">
<result property="id" column="id" jdbcType="DECIMAL" />
<result property="user_name" column="user_name" jdbcType="VARCHAR2" />
<result property="password" column="password" jdbcType="VARCHAR2" />
</resultMap>
<insert id="insertToTB">
insert into
td(id,user_name,password)values(#id#,#user_name#,#password#)
</insert>
<select id="getAllUsers" resultMap="ibatisTest">
select * from td
</select>
<!-- 根据id获得用户对象 -->
<select id="getUserById" resultMap="ibatisTest">
select *from td where
id=#value#
</select>
<!-- 更新用户对象 parameterClass是你要传进来的参数的类型
resultMap也是你要返回的值类型(一般是对象) -->
<update id="updateUsers" parameterClass="user">
update td set
user_name=#user_name# where id=#id#
</update>
<!-- 删除用户对象 -->
<delete id="deleteUsers">
delete from td where id=#value#
</delete>
</sqlMap>
4、sqlmap-config.xml配置文件
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="ibatis.xml" />
</sqlMapConfig>
5、spring配置文件applicationContext.xml
?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/conte xt
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- datasource for oracle -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<!-- 此处赢注入ibatis的配置文件 -->
<property name="configLocation" value="classpath:sqlmap-config.xml" />
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="iDAOImpl" class="com.spring.ibatis.IDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
</beans>
6、DAO实现类
public class IDAOImpl extends SqlMapClientDaoSupport implements IDAO{
/**
* 向数据库中插入数据
*/
@Override
public void insertToTB(User user) {
getSqlMapClientTemplate().insert("insertToTB", user);
}
/**
* 获取所有用户
*/
@Override
public List getList() {
return getSqlMapClientTemplate().queryForList("getAllUsers", null);
}
/**
* 通过id获取用户
*/
@Override
public User getById(BigDecimal id) {
return (User) getSqlMapClientTemplate().queryForObject("getUserById",id);
}
/**
* 更新用户
*/
@Override
public void update(User user) {
getSqlMapClientTemplate().update("updateUsers", user);
}
/**
* 删除用户
*/
@Override
public void delete(BigDecimal id) {
getSqlMapClientTemplate().delete("deleteUsers", id);
}
}
7、测试代码
public class batisTest {
@Test
public void test() {
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
IDAO idaoImpl=(IDAO) ctx.getBean("iDAOImpl");
/**
* 向数据库中插入数据
*/
idaoImpl.insertToTB(new User("cw","1235"));
/**
* System.out.println("获得全部查询列表");
*/
List result=new ArrayList();
result=idaoImpl.getList();
for(Iterator iter=result.iterator();iter.hasNext();){
User u=(User) iter.next();
System.out.println("name:"+u.getUser_name()+" id:"+u.getId()+" password:"+u.getPassword());
}
/**
* 通过id获得用户对象
*/
User u=idaoImpl.getById(new BigDecimal(22));
System.out.println("name:"+u.getUser_name()+" id:"+u.getId()+" password:"+u.getPassword());
/**
* 更新用户对象
*/
System.out.println("更新用户对象前");
List result1=new ArrayList();
result1=idaoImpl.getList();
for(Iterator iter=result1.iterator();iter.hasNext();){
User u1=(User) iter.next();
System.out.println("name:"+u1.getUser_name()+" id:"+u1.getId()+" password:"+u1.getPassword());
}
User u3=idaoImpl.getById(new BigDecimal(22));
u3.setUser_name("today");
idaoImpl.update(u3);
System.out.println("更新用户对象后");
List result2=new ArrayList();
result2=idaoImpl.getList();
for(Iterator iter=result2.iterator();iter.hasNext();){
User u4=(User) iter.next();
System.out.println("name:"+u4.getUser_name()+" id:"+u4.getId()+" password:"+u4.getPassword());
}
/**
* 删除用户
*/
idaoImpl.delete(new BigDecimal(21));
}
}
之前我是直接在spring中实现了oracle数据库的增、删、查、改,这里我通过学习Spring和Ibatis的整合,也实现了另一种
方式的oracel数据库的增、删、查、改