MyBatis written using two grammar Dao

Original link: http://www.yiidian.com/mybatis/mybatis-dao.html

In MyBatis, we have two wording Dao, Dao called traditional wording, called Mapper proxy interface. Let's see how.

1 Dao traditional wording

1.1 interface to write CustomerDao

package com.yiidian.dao;
import com.yiidian.domain.Customer;
import java.util.List;
/**
 * Dao接口
 *一点教程网 - www.yiidian.com
 */
public interface CustomerDao {

    /**
     * 查询所有用户
     */
    public List<Customer> findAll();

    /**
     * 添加
     */
    public void save(Customer customer);

    /**
     * 修改
     */
    public void update(Customer customer);

    /**
     * 查询一个
     */
    public Customer findById(Integer id);

    /**
     * 条件查询
     */
    public List<Customer> findByName(String name);

    /**
     * 删除
     */
    public void delete(Integer id);
}

1.2 write CustomerDao implementation class

package com.yiidian.dao.impl;

import com.yiidian.dao.CustomerDao;
import com.yiidian.domain.Customer;
import com.yiidian.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Dao实现类
 */
public class CustomerDaoImpl implements CustomerDao{
    @Override
    public List<Customer> findAll() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSession();
            return sqlSession.selectList("com.yiidian.dao.CustomerDao.findAll");
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            sqlSession.close();
        }
        return null;
    }

    @Override
    public void save(Customer customer) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSession();
            sqlSession.insert("com.yiidian.dao.CustomerDao.save", customer);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally{
            sqlSession.close();
        }
    }

    @Override
    public void update(Customer customer) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSession();
            sqlSession.update("com.yiidian.dao.CustomerDao.update", customer);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally{
            sqlSession.close();
        }
    }

    @Override
    public Customer findById(Integer id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSession();
            return sqlSession.selectOne("com.yiidian.dao.CustomerDao.findById",id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            sqlSession.close();
        }
        return null;
    }

    @Override
    public List<Customer> findByName(String name) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSession();
            return sqlSession.selectList("com.yiidian.dao.CustomerDao.findByName",name);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            sqlSession.close();
        }
        return null;
    }

    @Override
    public void delete(Integer id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSession();
            sqlSession.delete("com.yiidian.dao.CustomerDao.delete", id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally{
            sqlSession.close();
        }
    }
}

Focus on the traditional approach is that Dao implementation class, in Dao implementation class, manually invoke the method SqlSession provided direct execution of SQL statements in the mapped file.

1.3 write CustomerDao.xml map

<?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">
<!--
   namespace: 用于指定该映射文件需要映射的Dao接口
-->
<mapper namespace="com.yiidian.dao.CustomerDao">

    <!--查询所有-->
    <select id="findAll" resultType="com.yiidian.domain.Customer">
        select * from t_customer
    </select>

    <!--1.添加方法-->
    <insert id="save" parameterType="com.yiidian.domain.Customer">
        INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
    </insert>

    <!--2.修改方法-->
    <update id="update" parameterType="com.yiidian.domain.Customer">
        UPDATE t_customer SET
        NAME = #{name},
        gender = #{gender},
        telephone = #{telephone}
        WHERE id = #{id}
    </update>

    <!--查询一个-->
    <select id="findById" parameterType="integer" resultType="com.yiidian.domain.Customer">
        select * from t_customer where id = #{id}
    </select>

    <!--条件查询-->
    <select id="findByName" parameterType="string" resultType="com.yiidian.domain.Customer">
        select * from t_customer where name like #{name}
    </select>

    <!--删除-->
    <delete id="delete" parameterType="integer">
        delete from t_customer where id = #{id}
    </delete>
</mapper>

1.4 write test classes

package com.yiidian.mybatis;

import com.yiidian.dao.CustomerDao;
import com.yiidian.dao.impl.CustomerDaoImpl;
import com.yiidian.domain.Customer;
import com.yiidian.utils.MyBatisUtils;
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 org.junit.Test;

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

/**
 * MyBatis测试类 - 传统Dao写法
 * 一点教程网 - www.yiidian.com
 */
public class TestCustomerDao {

    /**
     * 添加
     */
    @Test
    public void testSave(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.创建传统Dao实现类对象
        CustomerDao customerDao = new CustomerDaoImpl();

        //3.调用save方法
        Customer customer = new Customer();
        customer.setName("小苍");
        customer.setGender("女");
        customer.setTelephone("15755556666");
        customerDao.save(customer);


        //4.关闭连接
        session.close();
    }

    /**
     * 修改
     */
    @Test
    public void testUpdate(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.创建传统Dao实现类对象
        CustomerDao customerDao = new CustomerDaoImpl();

        //3.调用update方法
        Customer customer = new Customer();
        customer.setId(5);
        customer.setName("小泽");
        customer.setGender("女");
        customer.setTelephone("15755556666");
        customerDao.update(customer);

        session.commit();

        //4.关闭连接
        session.close();
    }

    /**
     * 查询所有
     */
    @Test
    public void testFindAll(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.创建传统Dao实现类对象
        CustomerDao customerDao = new CustomerDaoImpl();

        //3.调用findAll方法
        List<Customer> list = customerDao.findAll();

        for(Customer cust:list){
            System.out.println(cust);
        }

        //4.关闭连接
        session.close();
    }

    /**
     * 查询一个
     */
    @Test
    public void testFindById(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.创建传统Dao实现类对象
        CustomerDao customerDao = new CustomerDaoImpl();

        //3.调用findById方法
        Customer customer = customerDao.findById(3);

        System.out.println(customer);

        //4.关闭连接
        session.close();
    }

