mapper代理(十一)

來源:互聯網
上載者:User

標籤:

 

  原始 dao開發問題

1、dao介面實作類別方法中存在大量模板方法,設想能否將這些代碼提取出來,大大減輕程式員的工作量。

2、調用sqlsession方法時將statement的id寫入程式碼了

3、調用sqlsession方法時傳入的變數,由於sqlsession方法使用泛型,即使變數類型傳入錯誤(傳入的是泛型類),在編譯階段也不報錯,不利於程式員開發。

 mapper代理方法(程式員只需要mapper介面(相當 於dao介面))

 思路(mapper代理開發規範):

Mapper代理開發避免了原始的dao介面開發中實作類別的很多重複代碼,讓程式員減輕了工作量

Sql語句的刪除、更新、添加操作要進行會話的提交才會生效

程式員還需要編寫mapper.xml對應檔

程式員編寫mapper介面需要遵循一些開發規範,mybatis可以自動產生mapper介面實作類別代理對象。

開發規範:

1、在mapper.xml中namespace等於mapper介面地址

2、mapper.java介面中的方法名和mapper.xml中statement的id一致

(使用mapper代理對象,mybatis會自動根據介面地址和命名空間去尋找相對應的statement的id)

3、mapper.java介面中的方法輸入參數類型和mapper.xml中statement的parameterType指定的類型一致。

4、mapper.java介面中的方法傳回值類型和mapper.xml中statement的resultType指定的類型一致。

 

 代理對象內部調用selectOne或selectList

如果mapper方法返回單個pojo對象(非集合對象),代理對象內部通過selectOne查詢資料庫。

如果mapper方法返回集合對象,代理對象內部通過selectList查詢資料庫。

 

 mapper介面方法參數只能有一個是否影響系統 開發

mapper介面方法參數只能有一個,系統是否不利於擴充維護。

系統 架構中,dao層的代碼是被業務層公用的。

即使mapper介面只有一個參數,可以使用封裝類型的pojo滿足不同的業務方法的需求。

注意:持久層方法的參數可以封裝類型、map。。。,service方法中建議不要使用封裝類型(不利於業務層的可擴充)。

 

eclipse項目的結構

 

UserMapper.xml

 

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace命名空間,作用就死對sql進行分類化管理,理解sql隔離 注意:使用mapper代理方法開發,namespace有特殊重要的作用(剛開始學還不能理解) --><mapper namespace="ql.mybatis.mapper.UserMapper">    <!-- 在對應檔中配置很多sql語句-->    <!-- 需求:通過id查詢使用者表的記錄 -->    <!-- 通過select 執行資料庫的查詢    id:標識對應檔的sql    將sql語句封裝到mappedStatement對象中,所以將id稱為statement的id    parameterType:指定輸入參數的類型,這裡指定int型    #{}表示一個預留位置    #{id}:其中的id表示接受輸入的參數,參數名稱就是id,如果輸入的參數是簡單類型,#{}中的參數可以任意    resultType:指定sql輸出的結果的所映射的java物件類型,select指定restultType表示將單條記錄映射成java對象    -->    <select id="findUserById" parameterType="int" resultType="ql.mybatis.pojo.User">        select * from User where id=#{id}    </select>        <select id="findUserById2" parameterType="ql.mybatis.pojo.User" resultType="ql.mybatis.pojo.User">        select * from User where sex=#{sex} and address=#{address}     </select>        <!-- 根據使用者名稱稱模糊查詢使用者資訊,可能返回多條    resultType:指定就是單條記錄所映射的java對象 類型    ${}:表示拼接sql串,將接收到參數的內容不加任何修飾拼接在sql中。    使用${}拼接sql,引起 sql注入    ${value}:接收輸入 參數的內容,如果傳入類型是簡單類型,${}中只能使用value     -->    <select id="findUserByName" parameterType="String" resultType="ql.mybatis.pojo.User">        SELECT * FROM USER WHERE username LIKE ‘%${value}%‘    </select>    <!-- 添加使用者     parameterType:指定輸入 參數類型是pojo(包括 使用者資訊)    #{}中指定pojo的屬性名稱,接收到pojo對象的屬性值,mybatis通過OGNL擷取對象的屬性值    -->    <insert id="insertUser" parameterType="ql.mybatis.pojo.User">        insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})    </insert>        <!-- 刪除使用者 -->    <delete id="deleteUser" parameterType="java.lang.Integer">     delete from user where id=#{id}    </delete>        <!-- 更新使用者    必須指定id,否則將會將表的內容全部更新     -->    <update id="updateUser" parameterType="ql.mybatis.pojo.User">    update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}    </update></mapper>

 

UserMapper介面:

package ql.mybatis.mapper;import java.util.List;import ql.mybatis.pojo.User;public interface UserMapper {    public User findUserById(int id) throws Exception;    public List<User> findUserByName(String name) throws Exception;    public User findUserById2(User user) throws Exception;    public void insertUser(User user) throws Exception;    public void deleteUser(int id) throws Exception;}

 

單元測試類:

 

package ql.mybatis.test;import java.io.InputStream;import java.util.Date;import java.util.List;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.Before;import org.junit.Test;import ql.mybatis.mapper.UserMapper;import ql.mybatis.pojo.User;public class UserMapperTest {    SqlSession sqlSession = null;    @Before    public void setUp() throws Exception {        // mybatis設定檔        String resource = "SqlMapConfig.xml";        // 得到設定檔流        InputStream inputStream = Resources.getResourceAsStream(resource);        // 建立會話工廠,傳入mybatis的設定檔資訊        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()                .build(inputStream);        sqlSession = sqlSessionFactory.openSession();    }    @Test    public void testFindUserById() throws Exception {        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        User user = userMapper.findUserById(22);        System.out.println(user);    }    @Test    public void testFindUserByName() throws Exception {        // 得到代理對象,介面回調        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        List<User> list = userMapper.findUserByName("小明");        System.out.println(list);    }    @Test    public void testInsertUser() throws Exception {        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        User user = new User();        user.setSex("1");        user.setAddress("北京市");        user.setBirthday(new Date());        user.setUsername("王大東");        userMapper.insertUser(user);        sqlSession.commit();    }    @Test    public void testDeleteUser() throws Exception {        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        userMapper.deleteUser(10);        sqlSession.commit();    }    @Test    public void testFindUserById2() throws Exception {        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        User user=new User();        user.setSex("1");        user.setAddress("福建福州");        User user2=userMapper.findUserById2(user);        System.out.println(user2);    }}

 還應在設定檔中SqlMapConfig.xml加上:

<mapper resource="mapper/UserMapper.xml"/>

 

可以看到並沒有寫UserMapper介面的實作類別,mybatis幫我們產生了一個Mapper代理對象

 

mapper代理(十一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.