1.为什么要简化dao层
随着我们的项目越来越大,我们的dao层会出现很多的dao层的代码冗余,项目看起来比较臃肿。如何解决这个问题。这里我们可以参考mybatis-plus中的mapper层的定义,他这里的BaseMapper<T> 其中User 对象 就是传入其中的泛型 通过User传入的泛型可以实现一些最基本的方法也就是我们常说的增删改查方法。直接调用mybatis-plus中的方法即可,如何有别的方法在去mapper去写即可如图下:
mybatis-plus代码示例
@Repository //告诉容器你是持久层的 @Repository是spring提供的注释,能够将该类注册成Bean
public interface UserMapper extends BaseMapper<User> {
//所有的crud都编写完成了
}
2.mybatis如何实现上述方法
步诹2.1:
/**
提取公共方法,自定义公共接口
**/
public interface BasicDAO<T> {
/**
* 根据条件查询
*
* @param id 查询条件
* @return 当前对象
*/
T queryById(Integer id);
/**
* 统计总行数
*
* @param t 查询条件
* @return 总行数
*/
long count(T t);
/**
* 新增数据
*
* @param t 实例对象
* @return 影响行数
*/
int insert(T t);
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<T> t 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<T> entities);
/**
* 修改数据
*
* @param t 实例对象
* @return 影响行数
*/
int update(T t);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
int deleteById(Integer id);
}
步诹2.2:
/**
* (Log)表数据库访问层
*
* @author make1java
* @since 2023-02-08 18:23:13
*/
public interface LogDao extends BasicDAO<Log> {
/**
* 查询指定行数据
*
* @param log 查询条件
* @param pageable 分页对象
* @return 对象列表
*/
/* List<Log> queryAllByLimit(Log log, @Param("pageable") Pageable pageable);*/
}
步诹2.3:编写mapper
<?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.baizhi.dao.LogDao">
<resultMap type="com.baizhi.entity.Log" id="LogMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<result property="date" column="date" jdbcType="TIMESTAMP"/>
<result property="operation" column="operation" jdbcType="VARCHAR"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="LogMap">
select
id, username, date, operation
from log
where id = #{id}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="LogMap">
select
id, username, date, operation
from log
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="date != null">
and date = #{date}
</if>
<if test="operation != null and operation != ''">
and operation = #{operation}
</if>
</where>
limit #{pageable.offset}, #{pageable.pageSize}
</select>
<!--统计总行数-->
<select id="count" resultType="java.lang.Long">
select count(1)
from log
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="date != null">
and date = #{date}
</if>
<if test="operation != null and operation != ''">
and operation = #{operation}
</if>
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into log(username, date, operation)
values (#{username}, #{date}, #{operation})
</insert>
<insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
insert into log(username, date, operation)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.username}, #{entity.date}, #{entity.operation})
</foreach>
</insert>
<insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
insert into log(username, date, operation)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.username}, #{entity.date}, #{entity.operation})
</foreach>
on duplicate key update
username = values(username),
date = values(date),
operation = values(operation)
</insert>
<!--通过主键修改数据-->
<update id="update">
update log
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="date != null">
date = #{date},
</if>
<if test="operation != null and operation != ''">
operation = #{operation},
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete from log where id = #{id}
</delete>
</mapper>
3.测试并验证
import com.baizhi.dao.LogDao;
import com.baizhi.entity.Log;
import com.baizhi.service.LogService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Date;
@SpringBootTest
public class SpringbootJasyptDemoApplicationTests {
private static final Logger log1 = LoggerFactory.getLogger(SpringbootJasyptDemoApplicationTests.class);
@Autowired
LogService logService;
private final LogDao logDao;
@Autowired
SpringbootJasyptDemoApplicationTests(LogDao logDao) {
this.logDao = logDao;
}
@Test
void contextLoads() {
ArrayList<Log> logs = new ArrayList<>();
logs.add(new Log(null,"admin",new Date(),"111"));
logs.add(new Log(null,"admin",new Date(),"333"));
logDao.insertBatch(logs);
long count = logDao.count(new Log());
System.out.println(count);
}