zoukankan      html  css  js  c++  java
  • Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)

    1.原始方法开发Dao

     Dao接口

     1 package cn.sm1234.dao;
     2 
     3 import java.util.List;
     4 
     5 import cn.sm1234.domain.Customer;
     6 
     7 public interface CustomerDao {
     8 
     9     public void saveCustomer(Customer customer);
    10     
    11     public void updateCustomer(Customer customer);
    12     
    13     public void deleteCustomer(Integer id);
    14     
    15     public List<Customer> queryAllCustomer();
    16     
    17     public Customer queryCustomerById(Integer id);
    18     
    19     public List<Customer> queryCustomerByName(String name);
    20 }

    Dao实现:

      1 package cn.sm1234.dao.impl;
      2 
      3 import java.util.List;
      4 
      5 import org.apache.ibatis.session.SqlSession;
      6 
      7 import cn.sm1234.dao.CustomerDao;
      8 import cn.sm1234.domain.Customer;
      9 import cn.sm1234.utils.SessionUtils;
     10 
     11 public class CustomerDaoImpl implements CustomerDao {
     12 
     13     @Override
     14     public void saveCustomer(Customer customer) {
     15         SqlSession sqlSession = null;
     16         try {
     17             sqlSession = SessionUtils.getSesion();
     18             sqlSession.insert("insertCustomer", customer);
     19             sqlSession.commit();
     20         } catch (Exception e) {
     21             e.printStackTrace();
     22             sqlSession.rollback();
     23         } finally {
     24             sqlSession.close();
     25         }
     26     }
     27 
     28     @Override
     29     public void updateCustomer(Customer customer) {
     30         SqlSession sqlSession = null;
     31         try {
     32             sqlSession = SessionUtils.getSesion();
     33             sqlSession.update("updateCustomer", customer);
     34             sqlSession.commit();
     35         } catch (Exception e) {
     36             e.printStackTrace();
     37             sqlSession.rollback();
     38         } finally {
     39             sqlSession.close();
     40         }
     41 
     42     }
     43 
     44     @Override
     45     public void deleteCustomer(Integer id) {
     46         SqlSession sqlSession = null;
     47         try {
     48             sqlSession = SessionUtils.getSesion();
     49             sqlSession.delete("deleteCustomer", id);
     50             sqlSession.commit();
     51         } catch (Exception e) {
     52             e.printStackTrace();
     53             sqlSession.rollback();
     54         } finally {
     55             sqlSession.close();
     56         }
     57 
     58     }
     59 
     60     @Override
     61     public List<Customer> queryAllCustomer() {
     62         SqlSession sqlSession = null;
     63         try {
     64             sqlSession = SessionUtils.getSesion();
     65             return sqlSession.selectList("queryAllCustomer");
     66         } catch (Exception e) {
     67             e.printStackTrace();
     68         } finally {
     69             sqlSession.close();
     70         }
     71         return null;
     72     }
     73 
     74     @Override
     75     public Customer queryCustomerById(Integer id) {
     76         SqlSession sqlSession = null;
     77         try {
     78             sqlSession = SessionUtils.getSesion();
     79             return sqlSession.selectOne("queryCustomerById", id);
     80         } catch (Exception e) {
     81             e.printStackTrace();
     82         } finally {
     83             sqlSession.close();
     84         }
     85         return null;
     86     }
     87 
     88     @Override
     89     public List<Customer> queryCustomerByName(String name) {
     90         SqlSession sqlSession = null;
     91         try {
     92             sqlSession = SessionUtils.getSesion();
     93             return sqlSession.selectList("queryCustomerById", name);
     94         } catch (Exception e) {
     95             e.printStackTrace();
     96         } finally {
     97             sqlSession.close();
     98         }
     99         return null;
    100     }
    101 
    102 }

    测试类:

    package cn.sm1234.test;
    
    import java.util.List;
    
    import org.junit.Test;
    
    import cn.sm1234.dao.CustomerDao;
    import cn.sm1234.dao.impl.CustomerDaoImpl;
    import cn.sm1234.domain.Customer;
    
    public class Demo1 {
    
        @Test
        public void test1(){
            Customer c = new Customer();
            c.setName("陈六222");
            c.setGender("男");
            c.setTelephone("13244445555");
            
            CustomerDao dao = new CustomerDaoImpl();
            dao.saveCustomer(c);
        }
        
        @Test
        public void test2(){
            Customer c = new Customer();
            c.setId(1);
            c.setName("李四");
            
            CustomerDao dao = new CustomerDaoImpl();
            dao.updateCustomer(c);
        }
        
        @Test
        public void test3(){
            CustomerDao dao = new CustomerDaoImpl();
            dao.deleteCustomer(14);
        }
        
        @Test
        public void test4(){
            CustomerDao dao = new CustomerDaoImpl();
            List<Customer> list = dao.queryAllCustomer();
            for (Customer customer : list) {
                System.out.println(customer);
            }
        }
        
        @Test
        public void test5(){
            CustomerDao dao = new CustomerDaoImpl();
            Customer customer = dao.queryCustomerById(1);
            System.out.println(customer);
        }
        
        @Test
        public void test6(){
            CustomerDao dao = new CustomerDaoImpl();
            List<Customer> list = dao.queryCustomerByName("%陈%");
            for (Customer customer : list) {
                System.out.println(customer);
            }
        }
    }

    Customer.xml:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper
     3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 <!-- 该文件存放CRUD的sql语句 -->
     6 <mapper namespace="test">
     7     <!-- 添加 -->
     8     <insert id="insertCustomer" parameterType="customer">
     9         INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
    10     </insert>
    11 
    12     <!-- 修改 -->    
    13     <!-- parameterType传入对象,包含需要使用的值 -->
    14     <update id="updateCustomer" parameterType="customer">
    15         UPDATE t_customer SET NAME = #{name} WHERE id = #{id}
    16     </update>
    17     
    18     <!-- 查询所有数据 -->
    19     <!-- 输出映射 resultType -->
    20     <!-- parameterType可以省略,resultType不可以省略 -->
    21     <select id="queryAllCustomer" resultType="customer">
    22         SELECT * FROM t_customer
    23     </select>
    24     
    25     <!-- 根据id查询 -->
    26     <select id="queryCustomerById" parameterType="_int" resultType="customer">
    27         SELECT * FROM t_customer WHERE id=#{value}
    28     </select>
    29     
    30     <!-- 根据name模糊查询 -->
    31     <select id="queryCustomerByName" parameterType="string" resultType="customer">
    32         <!-- 方法一 -->
    33         SELECT * FROM t_customer WHERE NAME LIKE #{value}
    34         <!-- 方法二 -->
    35         <!-- SELECT * FROM t_customer WHERE NAME LIKE '%${value}%' -->
    36     </select>
    37     
    38     <!-- 删除 -->
    39     <delete id="deleteCustomer" parameterType="int">
    40         DELETE FROM t_customer WHERE id=#{value}
    41     </delete>    
    42 
    43 </mapper>

    缺点:代码繁琐。

    解决方法:利用动态代理方式Dao接口开发。Dao层只需要接口,不需要重复的Dao层实现。

    2.动态代理方式开发Dao层(推荐使用)

    好处:无需再去编写Dao层的实现类。

    Dao接口:

     1 package cn.sm1234.dao;
     2 
     3 import java.util.List;
     4 
     5 import cn.sm1234.domain.Customer;
     6 
     7 public interface CustomerDao {
     8 
     9     public void saveCustomer(Customer customer);
    10     
    11     public void updateCustomer(Customer customer);
    12     
    13     public void deleteCustomer(Integer id);
    14     
    15     public List<Customer> queryAllCustomer();
    16     
    17     public Customer queryCustomerById(Integer id);
    18     
    19     public List<Customer> queryCustomerByName(String name);
    20 }

    Customer.xml:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper
     3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 <!-- 该文件存放CRUD的sql语句 -->
     6 <!-- 
     7     如果是动态代理方式
     8       1)namespace必须和Dao接口的路径保持一致
     9       2)Dao接口里面的方法和sql语句的id保持一致
    10       3)Dao接口的方法的参数和返回值类型 和 映射文件的parameterType和resultType要对应
    11  -->
    12 <mapper namespace="cn.sm1234.dao.CustomerDao">
    13     <!-- 添加 -->
    14     <insert id="saveCustomer" parameterType="customer">
    15         INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
    16     </insert>
    17 
    18     <!-- 修改 -->    
    19     <!-- parameterType传入对象,包含需要使用的值 -->
    20     <update id="updateCustomer" parameterType="customer">
    21         UPDATE t_customer SET NAME = #{name} WHERE id = #{id}
    22     </update>
    23     
    24     <!-- 查询所有数据 -->
    25     <!-- 输出映射 resultType -->
    26     <!-- parameterType可以省略,resultType不可以省略 -->
    27     <select id="queryAllCustomer" resultType="customer">
    28         SELECT * FROM t_customer
    29     </select>
    30     
    31     <!-- 根据id查询 -->
    32     <select id="queryCustomerById" parameterType="_int" resultType="customer">
    33         SELECT * FROM t_customer WHERE id=#{value}
    34     </select>
    35     
    36     <!-- 根据name模糊查询 -->
    37     <select id="queryCustomerByName" parameterType="string" resultType="customer">
    38         <!-- 方法一 -->
    39         SELECT * FROM t_customer WHERE NAME LIKE #{value}
    40         <!-- 方法二 -->
    41         <!-- SELECT * FROM t_customer WHERE NAME LIKE '%${value}%' -->
    42     </select>
    43     
    44     <!-- 删除 -->
    45     <delete id="deleteCustomer" parameterType="int">
    46         DELETE FROM t_customer WHERE id=#{value}
    47     </delete>    
    48 
    49 </mapper>

    测试代码:

     1 package cn.sm1234.test;
     2 
     3 import java.util.List;
     4 
     5 import org.apache.ibatis.session.SqlSession;
     6 import org.junit.Test;
     7 
     8 import cn.sm1234.dao.CustomerDao;
     9 import cn.sm1234.domain.Customer;
    10 import cn.sm1234.utils.SessionUtils;
    11 
    12 public class Demo2 {
    13 
    14     @Test
    15     public void test1(){
    16         Customer c = new Customer();
    17         c.setName("陈六333");
    18         c.setGender("男");
    19         c.setTelephone("13244445555");
    20         
    21         SqlSession sqlSession = SessionUtils.getSession();
    22         //getMapper(): 返回指定接口的动态代理的实现类对象
    23         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
    24         dao.saveCustomer(c);
    25         sqlSession.commit();
    26         sqlSession.close();
    27     }
    28     
    29     @Test
    30     public void test2(){
    31         Customer c = new Customer();
    32         c.setId(1);
    33         c.setName("李四222");
    34         
    35         SqlSession sqlSession = SessionUtils.getSession();
    36         //getMapper(): 返回指定接口的动态代理的实现类对象
    37         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
    38         dao.updateCustomer(c);
    39         sqlSession.commit();
    40         sqlSession.close();
    41     }
    42     
    43     @Test
    44     public void test3(){
    45         SqlSession sqlSession = SessionUtils.getSession();
    46         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
    47         dao.deleteCustomer(19);
    48         sqlSession.commit();
    49         sqlSession.close();
    50     }
    51     
    52     @Test
    53     public void test4(){
    54         SqlSession sqlSession = SessionUtils.getSession();
    55         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
    56         List<Customer> list = dao.queryAllCustomer();
    57         for (Customer customer : list) {
    58             System.out.println(customer);
    59         }
    60     }
    61     
    62     @Test
    63     public void test5(){
    64         SqlSession sqlSession = SessionUtils.getSession();
    65         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
    66         Customer customer = dao.queryCustomerById(1);
    67         System.out.println(customer);
    68     }
    69     
    70     @Test
    71     public void test6(){
    72         SqlSession sqlSession = SessionUtils.getSession();
    73         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
    74         List<Customer> list = dao.queryCustomerByName("%陈%");
    75         for (Customer customer : list) {
    76             System.out.println(customer);
    77         }
    78     }
    79 }

    要点总结:

    如果是动态代理方式
    1)namespace必须和Dao接口的路径保持一致
    2)Dao接口里面的方法和sql语句的id保持一致
    3) Dao接口的方法的参数和返回值类型 和 映射文件的parameterType和resultType要对应

  • 相关阅读:
    win10安装vue
    通过docker安装rabbitmq
    python定时访问主机进程信息并反馈到远程服务器
    win10配置Apache+部署静态html页面
    HTTP协议—HTTP报文格式详解
    python脚本编写(纯干货)
    linux/CentOS的安装(萌新版)
    gradle中调用ant.unzip对zip包解压缩
    通过gradle运行测试脚本
    使用rest-assured测试rest service, 遇到“415 Unsupported Media Type”
  • 原文地址:https://www.cnblogs.com/116970u/p/10158956.html
Copyright © 2011-2022 走看看