待完善的DEMO……
1 sqlMap.properties
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:1521:orcl username=username password=password
2 导包,iBatis必要jar和odbc驱动,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> <!-- 引用properties文件 --> <properties resource="resources/SqlMap.properties" /> <settings useStatementNamespaces="true" /> <transactionManager type="JDBC"> <dataSource type="simple"> <property name="JDBC.Driver" value="${driver}" /> <property name="JDBC.ConnectionURL" value="${url}" /> <property name="JDBC.Username" value="${username}" /> <property name="JDBC.Password" value="${password}" /> </dataSource> </transactionManager> <sqlMap resource="dao/imp/EmpDaoSqlMap.xml" /> </sqlMapConfig>
2 实体Emp.java
package model; import java.sql.Date; public class Emp { private Integer id; private String name; private String job; private Integer mgr; private Date hireDate; private Double sal; private Double comm; // getters & setters }
3 表
create table TESTEMPBYDF ( ID NUMBER(7) primary key, NAME VARCHAR2(20), JOB VARCHAR2(10), MGR NUMBER(7), HIREDATE DATE, SAL NUMBER(8,2), COMM NUMBER(8,2) ); create sequence testempbydf_seq;
4 dao接口EmpDao.java
package dao; import java.util.List; import model.Emp; public interface EmpDao { // 新增记录 void addEmp(Emp e); // 删除记录 void deleteEmp(Integer id); // 修改记录 void updateEmp(Emp e); // 查询一条记录 Emp getEmp(Integer id); // 查询全部记录 List<Emp> getAllEmp(); }
5 util类SessionFactory.java
package util; import java.io.IOException; import java.io.Reader; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class SessionFactory { private static final SqlMapClient sqlMapClient; static { String resource = "resources/sqlMapConfig.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { System.out.println("sqlMapConfig.xml文件找不到"); e.printStackTrace(); } sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); } public static SqlMapClient getSqlMapclient() { return sqlMapClient; } }
6 dao实现类EmpDaoIbatisImp.java
package dao.imp; import java.sql.SQLException; import java.util.List; import util.SessionFactory; import com.ibatis.sqlmap.client.SqlMapClient; import model.Emp; import dao.EmpDao; public class EmpDaoIbatisImp implements EmpDao { private SqlMapClient client = SessionFactory.getSqlMapclient(); public void addEmp(Emp e) { try { client.insert("dao.imp.EmpDaoIbatisImp.add", e); } catch (SQLException e1) { e1.printStackTrace(); } } public void deleteEmp(Integer id) { try { client.delete("dao.imp.EmpDaoIbatisImp.delete", id); } catch (SQLException e) { e.printStackTrace(); } } public void updateEmp(Emp e) { try { client.update("dao.imp.EmpDaoIbatisImp.update", e); } catch (SQLException e1) { e1.printStackTrace(); } } public Emp getEmp(Integer id) { Emp e = new Emp(); try { e = (Emp) client.queryForObject("dao.imp.EmpDaoIbatisImp.get", id); } catch (SQLException e1) { e1.printStackTrace(); } return e; } @SuppressWarnings("unchecked") public List<Emp> getAllEmp() { List<Emp> emps = null; try { emps = client.queryForList("dao.imp.EmpDaoIbatisImp.getAll"); } catch (SQLException e) { e.printStackTrace(); } return emps; } }
7 类-表 操作定义配置文件EmpDaoSqlMap.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="dao.imp.EmpDaoIbatisImp"> <typeAlias alias="Emp" type="model.Emp" /> <resultMap id="employee" class="model.Emp"> <result property="id" column="id" /> <result property="name" column="name" /> <result property="job" column="job" /> <result property="mgr" column="mgr" /> <result property="hireDate" column="hireDate" /> <result property="sal" column="sal" /> <result property="comm" column="comm" /> </resultMap> <insert id="add" parameterClass="Emp"> insert into testempbydf values(testempbydf_seq.nextval, #name#, #job#, #mgr#, #hireDate#, #sal#, #comm#) </insert> <delete id="delete" parameterClass="java.lang.Integer"> delete from testempbydf where id=#id# </delete> <update id="update" parameterClass="Emp"> update testempbydf set name=#name#, job=#job#, mgr=#mgr#, hireDate=#hireDate#, sal=#sal#, comm=#comm# where id=#id# </update> <select id="get" parameterClass="java.lang.Integer" resultClass="Emp"> select * from testempbydf where id=#id# </select> <select id="getAll" resultClass="Emp"> select * from testempbydf </select> </sqlMap>
8 测试
package test; import java.sql.Date; import java.util.List; import model.Emp; import dao.imp.EmpDaoIbatisImp; public class Test { @org.junit.Test public void testAdd() { EmpDaoIbatisImp dao = new EmpDaoIbatisImp(); Emp e = new Emp(); e.setId(0); e.setName("test1"); e.setJob("salesman"); e.setMgr(577703); e.setHireDate(new Date(1000000000)); e.setSal(5000.0); e.setComm(200.0); dao.addEmp(e); } @org.junit.Test public void testDelete() { EmpDaoIbatisImp dao = new EmpDaoIbatisImp(); dao.deleteEmp(4); } @org.junit.Test public void testUpdate() { EmpDaoIbatisImp dao = new EmpDaoIbatisImp(); Emp e = new Emp(); e.setId(23); e.setName("test111"); e.setJob("mgr"); e.setMgr(777777); e.setHireDate(new Date(100000000000L)); e.setSal(6000.0); e.setComm(300.0); dao.updateEmp(e); } @org.junit.Test public void testGet() { EmpDaoIbatisImp dao = new EmpDaoIbatisImp(); Emp e = new Emp(); e = dao.getEmp(23); System.out.println(e.getName() + " " + e.getMgr() + " " + e.getComm()); } @org.junit.Test public void testGetAll() { EmpDaoIbatisImp dao = new EmpDaoIbatisImp(); List<Emp> emps = null; emps = dao.getAllEmp(); for (Emp e : emps) { System.out.println(e.getId() + " " + e.getName() + " " + e.getSal()); } } }