Mybatis Series 1

 

For the first tutorial, let's simply write a demo and let's get to know mybatis together. For convenience, I use maven. As for how to use maven, I will not introduce it. I haven't used maven, and it doesn't affect reading.

1. Mybatis environment construction and simple examples

1. Create a new web project

Add dependency packages: mybatis package, database driver package (I use mysql), log package (I use log4j),

Since mine is a maven project, it's easy to add dependency packages, just add dependencies directly in pom.xml. pom.xml:

 1 <dependencies>
 2       <!-- 添加junit -->
 3     <dependency>
 4       <groupId>junit</groupId>
 5       <artifactId>junit</artifactId>
 6       <version>4.11</version>
 7       <scope>test</scope>
 8     </dependency>
 9     
10     <!-- 添加log4j -->
11     <dependency>
12         <groupId>log4j</groupId>
13       <artifactId>log4j</artifactId>
14         <version>1.2.16</version>
15     </dependency>
18     <!-- add mybatis -->
1716     
     <dependency>
19         <groupId>org.mybatis</groupId>
20       <artifactId>mybatis</artifactId>
21         <version>3.2.6</version>
22     </dependency>
23     
24     <!-- 添加mysql驱动 -->
25     <dependency>
26         <groupId>mysql</groupId>
27         <artifactId>mysql-connector-java</artifactId>
28         <version>5.1.12</version>
29     </dependency>
30     
31   </dependencies>

 

 

. Configure log4j, configure mybatis

Create a configuration file log4j.properties for configuring log4j on the classpath, and then create a configuration file configuration.xml for configuring Mybatis (the file can be named arbitrarily). I won't say much about the configuration of log4j. Here I mainly talk about configuration.xml: configuration.xml:

1 <?xml version="1.0" encoding="UTF-8" ?>
 2   2 <! DOCTYPE configuration
 3   3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4   4 "http:// mybatis.org/dtd/mybatis-3-config.dtd">
 5   5 <configuration>
 6   6 
 7   7 <!-- Specify the properties configuration file, I am configuring the database related here -->
 8   8 <properties resource= "dbConfig.properties"></properties>
 9   9   
 10 10 <!-- Specify Mybatis to use log4j -->
 11 11 <settings>
 12 12 <setting name="logImpl" value="LOG4J"/>
13 13   </settings>
14 14       
15 15 <environments default ="development">
 16 16 <environment id="development">
 17 17 <transactionManager type="JDBC"/>
 18 18 <dataSource type="POOLED">
 19 19 <!--
 20 20            if There is no properties file that specifies the database configuration above, so here you can directly configure it like this 
 21 21 <property name="driver" value="com.mysql.jdbc.Driver"/>
 22 22 <property name="url" value=" jdbc:mysql://localhost:3306/test1"/>
 23 23 <property name="username" value="root"/>
24 24         <property name="password" value="root"/>
25 25          -->
26 26          
 27 27 <!-- The database configuration file is specified above, and the configuration file also corresponds to these four properties -->
 28 28 <property name="driver" value="${driver}"/>
 29 29 < property name="url" value="${url}"/>
 30 30 <property name="username" value="${username}"/>
 31 31 <property name="password" value="${password }"/>
 32 32          
 33 33 </dataSource>
 34 34 </environment>
 35 35 </environments>
 36 36   
 37 37 <!-- Mapping files, the essence of mybatis, will be discussed later -->
38 38   <mappers>
39 39     <mapper resource="com/dy/dao/userDao-mapping.xml"/>
40 40   </mappers>
41 41   
42 42 </configuration>

3. Start writing Demo

First, create a table user in the mysql database test1:

Then, start writing java code. Take a look at my project structure:

First write an entity class User: The User class is used to correspond to the User table.

 1 User:
 2 
 3  package com.dy.entity;
 4  2 
 5  3 public class User {
 6  4 
 7  5     private int id;
 8  6     private String name;
 9  7     private String password;
10  8     private int age;
11  9     private int deleteFlag;
12 10     
13 11     public int getId() {
14 12         return id;
15 13     }
16 14     public void setId(int id) {
17 15         this.id = id;
18 16     }
19 17     public String getName() {
20 18         return name;
21 19     }
22 20     public void setName(String name) {
23 21         this.name = name;
24 22     }
25 23     public String getPassword() {
26 24         return password;
27 25     }
28 26     public void setPassword(String password) {
29 27         this.password = password;
30 28     }
31 29     public int getAge() {
32 30         return age;
33 31     }
34 32     public void setAge(int age) {
35 33         this.age = age;
36 34     }
37 35     public int getDeleteFlag() {
38 36         return deleteFlag;
39 37     }
40 38     public void setDeleteFlag(int deleteFlag) {
41 39         this.deleteFlag = deleteFlag;
42 40     }
43 41     
44 42 }

Write another UserDao interface: UserDao:

 

 1 package com.dy.dao;
 2  2 
 3  3 import java.util.List;
 4  4 
 5  5 import com.dy.entity.User;
 6  6 
 7  7 public interface UserDao {
 8  8 
 9  9     public void insert(User user);
10 10     
11 11     public User findUserById (int userId);
12 12     
13 13     public List<User> findAllUsers();
14 14     
15 15 }

Write another userDao-mapping.xml (you can name it whatever you want): userDao-mapping.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>   
 2  2 <!DOCTYPE mapper   
 3  3 PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  
 4  4 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 
 5  5 <mapper namespace="com.dy.dao.UserDao">
 6  6 
 7  7    <select id="findUserById" resultType="com.dy.entity.User" > 
 8  8       select * from user where id = #{id}
 9  9    </select>
10 10 
11 11 </mapper>

userDao-mapping.xml is equivalent to the implementation of UserDao, and also successfully associates the User entity class with the data table User.

4. Write the junit test code UserDaoTest below:

UserDaoTest:

 1 public class UserDaoTest {
 2  2 
 3  3     @Test
 4  4     public void findUserById() {
 5  5         SqlSession sqlSession = getSessionFactory().openSession();  
 6  6         UserDao userMapper = sqlSession.getMapper(UserDao.class);  
 7  7         User user = userMapper.findUserById(2);  
 8  8         Assert.assertNotNull("没找到数据", user);
 9  9     }
10 10     
11 11     //Mybatis obtains SqlSession through SqlSessionFactory, and then can interact with the database through SqlSession 
12 12      private  static SqlSessionFactory getSessionFactory() {  
 13 13 SqlSessionFactory sessionFactory = null ;  
 14 14 String resource = "configuration.xml" ;  
 15 15          try {  
 16 16 sessionFactory = new SqlSessionFactoryBuilder().build(Resources  
 17 17                      .getResourceAsReader(resource));
 18 18 } catch (IOException e) {  
 19 19             e.printStackTrace();  
20 20         }  
21 21         return sessionFactory;  
22 22     }  
23 23     
24 24 }

 

In this demo, you should be able to see the operating mechanism of mybatis initially. If it is not clear, it does not matter. From the next article, the official explanation of mybatis begins.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325390657&siteId=291194637