摘要:首先導(dǎo)包依賴如下構(gòu)建應(yīng)用是以為中心的實例可以通過獲得其中是工廠接口任務(wù)用于創(chuàng)建配置文件將會解析配置文件在類對象中配置獲取數(shù)據(jù)源事務(wù)管理器映射器在文件下新建文件配置文件內(nèi)容如下定義別名定義數(shù)據(jù)庫信息事物管理
首先導(dǎo)包
依賴如下
構(gòu)建SqlSessionFactorymysql mysql-connector-java 8.0.15 org.mybatis mybatis 3.5.1 junit junit 4.11 test
MyBatis應(yīng)用是以SqlSessionFactory為中心的,實例可以通過SqlSessionFactoryBuilder獲得.
其中SqlSessionFactory是工廠接口,任務(wù)用于創(chuàng)建SqlSession
配置文件將會解析配置XML文件在Configuration類對象中.
在resource文件下新建mybatis-config.xml文件
配置文件內(nèi)容如下
新建類
package com.ming; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class Role { public String getSqlSessionFactory(){ String resource = "mybatis-config.xml"; try {// 獲得輸入流 InputStream inputStream; inputStream = Resources.getResourceAsStream(resource); // 獲得SqlSessionFactory工廠 SqlSessionFactory sqlSessionFactory = null; sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch (IOException e){ e.printStackTrace(); } return null; } }
最后新建測試類
package com.ming; import org.junit.After; import org.junit.Before; import org.junit.Test; public class RoleTest { private Role role = null; @Before public void setUp() throws Exception { this.role = new Role(); } @After public void tearDown() throws Exception { } @Test public void getSqlSessionFactory() { role.getSqlSessionFactory(); } }
此時的目錄結(jié)構(gòu)
public SqlSessionFactory getSqlSessionFactory1(){ // 創(chuàng)建數(shù)據(jù)庫連接池 PooledDataSource dataSource = new PooledDataSource(); dataSource.setDriver("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://47.94.95.84:32786/mybatis"); dataSource.setUsername("mybatis"); dataSource.setPassword("ABCcba20170607"); // 構(gòu)建數(shù)據(jù)庫事物 TransactionFactory transactionFactory = new JdbcTransactionFactory(); // 創(chuàng)建數(shù)據(jù)庫環(huán)境 Environment environment = new Environment("development", transactionFactory, dataSource); // 構(gòu)建Configuration對象 Configuration configuration = new Configuration(environment); // 注冊上下文別名 configuration.getTypeAliasRegistry().registerAlias("role", Role.class); // 創(chuàng)建映射器 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); return sqlSessionFactory; }
書寫測試用例如下
package com.ming; import org.junit.After; import org.junit.Before; import org.junit.Test; public class RoleTest { private Role role = null; @Before public void setUp() throws Exception { this.role = new Role(); } @After public void tearDown() throws Exception { } @Test public void getSqlSessionFactory() { role.getSqlSessionFactory(); } @Test public void getSqlSessionFactory1() { role.getSqlSessionFactory1(); } }創(chuàng)建SqlSession
SqlSession屬于門面,通過SqlSession可以獲得需要的什么信息
SqlSession sqlSession = null; try{ // 創(chuàng)建一個sqlsession會話 sqlSession = sqlSessionFactory.openSession(); sqlSession.commit(); }catch (Exception e){ // 輸出錯誤信息 System.out.println(e.getMessage()); // 進(jìn)行事物操作,回滾數(shù)據(jù)庫數(shù)據(jù) sqlSession.rollback(); }finally { // 進(jìn)行資源關(guān)閉 if(sqlSession != null){ sqlSession.close(); } }
SqlSession作用,獲取映射器,通過命名信息執(zhí)行sql結(jié)果
映射器映射器由java和xml文件共同組成,作用
定義參數(shù)類型
描述緩存
描述sql
定義查詢結(jié)果和POJO映射關(guān)系
先給出java接口
package com.ming; public interface RoleMapper { public Role getRole(Long id); }
根據(jù)給定的id獲取角色對象
給出映射文件,然后在生成的時候會根據(jù)接口實現(xiàn)類,生成對象.
先編寫POJO
package com.ming; // POJO public class Role { private int id; private String roleName; private String note; public void setId(int id) { this.id = id; } public void setRoleName(String roleName) { this.roleName = roleName; } public void setNote(String note) { this.note = note; } public int getId() { return id; } public String getRoleName() { return roleName; } public String getNote() { return note; } }
在編寫映射配置文件
最后編寫mybatis,添加映射器配置文件
最后再編寫執(zhí)行sql的類
package com.ming; import org.apache.ibatis.datasource.pooled.PooledDataSource; import org.apache.ibatis.io.Resources; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import java.io.IOException; import java.io.InputStream; public class MyBatis { public String getSqlSessionFactory(){ String resource = "mybatis-config.xml"; SqlSessionFactory sqlSessionFactory = null; try {// 獲得輸入流 InputStream inputStream; inputStream = Resources.getResourceAsStream(resource); // 獲得SqlSessionFactory工廠 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch (IOException e){ e.printStackTrace(); } SqlSession sqlSession = null; try{ // 創(chuàng)建一個sqlsession會話 sqlSession = sqlSessionFactory.openSession(); // 獲得映射器 // 接口傳入映射器中 RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class); // 執(zhí)行方法 Role role = roleMapper.getRole(0); System.out.println(role.getRoleName()); // 刷新語句并提交鏈接 sqlSession.commit(); }catch (Exception e){ // 輸出錯誤信息 System.out.println(e.getMessage()); // 進(jìn)行事物操作,回滾數(shù)據(jù)庫數(shù)據(jù) sqlSession.rollback(); }finally { // 進(jìn)行資源關(guān)閉 if(sqlSession != null){ sqlSession.close(); } } return null; } public SqlSessionFactory getSqlSessionFactory1(){ // 創(chuàng)建數(shù)據(jù)庫連接池 PooledDataSource dataSource = new PooledDataSource(); dataSource.setDriver("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://47.94.95.84:32786/mybatis"); dataSource.setUsername("mybatis"); dataSource.setPassword("ABCcba20170607"); // 構(gòu)建數(shù)據(jù)庫事物 TransactionFactory transactionFactory = new JdbcTransactionFactory(); // 創(chuàng)建數(shù)據(jù)庫環(huán)境 Environment environment = new Environment("development", transactionFactory, dataSource); // 構(gòu)建Configuration對象 Configuration configuration = new Configuration(environment); // 注冊上下文別名 configuration.getTypeAliasRegistry().registerAlias("role", MyBatis.class); // 創(chuàng)建映射器 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); return sqlSessionFactory; } }
最后編寫測試類
package com.ming; import org.junit.After; import org.junit.Before; import org.junit.Test; public class MyBatisTest { private MyBatis myBatis = null; @Before public void setUp() throws Exception { this.myBatis = new MyBatis(); } @After public void tearDown() throws Exception { } @Test public void getSqlSessionFactory() { myBatis.getSqlSessionFactory(); } @Test public void getSqlSessionFactory1() { myBatis.getSqlSessionFactory1(); } }
執(zhí)行單元測試
結(jié)果如下
即完成了MyBatis的一次查詢
注解定義接口
package com.ming; import org.apache.ibatis.annotations.Select; public interface Role1 { @Select (value="SELECT id, role_name as roleName, note FROM t_role WHERE id = #{id}") public Role getRole(int id); }生命周期 SqlSessionFactoryBuilder
其是利用xml或者java編碼構(gòu)建SqlSessionFactory 可以構(gòu)建多個SessionFactory 作用 構(gòu)建器
根據(jù)構(gòu)建器獲得sqlSessionFactory
創(chuàng)建SqlSession 一個SqlSession相當(dāng)于JDBC的Connection對象
此為單例管理
每創(chuàng)建一個SqlSession就會創(chuàng)建一個數(shù)據(jù)庫連接
此為一個會話,相當(dāng)于一個Connection連接 線程不安全
生命周期為一個應(yīng)用的請求和操作,可以執(zhí)行多條sql
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/77500.html
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:前提好幾周沒更新博客了,對不斷支持我博客的童鞋們說聲抱歉了。熟悉我的人都知道我寫博客的時間比較早,而且堅持的時間也比較久,一直到現(xiàn)在也是一直保持著更新狀態(tài)。 showImg(https://segmentfault.com/img/remote/1460000014076586?w=1920&h=1080); 前提 好幾周沒更新博客了,對不斷支持我博客的童鞋們說聲:抱歉了!。自己這段時...
摘要:前言由于寫的文章已經(jīng)是有點多了,為了自己和大家的檢索方便,于是我就做了這么一個博客導(dǎo)航。 前言 由于寫的文章已經(jīng)是有點多了,為了自己和大家的檢索方便,于是我就做了這么一個博客導(dǎo)航。 由于更新比較頻繁,因此隔一段時間才會更新目錄導(dǎo)航哦~想要獲取最新原創(chuàng)的技術(shù)文章歡迎關(guān)注我的公眾號:Java3y Java3y文章目錄導(dǎo)航 Java基礎(chǔ) 泛型就這么簡單 注解就這么簡單 Druid數(shù)據(jù)庫連接池...
閱讀 1466·2019-08-29 17:14
閱讀 1657·2019-08-29 12:12
閱讀 739·2019-08-29 11:33
閱讀 3275·2019-08-28 18:27
閱讀 1454·2019-08-26 10:19
閱讀 914·2019-08-23 18:18
閱讀 3537·2019-08-23 16:15
閱讀 2551·2019-08-23 14:14