Build Mybatis framework for idea maven project

Preparatory work:
1. Create a database
2. Create a new user table. The table fields include id, username, birthday, sex and address
3. Download and unzip the maven installation package
New maven project:

2. Select maven and click next

3. GroupId generally fills in the company domain name and writes it backwards. ArtifactId writes the project name. Version is the project version number. Click next

4. Click next, and then finish directly
5. After the project is created, select allow automatic import in the lower right corner

6. Create two new configuration files under src – main – resources (JDBC config. Properties & & log4j. Properties)
① contents of JDBC

    	jdbc.url=jdbc:mysql://localhost:3306 / database name
    	jdbc.username=Database user name
    	jdbc.password=Database password


# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE., CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE.File=d:\axis.log**(Note here that the computer needs to have d Otherwise, it will fail to create the log file d Change the drive to the drive letter owned by your computer)**
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

7. Import related dependency packages in pom.xml

        <!-- -->

8. Create a new User entity class in the entity layer, and provide the corresponding get, set and other methods:

 package com.xmcc.entity;

import lombok.*;
import java.util.Date;

 * @author zxl
 * @date 2019-03-06 16:44
public class User implements Serializable {
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public User(int id, String sex) { = id; = sex;
    public User(int id) { = id;

9. Create a new interface in dao layer
Provide a simple method of adding, deleting, modifying and checking correspondence

package com.xmcc.dao;

import com.xmcc.entity.User;

import java.util.List;

public interface UserDao {
    List<User> findAll();
    void update(User user);
    void insert(User user);
    void delete(User user);

10. Create a UserDao.xml file with the same name under the same directory structure under resources

11. Import constraints in SqlMapConfig.xml and configure them

   <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-// Config 3.0//EN"
<!--Load profile-->
<properties resource=""></properties>

<!--To configure mybatis Environmental Science-->
<environments default="development">
    <environment id="development">
        <!-- transaction management -->
        <transactionManager type="JDBC"></transactionManager>
        <!-- data source -->
        <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
<!--Import mapping profile-->
    <mapper resource="com/xmcc/dao/UserDao.xml"></mapper>

12. Import constraints in UserDao.xml and implement corresponding methods of UserDao interface

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN"

<!--namespace Interface full pathname-->
<mapper namespace="com.xmcc.dao.UserDao">
    <select id="findAll" resultType="com.xmcc.entity.User">
        select * from user;
    <update id="update" parameterType="com.xmcc.entity.User">
        update user set sex=#{sex} where id=#{id};
    <insert id="insert" parameterType="com.xmcc.entity.User">
        insert into user values(#{id},#{username},#{birthday},#{sex},#{address});

    <delete id="delete" parameterType="com.xmcc.entity.User">
        delete from user where id=#{id};

13. Write test class
Test the findAll method:

            //1. Read configuration file
            InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2. Create SqlSessionFactory construction object
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //3. Create SqlSessionFactory
           SqlSessionFactory factory =;
   			 //4. Open sqlSession (operate database object)
            SqlSession sqlSession = factory.openSession();
            //5. Create dao interface proxy object with sqlSession
            UserDao dao = sqlSession.getMapper(UserDao.class);
            List<User> all = dao.findAll();
            //6. Release resources

Similar to other methods, adding, modifying, and deleting a transaction need to be submitted actively after the method is called
//Submit event

It's not easy to write. Please support more. If you have any questions, please contact email

Keywords: log4j JDBC Mybatis xml

Added by jawinn on Mon, 09 Dec 2019 23:48:54 +0200