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:每页的数据数量
至此,已经列举了基本的增删改查接口。当然,还可以组合出其他接口,可以解决大部分实际问题。
如果这篇博客对你有用,点个赞再走呗~
SpringBoot+MyBatis项目Dao层最简单写法的更多相关文章
- SpringBoot Mybatis项目中的多数据源支持
1.概述 有时项目里里需要抽取不同系统中的数据源,需要访问不同的数据库,本文介绍在Springboot+Mybatis项目中如何支持多数据源操作. 有需要的同学可以下载 示例代码 项目结构如下: 2. ...
- Mybatis的dao层实现 接口代理方式实现规范+plugins-PageHelper
Mybatis的dao层实现 接口代理方式实现规范 Mapper接口实现时的相关规范: Mapper接口开发只需要程序员编写Mapper接口而不用具体实现其代码(相当于我们写的Imp实现类) Mapp ...
- Mybatis的Dao层实现原理
1.Mybatis的Dao层实现 1.1 传统开发方式 1.1.1编写UserDao接口 public interface UserDao { List<User> findAll() t ...
- 基于Mybatis的Dao层的开发
基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建SqlSessionFacoty,SqlSessionFacoty一旦创建完成就不需要SqlSessionFa ...
- MyBatis开发Dao层的两种方式(原始Dao层开发)
本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...
- MyBatis开发Dao层的两种方式(Mapper动态代理方式)
MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...
- 基于Mybatis的Dao层开发
转自:https://www.cnblogs.com/rodge-run/p/6528398.html 基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建 Sq ...
- IDEA项目搭建四——使用Mybatis实现Dao层
一.引入mybatis及mysql的jar包 可以从阿里云上面查找版本,db操作放在dao层所以打开该层的pom.xml文件,找到<dependencies>节点增加两个引入 <de ...
- 零基础IDEA整合SpringBoot + Mybatis项目,及常见问题详细解答
开发环境介绍:IDEA + maven + springboot2.1.4 1.用IDEA搭建SpringBoot项目:File - New - Project - Spring Initializr ...
随机推荐
- JSON 文件的存取
import json data = {'Tom': {'Weight:': 65, 'Score': 90, 'Height': 170}} # json.dumps 将字典转化为 JSON 编码的 ...
- 007 Ceph手动部署单节点
前面已经介绍了Ceph的自动部署,本次介绍一下关于手动部署Ceph节点操作 一.环境准备 一台虚拟机部署单节点Ceph集群 IP:172.25.250.14 内核: Red Hat Enterpris ...
- Kerrigan:配置中心管理UI的实现思路和技术细节
去年写过一篇文章『中小团队落地配置中心详解』,介绍了我们借助etcd+confd实现的配置中心方案,这是一个对运维友好,与开发解耦的极佳方案,经过了一年多的实践也确实帮我们解决了配置文件无版本.难回滚 ...
- 洛谷$P$1486 郁闷的出纳员 $[NOI2004]$ $splay$
正解:$splay$ 解题报告: 传送门! 依然先考虑要呲呲些什么操作鸭$QwQ$ 其实就只要一个删除区间,一个查询第$k$大,还一个插入就欧克? 删除区间的话直接旋转下根什么的然后直接把子树删了就好 ...
- $CH$ $0x50$ & $0x51$ 做题记录
[X]$Mr.Young's\ Picture\ Permutations$ 前面这儿写了挺多道辣,,,懒得写辣$QAQ$ (后面所有同上都是同这个$QwQ$ [X]$LCIS$ 做过了,看这儿 $u ...
- BridgePattern(桥接模式)-----Java/.Net
桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化.这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦
- 认识Web应用框架
Web应用框架 Web应用框架(Web application framework)是一种开发框架,用来支持动态网站.网络应用程序及网络服务的开发.类型可以分为基于请求(request-based)的 ...
- [UWP]XAML中的响应式布局技术
响应式布局的概念是一个页面适配多个终端及不同分辨率.在针对特定屏幕宽度优化应用 UI 时,我们将此称为创建响应式设计.WPF设计之初响应式设计的概念并不流行,那时候大部分网页设计师都按着宽度960像素 ...
- CF1200D White Lines | 前缀和
传送门 Examples input 1 4 2 BWWW WBBW WBBW WWWB output 1 4 input 2 3 1 BWB WWB BWB output 2 2 input 3 5 ...
- 《C++Primer》第五版习题答案--第二章【学习笔记】
C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...