iBatis是一个ORM框架,是一种典型的交互式框架
下面构建一个使用iBatis框架的Java应用程序
系统环境
(1)语言:Java,JDK1.8
(2)数据库:MySQL5.7.11
(3)依赖JAR包:ibatis-2.3.3.720.jar、mysql-connector-java-5.1.38-bin.jar
(4)开发工具:Eclispe
项目结构
数据库表dept结构
POJO类
package gc.ibatis.model; /** * 2016-03-20 * @author Administrator * 部门信息POJO类 */ public class Dept { private Integer deptNo; private String deptName; private String deptLocation; public Integer getDeptNo() { return deptNo; } public void setDeptNo(Integer deptNo) { this.deptNo = deptNo; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public String getDeptLocation() { return deptLocation; } public void setDeptLocation(String deptLocation) { this.deptLocation = deptLocation; } @Override public String toString() { return "[" + "deptNo:" + deptNo + ",deptName:" + deptName + ",deptLocation:" + deptLocation + "]"; } }
JDBC配置文件
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/myTestDB username=root password=root
Dept.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 alias="Dept" type="gc.ibatis.model.Dept"></typeAlias> <select id="getAllDepts" resultClass="Dept"> SELECT * FROM DEPT </select> <insert id="addDept" parameterClass="Dept"> INSERT INTO DEPT(deptName, deptLocation) VALUES(#deptName#, #deptLocation#) <selectKey resultClass="int" keyProperty="deptNo"> select @@identity as inserted </selectKey> </insert> </sqlMap>
SqlMapConfig配置文件
<?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 resource="jdbc.properties"/> <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="gc/ibatis/model/Dept.xml" /> </sqlMapConfig>
DAO接口
package gc.ibatis.dao; import java.util.List; import gc.ibatis.model.Dept; /** * 2016-03-20 * @author Administrator * 部门信息DAO */ public interface DeptDao { /** * 查询所有的部门信息 * @return */ List<Dept> getAllDepts(); /** * 添加一个部门信息 * @param dept * @return */ boolean addDept(Dept dept); }
Impl类
package gc.ibatis.daoImpl; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import gc.ibatis.dao.DeptDao; import gc.ibatis.model.Dept; import gc.ibatis.util.IBatisSqlClientUtil; public class DeptDaoImpl implements DeptDao { @SuppressWarnings("unchecked") @Override public List<Dept> getAllDepts() { List<Dept> depts = new ArrayList<Dept>(); try { depts = IBatisSqlClientUtil.getSqlMapClient().queryForList("getAllDepts"); } catch (SQLException e) { e.printStackTrace(); } return depts; } @Override public boolean addDept(Dept dept) { boolean rs = false; try { if(IBatisSqlClientUtil.getSqlMapClient().insert("addDept", dept) != null) { rs = true; } } catch (SQLException e) { e.printStackTrace(); } return rs; } }
工具类
package gc.ibatis.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; /** * 2016-03-20 * @author Administrator * 获取SqlMapClient的工具类 */ public class IBatisSqlClientUtil { private static final String CONFIG_FILE_PATH = "gc/ibatis/model/resources/SqlMapConfig.xml"; private static Reader reader = null; static { try { reader = Resources.getResourceAsReader(CONFIG_FILE_PATH); } catch (IOException e) { e.printStackTrace(); } } public static SqlMapClient getSqlMapClient() { return SqlMapClientBuilder.buildSqlMapClient(reader); } }
JUnit4测试类
package gc.ibatis.test; import java.util.List; import org.junit.Assert; import org.junit.Test; import gc.ibatis.dao.DeptDao; import gc.ibatis.daoImpl.DeptDaoImpl; import gc.ibatis.model.Dept; /** * 2016-03-20 * @author Administrator * IBatis测试类 */ public class IBatisTest { private DeptDao deptDao = new DeptDaoImpl(); @Test public void test() { List<Dept> depts = deptDao.getAllDepts(); Assert.assertEquals(1, depts.size()); } @Test public void testAddDept() { Dept dept = new Dept(); dept.setDeptName("帮购"); dept.setDeptLocation("浙江温州"); Assert.assertEquals(true, deptDao.addDept(dept)); } }