Quick start to mybatis framework

Through the quick start example, we found it very easy to use mybatis, because you only need to write the Dao interface and follow the
mybatis requires to write two configuration files to realize the function. It's much more convenient than our previous jdbc. (when we use annotations, we will
It's simpler. Just write a mybatis configuration file.)

1. Environment construction of mybatis

Step 1: create maven project and import coordinates

Step 2: create the interface between entity class and dao

Step 3: create the main configuration file SqlMapConifg.xml of Mybatis

Step 4: create the mapping configuration file IUserDao.xml

  • Create maven factory and import coordinates
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   <modelVersion>4.0.0</modelVersion>
 6 
 7   <groupId>com.itheima</groupId>
 8   <artifactId>mybatis_eesy_day01_quickstart</artifactId>
 9   <version>1.0-SNAPSHOT</version>
10   <packaging>jar</packaging>
11 
12   <name>mybatis_eesy_day01_quickstart</name>
13   <!-- FIXME change it to the project's website -->
14   <url>http://www.example.com</url>
15 
16   <properties>
17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18     <maven.compiler.source>1.8</maven.compiler.source>
19     <maven.compiler.target>1.8</maven.compiler.target>
20   </properties>
21 
22   <dependencies>
23       <dependency>
24           <groupId>org.mybatis</groupId>
25           <artifactId>mybatis</artifactId>
26           <version>3.4.5</version>
27       </dependency>
28     <dependency>
29       <groupId>mysql</groupId>
30       <artifactId>mysql-connector-java</artifactId>
31       <version>5.1.6</version>
32     </dependency>
33     <dependency>
34       <groupId>log4j</groupId>
35       <artifactId>log4j</artifactId>
36       <version>1.2.17</version>
37     </dependency>
38     <dependency>
39       <groupId>junit</groupId>
40       <artifactId>junit</artifactId>
41       <version>4.11</version>
42       <scope>test</scope>
43     </dependency>
44   </dependencies>
45 
46   <build>
47     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
48       <plugins>
49         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
50         <plugin>
51           <artifactId>maven-clean-plugin</artifactId>
52           <version>3.1.0</version>
53         </plugin>
54         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
55         <plugin>
56           <artifactId>maven-resources-plugin</artifactId>
57           <version>3.0.2</version>
58         </plugin>
59         <plugin>
60           <artifactId>maven-compiler-plugin</artifactId>
61           <version>3.8.0</version>
62         </plugin>
63         <plugin>
64           <artifactId>maven-surefire-plugin</artifactId>
65           <version>2.22.1</version>
66         </plugin>
67         <plugin>
68           <artifactId>maven-jar-plugin</artifactId>
69           <version>3.0.2</version>
70         </plugin>
71         <plugin>
72           <artifactId>maven-install-plugin</artifactId>
73           <version>2.5.2</version>
74         </plugin>
75         <plugin>
76           <artifactId>maven-deploy-plugin</artifactId>
77           <version>2.8.2</version>
78         </plugin>
79         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
80         <plugin>
81           <artifactId>maven-site-plugin</artifactId>
82           <version>3.7.1</version>
83         </plugin>
84         <plugin>
85           <artifactId>maven-project-info-reports-plugin</artifactId>
86           <version>3.0.0</version>
87         </plugin>
88       </plugins>
89     </pluginManagement>
90   </build>
91 </project>
  • Creating entity classes and dao interfaces
1 public class User implements Serializable {
2     private Integer id;
3     private String username;
4     private Date birthday;
5     private String address;
6     private String sex;
public interface IUserDao {
//    Query all users
    List<User> findAll();
}

 

  • Create master profile
<?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>
    <!--To configure mybatis Environment-->
<environments default="mysql">
    <!--To configure mysql Environment-->
    <environment id="mysql">
        <!--Configure the type of transaction-->
        <transactionManager type="JDBC"></transactionManager>
        <!--Configure database information with data source (connection pool technology)-->
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>
            <property name="username" value="root"/>
            <property name="password" value="123"/>
        </dataSource>
    </environment>
</environments>

    <!--inform mybatis Location of the mapping file-->
    <mappers>
        <mapper resource="com/itheima/dao/IUserDao.xml"/>
    </mappers>
</configuration>

 

  • create mapping file
<?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.itheima.dao.IUserDao">
    <select id="findAll" resultType="com.itheima.domain.User">
        SELECT * FROM  user
    </select>
</mapper>

 

Write test code

package com.itheima;

import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {
    public static void main(String[] args) throws IOException {
        //1.Read profile
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.Establish SqlSessionFactory Builder object for
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.Using the builder to create a factory object SqlSessionFactory
        SqlSessionFactory factory = builder.build(in);
        //4.Use SqlSessionFactory Establish SqlSession object
        SqlSession sqlSession = factory.openSession();
        //5.Use SqlSession Establish dao Proxy object for
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        //6.Use proxy object to execute query method
        List<User> userList = userDao.findAll();
        for (User user :userList){
            System.out.println(user.toString());
        }
        //7.Release resources
        sqlSession.close();
        in.close();
    }
}

test result

User{id=41, username='Lao Wang', birthday=Tue Feb 27 17:47:08 CST 2018, address='Beijing', sex='male'}
User{id=42, username='Little two kings', birthday=Fri Mar 02 15:09:37 CST 2018, address='Beijing Golden Swallow Dragon', sex='female'}
User{id=43, username='Little two kings', birthday=Sun Mar 04 11:34:34 CST 2018, address='Beijing Golden Swallow Dragon', sex='female'}
User{id=45, username='Intelligent Podcasting', birthday=Sun Mar 04 12:04:06 CST 2018, address='Beijing Golden Swallow Dragon', sex='male'}
User{id=46, username='Lao Wang', birthday=Wed Mar 07 17:37:26 CST 2018, address='Beijing', sex='male'}
User{id=48, username='Little Ma Baoli', birthday=Thu Mar 08 11:44:00 CST 2018, address='Beijing amendment', sex='female'}

Tags: Java Maven Mybatis Apache xml

Posted on Sat, 11 Jan 2020 12:28:09 -0500 by tomkleijkers