MyBatis介绍
MyBatis的前身就是iBatis,iBatis本是apache的一个开源项目,2010年这个项目由apahce sofeware foundation 迁移到了google code,并且改名为MyBatis。
MyBatis优缺点
优点:简单、灵活、高效
缺点:移植性差
开发环境搭建
2、部署MyBatis jar包
3、编写MyBatis配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="Emp" type="com.accp.entity.Emp"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/accp/uitl/EmpDaoMapper.xml"/> </mappers> </configuration>
4、创建持久化类和数据库接口
1 package com.accp.entity; 2 3 import java.util.Date; 4 5 6 public class Emp implements java.io.Serializable { 7 8 // Fields 9 10 private int empno; 11 private String ename; 12 private String job; 13 private int mgr; 14 private Date hiredate; 15 private Double sal; 16 private Double comm; 17 private int deptno; 18 // Constructors 19 /** default constructor */ 20 public Emp() { 21 } 22 23 public Emp(int empno, String ename, String job, int mgr, Date hiredate, 24 Double sal, Double comm, int deptno) { 25 super(); 26 this.empno = empno; 27 this.ename = ename; 28 this.job = job; 29 this.mgr = mgr; 30 this.hiredate = hiredate; 31 this.sal = sal; 32 this.comm = comm; 33 this.deptno = deptno; 34 } 35 36 public int getEmpno() { 37 return empno; 38 } 39 public void setEmpno(int empno) { 40 this.empno = empno; 41 } 42 public String getEname() { 43 return ename; 44 } 45 public void setEname(String ename) { 46 this.ename = ename; 47 } 48 public String getJob() { 49 return job; 50 } 51 public void setJob(String job) { 52 this.job = job; 53 } 54 public int getMgr() { 55 return mgr; 56 } 57 public void setMgr(int mgr) { 58 this.mgr = mgr; 59 } 60 public Date getHiredate() { 61 return hiredate; 62 } 63 public void setHiredate(Date hiredate) { 64 this.hiredate = hiredate; 65 } 66 public Double getSal() { 67 return sal; 68 } 69 public void setSal(Double sal) { 70 this.sal = sal; 71 } 72 public Double getComm() { 73 return comm; 74 } 75 public void setComm(Double comm) { 76 this.comm = comm; 77 } 78 public int getDeptno() { 79 return deptno; 80 } 81 public void setDeptno(int deptno) { 82 this.deptno = deptno; 83 } 84 85 86 87 88 }
1 package com.accp.dao; 2 3 import com.accp.entity.Emp; 4 5 public interface IEmpDao { 6 public void insertEmp(Emp emp); 7 public void updateEmp(Emp emp); 8 }
5、创建SQL映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.accp.dao.IEmpDao"> <insert id="insertEmp" parameterType="Emp" > INSERT INTO emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)VALUES(#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno}) </insert> <update id="updateEmp" parameterType="Emp"> UPDATE emp SET ename=#{ename},job=#{job},mgr=#{mgr},sal=#{sal} WHERE empno=#{empno} </update> </mapper>
6、创建书库接口和实现类
1 package com.accp.daoImpl; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 import com.accp.dao.IEmpDao; 12 import com.accp.entity.Emp; 13 /** 14 * @author 孙洪雨 15 */ 16 public class IEmpDaoImpl implements IEmpDao { 17 SqlSession session=null; 18 SqlSessionFactory sf=null; 19 Reader reader=null; 20 @Override 21 public void insertEmp(Emp emp) { 22 try { 23 String resource="mybatis-config.xml"; 24 reader=Resources.getResourceAsReader(resource); 25 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); 26 sf=builder.build(reader); 27 session=sf.openSession(); 28 IEmpDao dao=session.getMapper(IEmpDao.class); 29 dao.insertEmp(emp); 30 session.commit(); 31 } catch (IOException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 }finally{ 35 if(session!=null){ 36 session.close(); 37 } 38 } 39 40 } 41 @Override 42 public void updateEmp(Emp emp) { 43 try { 44 String resource="mybatis-config.xml"; 45 reader=Resources.getResourceAsReader(resource); 46 SqlSessionFactoryBuilder bulder=new SqlSessionFactoryBuilder(); 47 sf=bulder.build(reader); 48 session=sf.openSession(); 49 IEmpDao dao=session.getMapper(IEmpDao.class); 50 dao.updateEmp(emp); 51 session.commit(); 52 } catch (IOException e) { 53 // TODO Auto-generated catch block 54 e.printStackTrace(); 55 }finally{ 56 if(session!=null){ 57 session.close(); 58 } 59 } 60 } 61 62 }
7、编写测试类进行测试
使用MyBatis实现数据表的查询
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.accp.dao.IEmpDao"> <select id="findEmpByEmpNo" parameterType="int" resultType="com.accp.entity.Emp" > SELECT empno,ename,job,mgr,hiredate,sal,comm from Emp where empno= #{empno} </select> </mapper>
select元素有如下设置:
1.使用selectOne方法
1 package com.accp.daoImpl; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 import com.accp.dao.IEmpDao; 12 import com.accp.entity.Emp; 13 14 public class IEmpDaoImpl implements IEmpDao { 15 SqlSessionFactory sf=null; 16 SqlSession session=null; 17 Reader reader=null; 18 @Override 19 public Emp findEmpByEmpNo(int empno) { 20 String resource="mybatis-config.xml"; 21 Emp emp=null; 22 try { 23 reader=Resources.getResourceAsReader(resource); 24 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); 25 sf=builder.build(reader); 26 session=sf.openSession(); 27 emp=session.selectOne("com.accp.dao.IEmpDao.findEmpByEmpNo", empno); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 return emp; 33 } 34 35 }
2.session.getMapper()方法实现
1 package com.accp.daoImpl; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 import com.accp.dao.IEmpDao; 12 import com.accp.entity.Emp; 13 14 public class IEmpDaoImpl implements IEmpDao { 15 SqlSessionFactory sf=null; 16 SqlSession session=null; 17 Reader reader=null; 18 @Override 19 public Emp findEmpByEmpNo(int empno) { 20 String resource="mybatis-config.xml"; 21 Emp emp=null; 22 try { 23 reader=Resources.getResourceAsReader(resource); 24 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); 25 sf=builder.build(reader); 26 session=sf.openSession(); 27 IEmpDao dao=session.getMapper(IEmpDao.class); 28 emp=dao.findEmpByEmpNo(empno); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 return emp; 34 } 35 36 }
使用MyBatis实现结果映射
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.accp.dao.IEmpDao"> <resultMap type="com.accp.entity.Emp" id="ResultMap"> <id property="empno" column="empno"/> <result property="ename" column="ename"/> <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> <select id="empResultMap" resultMap="ResultMap"> select * from EMP </select> </mapper>
使用MyBatis实现数据表增删改操作
MyBatis的官方网站是https://code.google.com/p/mybatis/