一、what is maven?
Maven項目對象模型(POM),可以通過一小段描述信息來管理項目的構建,報告和文檔的項目管理工具軟件。
二、安裝與配置
2.1、直接下載(需jdk1.7或更高)
地址:直接下載
2.2、官網下載:http://maven.apache.org/download.cgi
2.3、Maven與Eclipse關聯
2.4、創建Maven項目,並配置pom.xml
<dependencies> <!-- 添加MyBatis框架3.4.6版本 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> <!-- 版本號視情況修改 --> </dependency> <!-- 添加MySql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> </dependencies>
2.5、創建XML配置MyBatis
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <!-- 驅動類型 --> <property name="url" value="jdbc:mysql://localhost:3306/sam" /> <!-- 連接字符串 --> <property name="username" value="root" /> <!-- 用戶名 --> <property name="password" value="root" /> <!-- 密碼 --> </dataSource> </environment> </environments> <mappers> <mapper resource="DeptMapper.xml" /> <!-- 映射SQL語句的XML文件 --> </mappers> </configuration>
2.6、創建XML映射SQL語句
<?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="Dept"> <!-- 插入單個部門信息 --> <insert id="InsertDept"> INSERT INTO DEPT (DNAME,LOC) VALUES (#{DName},#{Loc}) </insert> </mapper>
CRUD語法
<insert id="insertAuthor"> insert into Author (id,username,password,email,bio) values (#{id},#{username},#{password},#{email},#{bio}) </insert> <update id="updateAuthor"> update Author set username = #{username}, password = #{password}, email = #{email}, bio = #{bio} where id = #{id} </update> <delete id="deleteAuthor"> delete from Author where id = #{id} </delete>
2.7、創建實體類
表結構
package com.chenyanbin; public class Dept { //部門名稱 private String DName; //部門位置 private String Loc; public String getDName() { return DName; } public void setDName(String dName) { DName = dName; } public String getLoc() { return Loc; } public void setLoc(String loc) { Loc = loc; } }
2.8、創建Main函數
package com.chenyanbin; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class TestMain { public static void main(String[] args) throws IOException { //創建實體類 Dept dept = new Dept(); dept.setDName("上海事業部"); dept.setLoc("上海"); //加載XML文件 InputStream is = Resources.getResourceAsStream("myBatis-config.xml"); //加載MyBatis的配置文件 //初始化SqlSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); SqlSession session = factory.openSession(); session.insert("InsertDept", dept); session.commit(); session.close(); } }
2.9、項目文件目錄圖
以上配置完成,但是博主碰到一個問題,數據庫保存進去了,程序警告,警告如下:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/C:/Users/Windows10/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar) to method java.lang.Class.checkPackageAccess(java.lang.SecurityManager,java.lang.ClassLoader,boolean)
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
網上查了下,jdk8之后對反射做限制了,有兩種解決方案
- 把jdk回到jdk9之前
- 升級MyBatis
三、MyBatis框架執行流程解析
- 將sql語句和數據庫配置信息保存在配置文件
- 在MyBatis運行時,將配置信息存儲Configuration對象
- 在創建SqlSession對象,提供屬性
- Configuration對象
- dirty:true->sql語句執行完畢后,可以事務提交;false->sql語句執行發送錯誤,事務進行回滾
- Executor執行器對象:創建Statement對象,在創建過程中依靠MapperStatement對象將賦值內容與sql占位符進行綁定處理
- SqlSession.commit():此時根據dirty屬性絕對提交和回滾
- SqlSession.close():