1. 程式人生 > >Mybatis進階學習筆記——動態代理方式開發Dao介面、Dao層(推薦第二種)

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要對應