Mybatis,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(); } }}