什么是 MyBatis?
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索。MyBatis 可以使用简单的XML 或注解用于配置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
MyBatis下载:https://github.com/mybatis/mybatis-3/releases
Mybatis实例:
对一个Student表的CRUD操作
在Src目录下建一个mybatis的xml配置文件mybatis.xml
<?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> <properties resource="db.properties"></properties> <!-- 定义别名 --> <typeAliases> <!-- <typeAlias type="com.model.Student" alias="list"/> --> <package name="com.model"/><!-- 扫描 --> </typeAliases> <!-- mybatis的环境们 --> <environments default="test"> <environment id="test"> <!-- mybatis支持的事务: JDBC:数据源自己的事务管理 MANAGE(托管),不支持tomcat,一般用在大型的商业服务器,weblogic,jboss --> <transactionManager type="JDBC"> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="venus"></property> <property name="password" value="123"></property> </dataSource> </environment> </environments> <mappers> <mapper resource="com/dao/StudentMapper.xml"/> </mappers> </configuration>
定义Student mappers的StudentMapper.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"> <mapper namespace="com.dao.StudentMapper"> <resultMap type="Student" id="stu"> </resultMap> <!-- 查所有数据 --> <select id="list" resultType="student"> select * from student </select> <!-- 插入一条数据 --> <insert id="add" parameterType="student"> insert into student values(#{sno},#{sname},#{ssex},sysdate,#{sclass}) </insert> <!-- 修改一条数据 --> <update id="update" parameterType="student"> update student s set s.sname=#{sname},s.ssex=#{ssex} ,s.sbirthday=sysdate,s.sclass=#{sclass} where s.sno=#{sno} </update> <!-- 删除一条数据 --> <delete id="del" parameterType="String"> delete student s where s.sno=#{sno} </delete> <!-- 查符合条件的数据 --> <select id="show" parameterType="Map" resultMap="stu"> select * from student s where s.sno like #{p1} and s.sname like #{p2} </select> </mapper>
配置文件实现了接口和SQL语句的映射关系。selectUsersByName采用了2种方式实现,注释掉的也是一种实现,采用resultMap可以把属性和数据库列名映射关系定义好,property为类的属性,column是表的列名,也可以是表列名的别名!
Student类的定义(可以用Hibernate自动生成Student实体类):
package com.model; // Generated 2017-4-19 11:51:47 by Hibernate Tools 5.2.0.CR1 import java.util.Date; /** * Student generated by hbm2java */ public class Student { private String sno; private String sname; private String ssex; private Date sbirthday; private String sclass; public Student() { } public Student(String sno, String sname, String ssex) { this.sno = sno; this.sname = sname; this.ssex = ssex; } public Student(String sno, String sname, String ssex, Date sbirthday, String sclass) { this.sno = sno; this.sname = sname; this.ssex = ssex; this.sbirthday = sbirthday; this.sclass = sclass; } public String getSno() { return this.sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return this.sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return this.ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public Date getSbirthday() { return this.sbirthday; } public void setSbirthday(Date sbirthday) { this.sbirthday = sbirthday; } public String getSclass() { return this.sclass; } public void setSclass(String sclass) { this.sclass = sclass; } }
为了方便写一个工具类 拿出session:
package com.util; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtil { private static SqlSessionFactory ssf; private static SqlSession ss; public static SqlSession getSqlSession(){ InputStream in = null; try { in = Resources.getResourceAsStream("mybatis.xml"); ssf = new SqlSessionFactoryBuilder().build(in); ss = ssf.openSession(); in.close(); } catch (IOException e) { e.printStackTrace(); } return ss; } }
测试类UserDao:
package com.util; import static org.junit.Assert.*; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.crypto.Data; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.model.Student; public class JUnit { private SqlSession ss; @Before public void setUp() throws Exception { ss = MybatisUtil.getSqlSession(); } @After public void tearDown() throws Exception { ss.commit(); ss.close(); } //查所有数据 public void test() { List<Student> list = ss.selectList("com.dao.StudentMapper.list"); for(Student data : list){ System.out.println(data.getSname()); } } //增加一条数据 public void addTest(){ Student stu = new Student(); stu.setSno("120"); stu.setSname("张自忠"); stu.setSsex("男"); stu.setSclass("123"); ss.insert("com.dao.StudentMapper.add",stu); } //修改一条数据 public void updateTest(){ Student stu = new Student(); stu.setSno("120"); stu.setSname("张自恋"); stu.setSsex("男"); stu.setSclass("123"); ss.update("com.dao.StudentMapper.update",stu); } //删除一条数据 public void delTest(){ ss.delete("com.dao.StudentMapper.del","120"); } //查符合条件的数据 @Test public void show(){ Map<String, Object> map = new HashMap<String, Object>(); map.put("p1", "%7"); map.put("p2", "王%"); List<Student> list = ss.selectList("com.dao.StudentMapper.show",map); for(Student data : list){ System.out.println(data.getSname()); } } }
PS:所用到的jar包就两个 一个是mybatis-3.4.4.jar 另外一个是数据库驱动包:ojdbc6.jar