mybatis framework learning-traditional dao layer development

Preface

mybatis officially recommends using the mapper proxy method to develop the mapper interface. Programmers do not need to write mapper interface implementation classes.

When using the mapper proxy method, the input parameters can use pojo wrapper object or map object to ensure the universality of dao.

Traditional dao layer development is prone to hard coding problems.

1. Traditional DAO layer development:

Problems with mybatis traditional DAO layer development:
1. There are a large number of template methods in the Dao interface, which increases the workload.

2. Hardcoded the statement's id when calling the sqlSession method

3. Invoke sqlSession incoming variables, because sqlSession method uses generics, even if the variable type is passed in incorrectly, it will not error at compile stage, which is not conducive to program development.

2. The mapper agent develops the Mapper interface:

The main differences between mapper agent development and traditional development are
1. Interface does not need to be implemented
2. The namespace of the mapper file is meaningful and must match the mapper interface class path.
3. The Mapper interface method name is the same as the id of each statement defined in Mapper.xml
4. The input and output parameters of the mapper interface method name must match the type defined in mapper.xml.

In fact, after learning mapper agent development, you may still have a question: if the result returned is a list, what will be the internal judgment of the agent?

  • It is automatically determined within the proxy that if a list is returned, the selectlist method of sqlsession is called, and if it is an object, the selectone method of sqlsession is called.

Summary: The mapper proxy development interface saves us code by not writing implementation classes; the mapper proxy object reduces our hard coding and makes coding more flexible.
The following personal cases

Pre-mysql preparation

create database cong
use cong;
create table account(
    id int primary key auto_increment,
    name varchar(40),
    money float
)character set utf8 collate utf8_general_ci;

insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);

 

1. Create maven projects and import dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cong</groupId>
    <artifactId>mybatis_traditionnal_dao</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>
</project>

2. Create the com.cong.pojo.Account class

package com.cong.pojo;

import java.io.Serializable;

public class Account implements Serializable{
    private int id;
    private String name;
    private float money;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }
}

3. Create the com.cong.mapper package, create the mapper interface, and implement classes

package com.cong.mapper;

import com.cong.pojo.Account;
import java.util.List;

public interface AccountMapper {
    List<Account> findAll();
    Account findById(int id);
    List<Account> findByName(String name);
    int findTotal();
    void saveAccount(Account account);
    void updateAccount(Account account);
    void deleteAccount(int id);
}




package com.cong.mapper;

import com.cong.pojo.Account;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;

public class AccountMapperImpl implements AccountMapper {
    private SqlSessionFactory factory;
    public AccountMapperImpl(SqlSessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public List<Account> findAll() {
        //1.according to factory Obtain SqlSession object
        SqlSession session = factory.openSession();
        //2.call SqlSession Implementation of query list
        //A parameter is one that gets configuration information key,that is mapper.xml in namespace Add Method Name
        List<Account> accounts = session.selectList("accountMapper.findAll");
        //3.Release Resources
        session.close();
        return accounts;
    }

    @Override
    public Account findById(int id) {
        //1.according to factory Obtain SqlSession object
        SqlSession session = factory.openSession();
        //2.call SqlSession The implementation follows the id query
        //A parameter is one that gets configuration information key
        Account account = session.selectOne("accountMapper.findById",id);
        //3.Release Resources
        session.close();
        return account;
    }

    @Override
    public List<Account> findByName(String name) {
        //1.according to factory Obtain SqlSession object
        SqlSession session = factory.openSession();
        //2.call SqlSession Method in to implement fuzzy query list by name
        List<Account> accounts = session.selectList("accountMapper.findByName",name);
        //3.Release Resources
        session.close();
        return accounts;
    }

    @Override
    public int findTotal() {
        //1.according to factory Obtain SqlSession object
        SqlSession session = factory.openSession();
        //2.call SqlSession Method in to implement the number of queries
        int total = session.selectOne("accountMapper.findTotal");
        //3.Release Resources
        session.close();
        return total;
    }

    @Override
    public void saveAccount(Account account) {
        //1.according to factory Obtain SqlSession object
        SqlSession session = factory.openSession();
        //2.call SqlSession Method in to save
        session.insert("accountMapper.saveAccount",account);
        //Submit
        session.commit();
        //3.Release Resources
        session.close();
    }

    @Override
    public void updateAccount(Account account) {
        //1.according to factory Obtain SqlSession object
        SqlSession session = factory.openSession();
        //2.call SqlSession Method in to update
        session.update("accountMapper.updateAccount",account);
        //Submit
        session.commit();
        //3.Release Resources
        session.close();
    }

    @Override
    public void deleteAccount(int id) {
        //1.according to factory Obtain SqlSession object
        SqlSession session = factory.openSession();
        //2.call SqlSession Method in to delete
        session.delete("accountMapper.deleteAccount",id);
        //Submit
        session.commit();
        //3.Release Resources
        session.close();
    }
}

4. Create SqlMapConfig.xml configuration file under resources

<?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>
    <typeAliases>
        <typeAlias type="com.cong.pojo.Account" alias="account"></typeAlias>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/cong"></property>
                <property name="username" value="root"></property>
                <property name="password" value="123456"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/cong/mapper/AccountMapper.xml"></mapper>
    </mappers>
</configuration>

5. Create log4j.properties configuration file in resources

# 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.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.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=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

6. Create the directory com/cong/mapper under resources, and then create the AccountMapper.xml 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">
<!-- here namespace No special meaning -->
<mapper namespace="accountMapper">
    <!-- Query all -->
    <select id="findAll" resultType="account">
        select * from account;
    </select>
    <!-- according to id Query Users -->
    <select id="findById" parameterType="INT" resultType="account">
        select * from account where id = #{uid}
    </select>

