MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
iBatis是半ORM映射框架,它需要在数据库里手动建表,CURD操作时要自己写SQL语句,而Hibernate是全ORM映射框架,它只需要配置好文件,表会自动生成,CURD的SQL语句也是自动生成的,这是他们的主要区别。
搭建MyBatis 首先需要引入jar包
搭建小配置文件
<?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"> <!-- namespace:隔离同名类 --> <mapper namespace="cn.happy.entity.Dept">
其次是搭建大配置文件,并与小配置相关联
<?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> <environments default="development"> <environment id="development"> <!-- tx use jdbc's --> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" ></property> <property name="username" value="****" /> <property name="password" value="****" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/happy/entity/Dept.xml" /> <mapper resource="cn/happy/entity/getMapper.xml" /> </mappers> </configuration>
创建实体类,并封装
package cn.happy.entity; public class Dept { private Integer deptno;//部门编号 private String deptname;//部门名称 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; } }
最后书写测试类
小配置:
<select id="selectDept" resultType="cn.happy.entity.Dept"> <!-- SQL 列名,表名和数据库表 不区分大小写 记录是区分的: oa OA --> select * from Y2162Dept </select>
测试类:
static SqlSession session; @Before public void initData() throws Exception{ SqlSessionFactoryBuilder sb=new SqlSessionFactoryBuilder(); Reader reader=new FileReader("src/mybatis-config.xml"); SqlSessionFactory factory = sb.build(reader); session = factory.openSession(); } @Test //检索所有部门名称 public void testSelectDept() throws Exception{ List<Dept> list = session.selectList("selectDept"); for (Dept dept : list) { System.out.println(dept.getDeptname()); } session.close(); }
运行结果:
小配置:
<insert id="insertDept" parameterType="cn.happy.entity.Dept"> //添加数据 insert into Y2162Dept values(#{deptno},#{deptname}) </insert> <update id="updateDeptById" parameterType="cn.happy.entity.Dept"> //按部门编号修改部门名称 update Y2162Dept set deptname=#{deptname} where deptno=#{deptno} </update> <delete id="deleteDeptById" parameterType="cn.happy.entity.Dept"> //按部门编号删除部门 delete from Y2162Dept where deptno=#{deptno} </delete>
测试类:
//增加 @Test public void testInsertDept() throws Exception{ Dept dt=new Dept(); dt.setDeptno(22); dt.setDeptname("月饼部"); int count = session.insert("insertDept", dt); session.commit(); System.out.println(count); session.close(); } //修改 @Test public void testupdateDeptById() throws Exception{ Dept dt=new Dept(); dt.setDeptno(101); dt.setDeptname("月饼不"); int count = session.update("updateDeptById",dt); session.commit(); System.out.println(count); session.close(); } //删除 @Test public void testdeleteDeptById() throws Exception{ Dept dt=new Dept(); dt.setDeptno(101); int count = session.delete("deleteDeptById",dt); session.commit(); System.out.println(count); session.close(); }
还可以通过getMapper()方法查询数据
创建一个接口IDeptDao
package cn.happy.dao; import java.util.List; import cn.happy.entity.Dept; public interface IDeptDao { public List<Dept> getAllDepts(); }
创建getMapper.xml配置文件
<?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"> <!-- namespace:隔离同名类 --> <mapper namespace="cn.happy.dao.IDeptDao"> <select id="getAllDepts" resultType="cn.happy.entity.Dept"> select * from Y2162Dept </select> </mapper>
测试类代码:
//使用getMapper()检索所有数据 @Test public void testgetAllDepts(){ IDeptDao mapper = session.getMapper(IDeptDao.class); List<Dept> list=mapper.getAllDepts(); for (Dept dept : list) { System.out.println(dept.getDeptname()); } }
运行结果: