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

前言

DAO(Data Access Object) 是數據訪問層,說白了就是跟數據庫打交道的,而數據庫都有哪幾種操作呢?沒錯,就是

增刪改查。這就意味著Dao層要提供增刪改查操作。

不知道大家是怎麼寫Dao層的接口的。如果你沒有一個好的思路,那就看看我的思路吧。如果你有更好的思路,歡迎指正。

正文

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

結構如下:


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


2.UserDao採用統一寫法

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

User.java

<code>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 + '\\'' +
'}';
}
}/<code>

UserDao.java

<code>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);
}/<user>/<code>

UserDao.xml

<code>

<mapper>

<insert> useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into
tb_user(username,password,realname)
values (#{username},#{password},#{realname})
/<insert>

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

<update> keyProperty="id" useGeneratedKeys="true">
update tb_user

username = #{username},
password = #{password},
realname = #{realname}

where id=#{id}
/<update>

<select>
select
u.id,
u.username,
u.password,
u.realname

from tb_user u
where ur.id = #{id}
/<select>

<select>
select
u.id,
u.username,
u.password,
u.realname
from tb_user u
<where>

and u.username LIKE concat('%',#{userCondition.username},'%')


and u.realname LIKE concat('%',#{userCondition.realname},'%')

/<where>
limit #{rowIndex},#{pageSize};
/<select>

<select>
select count(1) from tb_user u
<where>

and u.username LIKE concat('%',#{userCondition.username},'%')


and u.realname LIKE concat('%',#{userCondition.realname},'%')

/<where>
/<select>
/<mapper>/<code>

3.使用方法

添加用戶

<code>User user = new User(); 

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

刪除用戶

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

更新用戶

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

查詢用戶

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

查詢用戶列表

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

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

吐槽:頭條這個代碼排版也是醉了

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


分享到:


相關文章: