SpringBoot+MyBatis項目Dao層最簡單寫法


前言

DAO(Data Access Object) 是數據訪問層,說白了就是跟數據庫打交道的,而數據庫都有哪幾種操作呢?沒錯,就是增刪改查。這就意味着Dao層要提供增刪改查操作。
不知道大家是怎么寫Dao層的接口的。如果你沒有一個好的思路,那就看看我的思路吧。如果你有更好的思路,歡迎指正。

正文

1.每一個實體類對應一個Dao接口文件和一個mybatis文件

結構如下:
在這里插入圖片描述

2.UserDao采用統一寫法

Dao層只寫六個接口就能解決百分之九十的問題

User.java

package com.example.demo.entity;

public class User {
    private Long id;
    private String username;
    private String password;
    private String realname;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", realname='" + realname + '\'' +
                '}';
    }
}

UserDao.java

package com.example.demo.dao;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserDao {
    // 用於添加用戶
    int insertUser(User user);

    // 用於刪除用戶
    int deleteUser(Long userId);

    // 用於更新用戶
    int updateUser(User user);

    // 用於查詢用戶
    User getUser(Long userId);

    // 用於查詢用戶列表
    List<User> getUserList(@Param("userCondition") User userCondition,
                                @Param("rowIndex") int rowIndex,
                                @Param("pageSize") int pageSize);

    // 用於查詢用戶列表數量
    int getUserCount(@Param("userCondition") User userCondition);
}

UserDao.xml

<?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" >
<mapper namespace="com.example.demo.dao.UserDao">

    <insert id="insertUser" parameterType="com.example.demo.entity.User"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into
        tb_user(username,password,realname)
        values (#{username},#{password},#{realname})
    </insert>

    <delete id="deleteUser">
        delete from
        tb_user
        where id=#{id}
    </delete>

    <update id="updateUser" parameterType="com.example.demo.entity.User"
            keyProperty="id" useGeneratedKeys="true">
        update tb_user
        <set>
            <if test="username != null">username = #{username},</if>
            <if test="password != null">password = #{password},</if>
            <if test="realname != null">realname = #{realname}</if>
        </set>
        where id=#{id}
    </update>

    <select id="getUser" resultType="com.example.demo.entity.User" parameterType="Long">
         select
         u.id,
         u.username,
         u.password,
         u.realname
         from tb_user u
         where ur.id = #{id}
    </select>

    <select id="getUserList" resultType="com.example.demo.entity.User">
        select
        u.id,
        u.username,
        u.password,
        u.realname
        from tb_user u
        <where>
            <if test="userCondition != null and userCondition.username != null">
                and u.username LIKE concat('%',#{userCondition.username},'%')
            </if>
            <if test="userCondition != null and userCondition.realname != null">
                and u.realname LIKE concat('%',#{userCondition.realname},'%')
            </if>
        </where>
        limit #{rowIndex},#{pageSize};
    </select>

    <select id="getUserCount" resultType="int">
        select count(1) from tb_user u
        <where>
            <if test="userCondition != null and userCondition.username != null">
                and u.username LIKE concat('%',#{userCondition.username},'%')
            </if>
            <if test="userCondition != null and userCondition.realname != null">
                and u.realname LIKE concat('%',#{userCondition.realname},'%')
            </if>
        </where>
    </select>
</mapper>
3.使用方法

添加用戶

User user = new User();
user.setUsername("lauyon");
user.setRealname("lauyon");
user.setPassword("e10adc3949ba59abbe56e057f20f883e");
int insertCount = userDao.insertUser(user);   //返回添加數據的條數

刪除用戶

int deleteCount = userDao.deleteUser(1L);	//返回刪除用戶的個數

更新用戶

User user = new User();
user.setId(1L);         // 注意:與添加用戶不同
user.setUsername("lauyon2");
user.setRealname("lauyon2");
user.setPassword("pf2wzmefd3sfgh5dfs6sdf");
int count = userDao.updateUser(user);   //返回更新數據的條數

查詢用戶

User user = userDao.getUser(1L);	//返回用戶,參數為用戶Id

查詢用戶列表

int listCount = userDao.getUserCount(userCondition);	//返回給service層,用於封裝分頁對象
List<User> userList = userDao.getUserList(userCondition, (page - 1) * size, size);	//page:頁碼		size:每頁的數據數量

至此,已經列舉了基本的增刪改查接口。當然,還可以組合出其他接口,可以解決大部分實際問題。

如果這篇博客對你有用,點個贊再走唄~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM