MyBatis是一個支援普通SQL查詢,儲存過程和高階對映的優秀持久層框架。MyBatis消除了幾乎所有的JDBC程式碼和引數的手工設定以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或註解用於配置和原始對映,將介面和Java的POJO(Plain Old Java Objects,普通的Java物件)對映成資料庫中的記錄。
框架整體結構圖:
關鍵程式碼介紹
1.ICatagoryOperation介面
public interface ICatagoryOperation { void inserteCatagory(Catagory catagory); Catagory getCatagorybyID(int id); }
2.Catagory類
public class Catagory { private int id; private String name; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return String.format("id:%d,name:%s,description:%s", id, name, description); } }
3.MyBatisUtils類
public class MyBatisUtils { private static final String CONFIG_PATH = "config/mybatis_config.xml"; /* * 獲取資料庫訪問連結 */ public static SqlSession getSqlSession() { SqlSession session = null; try { InputStream stream = Resources.getResourceAsStream(CONFIG_PATH); // 可以根據配置的相應環境讀取相應的資料庫環境 // SqlSessionFactory factory = new SqlSessionFactoryBuilder().build( // stream, "development"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream); session = factory.openSession(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return session; } /* * 獲取資料庫訪問連結 */ public static void closeSession(SqlSession session) { session.close(); } }
4.CatagoryMapper.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.franson.study.interfaces.ICatagoryOperation"> <!-- 此處的resultType就是對應剛剛你在typeAlias節點裡面規定的別名 --> <select id="inserteCatagory" parameterType="Catagory"> insert into tb_catagories(name,description) values(#{name},#{description}) </select> <select id="getCatagorybyID" resultType="Catagory" parameterType="java.lang.Integer"> select * from tb_catagories where id=#{id} </select> </mapper>
5.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"> <configuration> <typeAliases> <typeAlias type="com.franson.study.model.Catagory" alias="Catagory" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <!-- ?useUnicode=true&characterEncoding=utf8為了支援中文資料的寫入 --> <property name="url" value="jdbc:mysql://127.0.0.1:3306/db_catagory?useUnicode=true&characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/CatagoryMapper.xml" /> </mappers> </configuration>
6.TestMybatis測試入口類
public class TestMybatis { public static void main(String[] args) { addTest(); getTest(); } /* * 新增Catagory */ static void addTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession(); ICatagoryOperation catagoryOperation = sqlSession.getMapper(ICatagoryOperation.class); Catagory item = new Catagory(); item.setName("sql server"); item.setDescription("sql server類"); int result = catagoryOperation.inserteCatagory(item); if (result > 0) { sqlSession.commit(); System.out.println("新增成功"); } else sqlSession.rollback(); MyBatisUtils.closeSession(sqlSession); } /* * 根據ID獲取Catagory */ static void getTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession(); ICatagoryOperation catagoryOperation = sqlSession.getMapper(ICatagoryOperation.class); System.out.println(catagoryOperation.getCatagorybyID(3).toString()); MyBatisUtils.closeSession(sqlSession); } }
7.測試執行結果