Eclipse利用Maven快速上手搭建MyBatis

  • 2019 年 10 月 7 日
  • 筆記

一、what is maven?

  Maven項目對象模型(POM),可以通過一小段描述信息來管理項目的構建,報告和文檔的項目管理工具軟件。

  Maven 除了以程序構建能力為特色之外,還提供高級項目管理工具。由於 Maven 的缺省構建規則有較高的可重用性,所以常常用兩三行 Maven 構建腳本就可以構建簡單的項目。由於 Maven 的面向項目的方法,許多 Apache Jakarta 項目發文時使用 Maven,而且公司項目採用 Maven 的比例在持續增長。
  Maven這個單詞來自於意第緒語(猶太語),意為知識的積累,最初在Jakata Turbine項目中用來簡化構建過程。當時有一些項目(有各自Ant build文件),僅有細微的差別,而JAR文件都由CVS來維護。於是希望有一種標準化的方式構建項目,一個清晰的方式定義項目的組成,一個容易的方式發佈項目的信息,以及一種簡單的方式在多個項目中共享JARs。

二、安裝與配置

  2.1、直接下載(需jdk1.7或更高)

地址:直接下載

  2.2、官網下載:http://maven.apache.org/download.cgi

 

   2.3、Maven與Eclipse關聯

 

   2.4、創建Maven項目,並配置pom.xml

 1     <dependencies>   2         <!-- 添加MyBatis框架3.4.6版本 -->   3         <dependency>   4             <groupId>org.mybatis</groupId>   5             <artifactId>mybatis</artifactId>   6             <version>3.4.6</version> <!-- 版本號視情況修改 -->   7         </dependency>   8         <!-- 添加MySql驅動包 -->   9         <dependency>  10             <groupId>mysql</groupId>  11             <artifactId>mysql-connector-java</artifactId>  12             <version>5.1.25</version>  13         </dependency>  14     </dependencies>

  2.5、創建XML配置MyBatis

 

 1 <?xml version="1.0" encoding="UTF-8"?>   2 <!DOCTYPE configuration   3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"   4   "http://mybatis.org/dtd/mybatis-3-config.dtd">   5 <configuration>   6     <environments default="development">   7         <environment id="development">   8             <transactionManager type="JDBC" />   9             <dataSource type="POOLED">  10                 <property name="driver" value="com.mysql.jdbc.Driver" /> <!-- 驅動類型 -->  11                 <property name="url" value="jdbc:mysql://localhost:3306/sam" /> <!-- 連接字符串 -->  12                 <property name="username" value="root" /> <!-- 用戶名 -->  13                 <property name="password" value="root" /> <!-- 密碼 -->  14             </dataSource>  15         </environment>  16     </environments>  17     <mappers>  18         <mapper resource="DeptMapper.xml" /> <!-- 映射SQL語句的XML文件 -->  19     </mappers>  20 </configuration>

  2.6、創建XML映射SQL語句

 1 <?xml version="1.0" encoding="UTF-8"?>   2 <!DOCTYPE mapper   3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">   5 <mapper namespace="Dept">   6     <!-- 插入單個部門信息 -->   7     <insert id="InsertDept">   8         INSERT INTO DEPT (DNAME,LOC)   9         VALUES (#{DName},#{Loc})  10     </insert>  11 </mapper>

CRUD語法

 1 <insert id="insertAuthor">   2   insert into Author (id,username,password,email,bio)   3   values (#{id},#{username},#{password},#{email},#{bio})   4 </insert>   5   6 <update id="updateAuthor">   7   update Author set   8     username = #{username},   9     password = #{password},  10     email = #{email},  11     bio = #{bio}  12   where id = #{id}  13 </update>  14  15 <delete id="deleteAuthor">  16   delete from Author where id = #{id}  17 </delete>

  2.7、創建實體類

表結構

 1 package com.chenyanbin;   2   3 public class Dept {   4     //部門名稱   5     private String DName;   6     //部門位置   7     private String Loc;   8     public String getDName() {   9         return DName;  10     }  11     public void setDName(String dName) {  12         DName = dName;  13     }  14     public String getLoc() {  15         return Loc;  16     }  17     public void setLoc(String loc) {  18         Loc = loc;  19     }  20 }

  2.8、創建Main函數

 1 package com.chenyanbin;   2   3 import java.io.IOException;   4 import java.io.InputStream;   5 import org.apache.ibatis.io.Resources;   6 import org.apache.ibatis.session.SqlSession;   7 import org.apache.ibatis.session.SqlSessionFactory;   8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;   9  10 public class TestMain {  11     public static void main(String[] args) throws IOException {  12         //創建實體類  13         Dept dept = new Dept();  14         dept.setDName("上海事業部");  15         dept.setLoc("上海");  16         //加載XML文件  17         InputStream is = Resources.getResourceAsStream("myBatis-config.xml"); //加載MyBatis的配置文件  18         //初始化SqlSessionFactory  19         SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);  20         SqlSession session = factory.openSession();  21         session.insert("InsertDept", dept);  22         session.commit();  23         session.close();  24     }  25 }

 

  

  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之後對反射做限制了,有兩種解決方案

  1. 把jdk回到jdk9之前
  2. 升級MyBatis