Eclipse uses Maven to build MyBatis quickly

Keywords: Java Mybatis Maven xml Apache

what is maven?

Maven Project Object Model (POM) is a project management tool software that can manage project construction, reporting and documentation through a short description of information.

Maven provides advanced project management tools in addition to program building capabilities. Because Maven's default build rules are highly reusable, it is often possible to build simple projects with two or three lines of Maven build scripts. Because of Maven's project-oriented approach, many Apache Jakarta projects use Maven for publishing, and the proportion of corporate projects using Maven is growing.
The word Maven comes from Yiddish, meaning the accumulation of knowledge, which was originally used in the Jakata Turbine project to simplify the construction process. At that time, there were some projects (with their own Ant build files) with only minor differences, while JAR files were all made by CVS For maintenance. So I want a standardized way to build projects, a clear way to define the composition of projects, an easy way to publish project information, and a simple way to share JARs across multiple projects.

II. Installation and Configuration

2.1. Direct download (jdk1.7 or higher)

Address: Direct download

2.2. Official website download: http://maven.apache.org/download.cgi

 

2.3. Maven and Eclipse Association

 

2.4. Create a Maven project and configure pom.xml

 1     <dependencies>
 2         <!-- Add to MyBatis Frame 3.4.6 Edition -->
 3         <dependency>
 4             <groupId>org.mybatis</groupId>
 5             <artifactId>mybatis</artifactId>
 6             <version>3.4.6</version> <!-- Revision of version number as appropriate -->
 7         </dependency>
 8         <!-- Add to MySql Driving package -->
 9         <dependency>
10             <groupId>mysql</groupId>
11             <artifactId>mysql-connector-java</artifactId>
12             <version>5.1.25</version>
13         </dependency>
14     </dependencies>

2.5. Create an XML configuration for 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" /> <!-- Driving type -->
11                 <property name="url" value="jdbc:mysql://localhost:3306/sam" /> <!-- Connection string -->
12                 <property name="username" value="root" /> <!-- User name -->
13                 <property name="password" value="root" /> <!-- Password -->
14             </dataSource>
15         </environment>
16     </environments>
17     <mappers>
18         <mapper resource="DeptMapper.xml" /> <!-- mapping SQL Sentence XML file -->
19     </mappers>
20 </configuration>

2.6. Create XML Mapping SQL Statements

 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     <!-- Insert individual department information -->
 7     <insert id="InsertDept">
 8         INSERT INTO DEPT (DNAME,LOC)
 9         VALUES (#{DName},#{Loc})
10     </insert>
11 </mapper>

CRUD syntax

 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. Creating Entity Classes

Table structure

 1 package com.chenyanbin;
 2 
 3 public class Dept {
 4     //Department name
 5     private String DName;
 6     //Departmental position
 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. Create Main Function

 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         //Create entity classes
13         Dept dept = new Dept();
14         dept.setDName("Business department");
15         dept.setLoc("Xie");
16         //Load XML file
17         InputStream is = Resources.getResourceAsStream("myBatis-config.xml"); //Load MyBatis Configuration file
18         //Initialization 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. Catalogue of Project Files

 

The above configuration is complete, but the blogger encounters a problem, the database is saved, the program warning, warning as follows:

 

 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

After checking online, jdk8 has limited reflection. There are two solutions

  1. Return JDK to jdk9
  2. Upgrade MyBatis

Posted by xt3mp0r~ on Tue, 08 Oct 2019 21:27:50 -0700