    /**
     * 条件查询
     */
    @Test
    public void testFindByName(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.创建传统Dao实现类对象
        CustomerDao customerDao = new CustomerDaoImpl();

        //3.调用findByName方法
        List<Customer> list = customerDao.findByName("%小%");

        for(Customer cust:list){
            System.out.println(cust);
        }

        //4.关闭连接
        session.close();
    }

    /**
     * 删除
     */
    @Test
    public void testDelete(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.创建传统Dao实现类对象
        CustomerDao customerDao = new CustomerDaoImpl();

        //3.调用findByName方法
        customerDao.delete(5);

        // 提交事务
        session.commit();

        //4.关闭连接
        session.close();
    }
}

2 Mapper Proxy Interface

2.1 interface to write CustomerDao

package com.yiidian.dao;
import com.yiidian.domain.Customer;
import java.util.List;
/**
 * Dao接口
 *一点教程网 - www.yiidian.com
 */
public interface CustomerDao {

    /**
     * 查询所有用户
     */
    public List<Customer> findAll();

    /**
     * 添加
     */
    public void save(Customer customer);

    /**
     * 修改
     */
    public void update(Customer customer);

    /**
     * 查询一个
     */
    public Customer findById(Integer id);

    /**
     * 条件查询
     */
    public List<Customer> findByName(String name);

    /**
     * 删除
     */
    public void delete(Integer id);
}

2.2 write CustomerDao.xml map

<?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">
<!--
   namespace: 用于指定该映射文件需要映射的Dao接口
-->
<mapper namespace="com.yiidian.dao.CustomerDao">

    <!--查询所有-->
    <select id="findAll" resultType="com.yiidian.domain.Customer">
        select * from t_customer
    </select>

    <!--1.添加方法-->
    <insert id="save" parameterType="com.yiidian.domain.Customer">
        INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
    </insert>

    <!--2.修改方法-->
    <update id="update" parameterType="com.yiidian.domain.Customer">
        UPDATE t_customer SET
        NAME = #{name},
        gender = #{gender},
        telephone = #{telephone}
        WHERE id = #{id}
    </update>

    <!--查询一个-->
    <select id="findById" parameterType="integer" resultType="com.yiidian.domain.Customer">
        select * from t_customer where id = #{id}
    </select>

    <!--条件查询-->
    <select id="findByName" parameterType="string" resultType="com.yiidian.domain.Customer">
        select * from t_customer where name like #{name}
    </select>

    <!--删除-->
    <delete id="delete" parameterType="integer">
        delete from t_customer where id = #{id}
    </delete>
</mapper>

2.3 write test classes

package com.yiidian.mybatis;

import com.yiidian.dao.CustomerDao;
import com.yiidian.dao.impl.CustomerDaoImpl;
import com.yiidian.domain.Customer;
import com.yiidian.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * MyBatis测试类 - Mapper代理接口
 * 一点教程网 - www.yiidian.com
 */
public class TestCustomerDao2 {

    /**
     * 添加
     */
    @Test
    public void testSave(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.生成Dao代理对象
        CustomerDao customerDao = session.getMapper(CustomerDao.class);

        //3.调用save方法
        Customer customer = new Customer();
        customer.setName("小苍");
        customer.setGender("女");
        customer.setTelephone("15755556666");
        customerDao.save(customer);


        //4.关闭连接
        session.close();
    }

    /**
     * 修改
     */
    @Test
    public void testUpdate(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.生成Dao代理对象
        CustomerDao customerDao = session.getMapper(CustomerDao.class);

        //3.调用update方法
        Customer customer = new Customer();
        customer.setId(5);
        customer.setName("小泽");
        customer.setGender("女");
        customer.setTelephone("15755556666");
        customerDao.update(customer);

        session.commit();

        //4.关闭连接
        session.close();
    }

    /**
     * 查询所有
     */
    @Test
    public void testFindAll(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.生成Dao代理对象
        CustomerDao customerDao = session.getMapper(CustomerDao.class);

        //3.调用findAll方法
        List<Customer> list = customerDao.findAll();

        for(Customer cust:list){
            System.out.println(cust);
        }

        //4.关闭连接
        session.close();
    }

    /**
     * 查询一个
     */
    @Test
    public void testFindById(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.生成Dao代理对象
        CustomerDao customerDao = session.getMapper(CustomerDao.class);

        //3.调用findById方法
        Customer customer = customerDao.findById(5);

        System.out.println(customer);

        //4.关闭连接
        session.close();
    }

    /**
     * 条件查询
     */
    @Test
    public void testFindByName(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.生成Dao代理对象
        CustomerDao customerDao = session.getMapper(CustomerDao.class);

        //3.调用findByName方法
        List<Customer> list = customerDao.findByName("%小%");

        for(Customer cust:list){
            System.out.println(cust);
        }

        //4.关闭连接
        session.close();
    }

    /**
     * 删除
     */
    @Test
    public void testDelete(){
        //1.获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();

        //2.生成Dao代理对象
        CustomerDao customerDao = session.getMapper(CustomerDao.class);

        //3.调用findByName方法
        customerDao.delete(5);

        // 提交事务
        session.commit();

        //4.关闭连接
        session.close();
    }
}

Download Source: https://pan.baidu.com/s/1ZsM5CAu026zfF3wZEA-aFQ

file

Welcome to my attention that the public number :: tutorial. Get exclusive organize learning resources and push dry goods daily.
If you are interested in my tutorial series, you can focus on my website: yiidian.com

Guess you like

Origin www.cnblogs.com/yiidian/p/12590751.html