Mybatis
一.Mybatis介绍
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
二.Mybatis jar包
需要准备的引用包
mybatis-3.2.8.jar:myBatis框架使用
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
ojdbc6.jar:数据库连接
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>5.1.36</version>
</dependency>
三.结构分析
1)需要配置文件连接数据库;
2)需要公共方法提供数据库连接的使用;
3)PO:持久对象
4)数据查询SQL配置XML文件;
5)对外接口;
四.结构搭建
示例如下:
IStudentInfoDAO.java
package cn.happy.dao; import cn.happy.entity.StudentInfo; import java.util.List; /** * Created by Happy on 2017-07-09. */ public interface IStudentInfoDAO { //01.查询所有的学生信息 public List<StudentInfo> findAll(); //02.根据学生学号查询特定学生对象 public StudentInfo getStudentById(int stuid); }
IStudentInfoDAO.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: 命名空间:区分不同空间下的同名SQLID A: findlAll B: findAll --> <mapper namespace="cn.happy.dao.IStudentInfoDAO"> <!--SQL标签 id:唯一锁定到SQL标识 paramenterType:SQL语句的入参 可以省略 resultType: 增删除操作:不能 写 查询:单个实体的类型 --> <select id="findAll" resultType="StudentInfo"> /*SQL文:SQL语句*/ select * from studentinfo </select> <!--按主键查询--> <select id="getStudentById" resultType="StudentInfo"> select * from studentinfo WHERE stuid=#{stuId} </select> </mapper>
StudentInfo.java
package cn.happy.entity; import java.util.Date; /** * Created by Happy on 2017-07-09. */ public class StudentInfo { private Integer stuId; private String stuName; private Integer stuAge; private Date stuDate; public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public Integer getStuAge() { return stuAge; } public void setStuAge(Integer stuAge) { this.stuAge = stuAge; } public Date getStuDate() { return stuDate; } public void setStuDate(Date stuDate) { this.stuDate = stuDate; } }
MyBatis-config.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"> <!--根节点,XMl只能有一个--> <configuration> <typeAliases> <!--<typeAlias type="cn.happy.entity.StudentInfo" alias="StudentInfo"></typeAlias>--> <!--将该包中的简单类型 StudentInfo作为类的别名--> <package name="cn.happy.entity"></package> </typeAliases> <!--一个environments有N个environment--> <environments default="development"> <environment id="development"> <!-- transactionManager:JDBC保证事务的 update delete 事务分类:JDBC:编程式事务 xxx.beginTransaction() tx.commit() tx.rollback() 配置式事务 JDBC|MANAGED 区别 --> <transactionManager type="JDBC"/> <!-- POOLED:MyBatis内置的连接池 c3p0连接池 POOLED 、UNPOOLED 、JNDI --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/y2165"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/happy/dao/IStudentInfoDAO.xml"/> </mappers> </configuration>
MyBatisTest0709.java
package cn.happy.test; import cn.happy.dao.IStudentInfoDAO; import cn.happy.entity.StudentInfo; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * Created by Happy on 2017-07-09. */ public class MyBatisTest0709 { @Test //03.根据主键查询单个对象 getMapper()方式 public void testSelectOneByGetMapper(){ String path="MyBatis-config.xml"; InputStream is= null; try { is = Resources.getResourceAsStream(path); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession(); IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class); StudentInfo info = dao.getStudentById(3); System.out.println(info.getStuName()); /*MyBatisTest0709 my=new MyBatisTest0709(); Class<MyBatisTest0709> clazz = (Class<MyBatisTest0709>) my.getClass(); try { Class<MyBatisTest0709> clazzz = (Class<MyBatisTest0709>)Class.forName("cn.happy.test.MyBatisTest0709"); } catch (ClassNotFoundException e) { e.printStackTrace(); }*/ session.close(); } catch (IOException e) { e.printStackTrace(); } } @Test //02.根据主键查询单个对象 public void testSelectOne(){ String path="MyBatis-config.xml"; InputStream is= null; try { is = Resources.getResourceAsStream(path); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession(); StudentInfo info = session.selectOne("getStudentById",3); System.out.println(info.getStuName()); session.close(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testAll(){ String path="MyBatis-config.xml"; InputStream is= null; try { is = Resources.getResourceAsStream(path); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession(); List<StudentInfo> list = session.selectList("findAll"); for (StudentInfo info:list) { System.out.println(info.getStuName()); } session.close(); } catch (IOException e) { e.printStackTrace(); } } }