MyBatis入門一,MyBatis入門
一.MyBatis
MyBatis是一個開源的資料持久層架構,其主要思想是將程式中的大量sql語句剝離出來,配置在設定檔中,實現SQL的靈活配置。並且可以在不修改程式碼的情況下
,直接在設定檔中修改SQL。
二.什麼事ORM?
orm(object/Relation Mapping)對象/關係映射。是一種資料持久化技術,在物件模型和關係型資料庫之間建立對應關係,通過javaBean對象去操作資料庫表中的資料。
三.建立MyBatis核心設定檔configureation.xml
----案例配置
xml節點作用:https://www.geeksss.com/article-298-1.html
完整的configure配置
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <!-- 設定mtbatis的一些運行參數:日誌,是直接輸出到控制台上還是輸出到log4j檔案中 --> <settings> <!-- name不可以隨便寫,只能叫這個名字;value有好幾個選項這裡選擇向控制台輸出 --> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!--設定類型別名:在執行sql語句的時候,如果不設定別名。每次在指定的時候都需要包名+類名,寫的長 --> <typeAliases> <!-- type就是完整實體類名, --> <tapeAlias type="entity.student" alias="User"/> </typeAliases> <!--資料庫連接的屬性,不同的開發階段串連不同的資料庫 開發階段——開發環境:串連程式員自己的資料庫 測試階段——測試環境:串連測試資料庫 上線運行——生產環境:串連正式資料庫 裡邊可以寫多個environment --> <environments default="dev"> <!-- 開發環境 --> <environment id="dev"> <!-- 通過JDBC管理事務 --> <transactionManager type="JDBC"/> <!--不使用串連池 --> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306:myschool"/> <property name="username" value="root"/> <property name="password" value="0217"/> </dataSource> </environment> </environments> <!--sql對應檔的路徑--> <mappers> <!-- 引入要載入的sql檔案 --> <mapper resource="studentDao.xml"/> </mappers> </configuration>
mapper映射配置
<?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="dao.studentDao"> <!--resultMap--> <resultMap id="studentMap" type="entity.student"> <id property="stuId" column="stuId"></id> <result property="stuName" column="stuName"></result> </resultMap> <!--查詢所有--> <select id="getAllStudent" resultMap="studentMap"> <!--resultMap對應 resultMap下的id值--> select * from student </select> <!--根據id值查詢--> <select id="getStudentById" parameterType="int" resultType="stu"> select * from student where stuId = #{stuId} </select></mapper>
四:編寫工具類
public class MyBatis { private static final String RESOURCE = "mybatis-config.xml"; private static SqlSessionFactory sqlSessionFactory = null; private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); //關閉sqlsession public static void closeSession(){ SqlSession session = (SqlSession) threadLocal.get(); // 2 threadLocal.set(null); if (session !=null){ session.close(); } } public static SqlSession getSessionTwo(){ //讀取設定檔 try { InputStream stream = Resources.getResourceAsStream(RESOURCE); sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); return sqlSessionFactory.openSession(); } catch (IOException e) { e.printStackTrace(); } return null; }}
五:介面層
public interface studentDao { /** * 查詢所有學生 * @return */ public List<student> getAllStudent(); /** * 根據id查學生 * @param * @return */ public student getStudentById(int id); /** * 新增學生 * @return */ public int addStudent(student stu); /** * 刪除 學生根據id * @param id * @return */ public int deleStudent(int id);}
六: 測試類別
@Test public void TestDele() throws IOException { //找到大配置 // String path = "mybatis-config.xml"; //讀取設定檔 //InputStream is = Resources.getResourceAsStream(path); //拿到sqlsession工廠 //SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //SqlSession session = sqlSessionFactory.openSession(); studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class); int value = dao.deleStudent(1); ------修改 ??? 下面 System.out.print(value); }
另外 在刪除和添加 的時候,如果 不進行sqlSession提交,所執行的結果是不會影響到資料庫的,而且牽扯到Sqlsession的線程問題,
執行方法的sqlsession和執行提交的sqlsession必須一致
SqlSession sessionTwo = MyBatis.getSessionTwo(); studentDao dao =sessionTwo.getMapper(studentDao.class); int id = dao.deleStudent(1); System.out.print(id); sessionTwo.commit();