Mybatis的dao層實現 介面代理方式實現規範+plugins-PageHelper

Mybatis的dao層實現 介面代理方式實現規範

Mapper介面實現時的相關規範:

Mapper介面開發只需要程式設計師編寫Mapper介面而不用具體實現其程式碼(相當於我們寫的Imp實現類)

Mapper介面實現時的相關規範:

1.Mapper.xml文件中的namespace與mapper介面的全限定名要相同
2.Mapper.xml文件定義的每個statement的id需要和介面的方法名相同
3.Mapper介面方法的輸入參數類型和Mapper.xml中定義的每個sql的parameterType的類型相同
4.Mapper介面方法中的輸出參數類型和mapper.xml中定義的每個sql的resultType的類型相同

簡單演示

編寫介面

package Interface;

import domain.User;

public interface UserDao {
    User findById(int id );
}

配置配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "//mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Interface.UserDao">
    <select id="findById" parameterType="int">
        select * from test where id=#{id}
    </select>
</mapper>

在這裡插入圖片描述
獲取方式

package Test;

import Interface.UserDao;
import domain.User;
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 java.io.IOException;
import java.io.InputStream;

public class test3 {
    public static void main(String[] args) throws IOException {
      //載入核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //獲取sqlSession工廠類對象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //獲取session對象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //執行sql語句
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.findById(1);
        System.out.println(user);
    }
}

在這裡插入圖片描述
在這裡插入圖片描述

再實際的開發中我們的sql語句往往是動態變化的,下面我們來介紹一下動態sql語句

在這裡插入圖片描述
我們根據實體類的不同取值來使用不同的sql語句進行查詢,比如在id不為空的時候可以根據id進行查詢,如果username不為空的時候加入,username一同進行查詢,也就是組合查詢

在這裡使用語句就可以實現
if用於判斷是否為空,不為空則加入查詢語句中

<select id="findByCondition" parameterType="user" resultType="user">
        select  * from test
        <where>
            <if test="id!=0">
                and id=#{id}
            </if>
            <if test="username!=null">
                and username=#{username}
            </if>
        </where>
    </select>

當我們使用sql語句的時候可能會有拼接操作,比如:SELECT * FROM USER WHERE id IN (1,2,5)。
這裡需要將數據與括弧拼接起來,那麼下面我們來講講怎麼使用

的使用

foreach標籤的屬性含義如下:
標籤用於遍歷集合,它的屬性:
collection:代表要遍歷的集合元素,注意編寫時不要寫#{}
open:代表語句的開始部分
close:代表結束部分
item:代表遍歷集合的每個元素,生成的變數名
sperator:代表分隔符

<select id="findByIds" parameterType="list" resultType="user">
       select * from test
       <where>
           <foreach collection="list" item="id" open="id in ("  separator="," close=")">
               #{id}
           </foreach>
       </where>
    </select>

sql片段的抽取

在我們使用sql語句的時候,sql語句中的許多內容也是重複的,所以我們可以把相同的sql語句抽取出來
中可以將重複的sql題取出來,以達到sql重用的目的

程式碼演示

<sql id="selectUser">
          select * from test
    </sql>
    <select id="findById" parameterType="int" resultType="user">
       <include refid="selectUser"></include> where id=#{id}
    </select>
    <select id="findByCondition" parameterType="user" resultType="user">
        <include refid="selectUser"></include>
        <where>
            <if test="id!=0">
                and id=#{id}
            </if>
            <if test="username!=null">
                and username=#{username}
            </if>
        </where>
    </select>

Mybatis映射文件配置

Tags: