iBatis学习(一)

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

  • 项目结构

141158_pVrV_2679804.png

  • 数据库表dept结构

140938_SdNl_2679804.png

  • 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配置文件

<?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));
 }
}

转载于:https://my.oschina.net/migoo/blog/644000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值