    <!-- Fuzzy Query by Name -->
    <select id="findByName" parameterType="string" resultType="account">
        select * from account where name like #{name}
    </select>

    <!-- Get the total number of records for the user -->
    <select id="findTotal" resultType="int">
        select count(id) from account;
    </select>

    <!-- Save User -->
    <insert id="saveAccount" parameterType="account">
        <selectKey keyColumn="id" keyProperty="id" resultType="int" order="AFTER">
            select LAST_INSERT_ID();
        </selectKey>
        <!-- Get the insert data after configuring the insert operation id -->
        insert into account(name,money) values(#{name},#{money})
    </insert>

    <!-- Update User -->
    <update id="updateAccount" parameterType="account">
        update account set name=#{name},money=#{money} where id=#{id}
    </update>

    <!-- delete user-->
    <delete id="deleteAccount" parameterType="java.lang.Integer">
        delete from account where id = #{uid}
    </delete>
</mapper>

7. Create test classes

import com.cong.mapper.AccountMapper;
import com.cong.mapper.AccountMapperImpl;
import com.cong.pojo.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;

public class TestAccount {
    public InputStream inputStream;
    public AccountMapper mapper;
    @Test
    public void findAll(){
        List<Account> accounts = mapper.findAll();
        for (Account account : accounts) {
            System.out.println(account.toString());
        }
    }
    @Test
    public void findById(){
        Account account = mapper.findById(1);
        System.out.println(account.toString());
    }
    @Test
    public void findByName(){
        List<Account> accounts = mapper.findByName("%ong%");
        for (Account account : accounts) {
            System.out.println(account.toString());
        }
    }
    @Test
    public void findTotal(){
        int accounts = mapper.findTotal();
        System.out.println(accounts);
    }
    @Test
    public void saveAccount(){
        Account account = new Account();
        account.setName("rainbow");
        account.setMoney(222222);
        mapper.saveAccount(account);
        System.out.println(account);
    }
    @Test
    public void updateAccount(){
        Account account = new Account();
        account.setName("rainbow");
        account.setMoney(222222);
        account.setId(6);
        mapper.updateAccount(account);
    }
    @Test
    public void deleteAccount(){
        mapper.deleteAccount(8);
    }
    @Before
    public void init() throws Exception{
        //1.Read profile to generate byte input stream
        inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.Obtain SqlSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
        //3.Using factory objects, create mapper object
        mapper = new AccountMapperImpl(factory);
    }
    @After
    public void destroy() throws  Exception{
        inputStream.close();
    }
}

8. Full directory structure

 

Here's how to write the execution of the dao layer implementation class Mybatis. The example is different from my case, but it's still understandable

 

Reference resources: https://blog.csdn.net/changyinling520/article/details/78102734

Keywords: PHP Session log4j Apache Mybatis

Added by xzilla on Mon, 05 Aug 2019 21:52:01 +0300