前言

DAO(Data Access Object) 是数据访问层,说白了就是跟数据库打交道的,而数据库都有哪几种操作呢?没错,就是增删改查。这就意味着Dao层要提供增删改查操作。

不知道大家是怎么写Dao层的接口的。如果你没有一个好的思路,那就看看我的思路吧。如果你有更好的思路,欢迎指正。

正文

1.每一个实体类对应一个Dao接口文件和一个mybatis文件

结构如下:

SpringBoot+MyBatis项目Dao层最简单写法-LMLPHP

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:每页的数据数量

至此,已经列举了基本的增删改查接口。当然,还可以组合出其他接口,可以解决大部分实际问题。

05-11 13:30