mybatis development process

1) New Java Project

2) lib file import mybatis jar package and database-driven (into the project directory folder, you need to Build Path)

3) create a database and tables

4) adding the entity class and a constructor

5) Add mybatis profile conf.xml (load the database driver to connect to the database, the user name and password for the database)

* Add in the resource folder

* Conf.xml configuration information of the data sources: the information database

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <environments default="development">
 6         <environment id="development">
 7             <transactionManager type="JDBC" />
 8             <dataSource type="POOLED">
 9                 <property name="driver" value="com.mysql.jdbc.Driver" />
10                 <property name="url"
11                     value="jdbc:mysql://localhost:3306/mybatis" />
12                 <property name="username" value="root" />
13                 <property name="password" value="root" />
14             </dataSource>
15         </environment>
16     </environments>
17 </configuration>

 

6) definition of the operation of the user table mapping file sql userMapper.xml (an example in the user table, i.e. mapping file mapping between the entity class table, sql statement)

* New mapper package in the src directory, then create a new file in the package userMapper.xml

* Id: identify the tag

* ParameterType: data type of the parameter

* ResultType: return value data type

* Mapper: namely mapping

* Namespace: namespace representation, now the aim is to distinguish between the id

* Mybatis placeholder with # {}

# {Id}: similar to the EL expression resolves id

<?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.zhiyou.clg.mapper.UserMapper">
<select id="selectUser" parameterType="int"
resultType="com.zhiyou.clg.bean.User">
select * from user where id=#{id}
</select>
</mapper>

 

7) Register user Mapper.xml conf.xml file in the file (i.e., the map file is introduced to the profile)

* Use mapper mappers tag tags (attributes: resource)

1     <mappers>
2         <mapper resource="com/zhiyou/clg/mapper/UserMapper.xml"/>
3     </mappers>

 

8) to test

// parse the configuration file conf.xml

Reader reader = Resources.getResourceAsReader(“conf.xml”);

// Get SessionFactory object

SqlSessionFactory  sessionFactory = new  SqlSessionFactoryBuilder().build(reader);

// Get the Session object. Here represented in jdbc Connection. Is the operation of the database

SqlSession session = sessionFactory.openSession();

User user = session.selectOne("com.zhiyou.clg.mapper.userMapper.getUser", 1);

* Additions and deletions to the time required to manually submit the task, is about to submit data to the database

session.commit();

Manual submission management can achieve things: things are composed of a series of actions, these actions are either executed or not executed

 1 package com.zhiyou.clg.mapper;
 2 
 3 import static org.junit.jupiter.api.Assertions.*;
 4 
 5 import java.io.IOException;
 6 import java.io.Reader;
 7 
 8 import org.apache.ibatis.io.Resources;
 9 import org.apache.ibatis.session.SqlSession;
10 import org.apache.ibatis.session.SqlSessionFactory;
11 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
12 import org.junit.jupiter.api.AfterAll;
13 import org.junit.jupiter.api.BeforeAll;
14 import org.junit.jupiter.api.Test;
15 
16 import com.zhiyou.clg.bean.User;
17 
18 class UserTest {
19     private static SqlSession session = null;
20     final static String str="com.zhiyou.clg.mapper.UserMapper";
21     @BeforeAll//测试前会执行
22     static void setUpBeforeClass() throws Exception {
23                 //解析配置文件
24                 Reader reader = Resources.getResourceAsReader("conf.xml");
25                 //获取SessionFactory对象
26                 SqlSessionFactory SessionFactory = new SqlSessionFactoryBuilder().build(reader);
27                 //获取Session对象
28                 session = SessionFactory.openSession();
29     }
30 
31     @AfterAll//测试后执行
32     static void tearDownAfterClass() throws Exception {
33         session.commit();
34     }
35 
36     @Test
37     void test() throws IOException {
38         User user=session.selectOne(str+".selectUser", 1);
39         System.out.println(user);
40     }
41 
42 }

 

Guess you like

Origin www.cnblogs.com/lwgok1003/p/11442629.html