1. Introduction to MyBatis ( http://www.mybatis.org/mybatis-3/zh/index.html )
MyBatis is an excellent persistence layer framework that supports custom SQL , stored procedures and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets. MyBatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (Plain Old Java Objects, ordinary Java objects ) to records in the database.
2. MyBatis Quick Start
1. Build the development environment
1.1 Create a maven project (Note: I created a java project here, and the database uses sqlite)
1.2 Add the sqlite database driver package and the mybatis package to the pom.xml file
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.11.2</version>
</dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
1.3 Create tables t_group and t_person (the process of creating tables is ignored here, and the operations of creating tables in sqlite are similar to those in mysql)
2. Use the mybatis framework to write a simple query
2.1 Write the entity class corresponding to the t_group table
public class Group { private int id; private String groupName; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
2.2 Add MyBatis configuration file mybatis.xml
<?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="org.sqlite.JDBC" /> <property name="url" value="jdbc:sqlite:cfg/ht.db" /> <property name="username" value="" /><!-- sqlite is a built-in file database and does not require a username and password--> <property name="password" value="" /> </dataSource> </environment> </environments> </configuration>
2.3 Write the sql mapping file GroupMapper.xml corresponding to the t_group table
<?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="com.zlt.mybatis.model.Group"> <select id="selectGroupByID" parameterType="int" resultType="Group" > select t.id,t.group_name,t.description from t_group t where t.id = #{id} </select> </mapper>
2.4 Register the GroupMapper.xml file in the mybatis.xml file
Add the following in the mybatis.xml file
<mappers> <mapper resource="com/zlt/mybatis/mapping/GroupMapper.xml" /> </mappers>
2.5 Writing test classes
Building an instance of SqlSessionFactory from an XML file is straightforward, and it is recommended to use a resource file on the classpath for configuration. But it can also be configured using any InputStream instance, including a file path in the form of a string or a file path in the form of a URL of file://. MyBatis includes a utility class called Resources that contains utility methods that make it easier to load resource files from the classpath or other locations.
public class TestMyBatis { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("mybatis.xml");//Load the configuration file of mybatis sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);//Build sqlSession factory } catch (Exception e) { e.printStackTrace (); } } public static void main(String[] args) { SqlSession session = sqlSessionFactory.openSession();///Create a sqlSession that can execute sql in the mapping file String statement = "com.zlt.mybatis.model.Group.selectGroupByID";////Map sql identification string Group group = session.selectOne(statement, 1);////Execute the query to return a unique group object sql if(null != group) { System.out.println("id:" +group.getId()+"name:" +group.getGroupName()+"description:" + group.getDescription()); } } }
2.6 Running results (Note: Although the results are out, the group_name attribute is found to be empty, and the records in the table are valuable, the next section analyzes)
2.7 Finally, the entire project structure is as follows: