基于Mybatis的通用Service层实现

近日做项目,表很多,不想一一写CRUD操作,所以整理了下实现方法,mybatis框架,具体配置此处不提,只说核心思想,有兴趣的欢迎一起讨论。

=================以下例子均用User为范例==============

1.实体类

public class User {
    private Integer id;

    private String username;

    private String mobile;
    //其他属性……get/set方法省略

}

2.dao层(此处命名为mapper后缀)


BaseMapper.java文件,为dao层所有父类接口:

public interface BaseMapper<T,ID extends Serializable> {
	int deleteByPrimaryKey(ID id);

	int insert(T record);

	int insertSelective(T record);

	T selectByPrimaryKey(ID id);

	int updateByPrimaryKeySelective(T record);

	int updateByPrimaryKey(T record);
	
	List<T> selectAll();
}

参数说明:T为实现类对应类型,ID为主键类型,具体方法不再具体介绍


UserMapper.java,BaseMapper的子接口,此处添加三个新方法

public interface UserMapper extends BaseMapper<User, Integer>{

    User selectByMobile(String mobile);
    
    int updateByMobileSelective(User user);
    
    List<User> selectStudents();
}

UserMapper.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.mingxu.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.mingxu.entity.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="mobile" jdbcType="VARCHAR" property="mobile" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="head_img" jdbcType="VARCHAR" property="headImg" />
    <result column="open_id" jdbcType="VARCHAR" property="openId" />
    <result column="id_bind" jdbcType="CHAR" property="idBind" />
    <result column="insert_time" jdbcType="TIMESTAMP" property="insertTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="real_name" jdbcType="VARCHAR" property="realName" />
    <result column="user_role" jdbcType="CHAR" property="userRole" />
    <result column="is_trained" jdbcType="CHAR" property="isTrained" />
    <result column="gender" jdbcType="CHAR" property="gender" />
    <result column="grade" jdbcType="VARCHAR" property="grade" />
    <result column="major" jdbcType="VARCHAR" property="major" />
    <result column="id_card" jdbcType="VARCHAR" property="idCard" />
    <result column="email" jdbcType="VARCHAR" property="email" />
  </resultMap>
  <sql id="Base_Column_List">
    id, username, mobile, password, head_img, open_id, id_bind, insert_time, update_time, 
    real_name, user_role, is_trained, gender, grade, major, id_card, email
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.mingxu.entity.User">
    insert into user (id, username, mobile, 
      password, head_img, open_id, 
      id_bind, insert_time, update_time, 
      real_name, user_role, is_trained, 
      gender, grade, major, 
      id_card, email)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, 
      #{password,jdbcType=VARCHAR}, #{headImg,jdbcType=VARCHAR}, #{openId,jdbcType=VARCHAR}, 
      #{idBind,jdbcType=CHAR}, #{insertTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{realName,jdbcType=VARCHAR}, #{userRole,jdbcType=CHAR}, #{isTrained,jdbcType=CHAR}, 
      #{gender,jdbcType=CHAR}, #{grade,jdbcType=VARCHAR}, #{major,jdbcType=VARCHAR}, 
      #{idCard,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.mingxu.entity.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="mobile != null">
        mobile,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="headImg != null">
        head_img,
      </if>
      <if test="openId != null">
        open_id,
      </if>
      <if test="idBind != null">
        id_bind,
      </if>
      <if test="insertTime != null">
        insert_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="realName != null">
        real_name,
      </if>
      <if test="userRole != null">
        user_role,
      </if>
      <if test="isTrained != null">
        is_trained,
      </if>
      <if test="gender != null">
        gender,
      </if>
      <if test="grade != null">
        grade,
      </if>
      <if test="major != null">
        major,
      </if>
      <if test="idCard != null">
        id_card,
      </if>
      <if test="email != null">
        email,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="headImg != null">
        #{headImg,jdbcType=VARCHAR},
      </if>
      <if test="openId != null">
        #{openId,jdbcType=VARCHAR},
      </if>
      <if test="idBind != null">
        #{idBind,jdbcType=CHAR},
      </if>
      <if test="insertTime != null">
        #{insertTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="realName != null">
        #{realName,jdbcType=VARCHAR},
      </if>
      <if test="userRole != null">
        #{userRole,jdbcType=CHAR},
      </if>
      <if test="isTrained != null">
        #{isTrained,jdbcType=CHAR},
      </if>
      <if test="gender != null">
        #{gender,jdbcType=CHAR},
      </if>
      <if test="grade != null">
        #{grade,jdbcType=VARCHAR},
      </if>
      <if test="major != null">
        #{major,jdbcType=VARCHAR},
      </if>
      <if test="idCard != null">
        #{idCard,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.mingxu.entity.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
        mobile = #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="headImg != null">
        head_img = #{headImg,jdbcType=VARCHAR},
      </if>
      <if test="openId != null">
        open_id = #{openId,jdbcType=VARCHAR},
      </if>
      <if test="idBind != null">
        id_bind = #{idBind,jdbcType=CHAR},
      </if>
      <if test="insertTime != null">
        insert_time = #{insertTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="realName != null">
        real_name = #{realName,jdbcType=VARCHAR},
      </if>
      <if test="userRole != null">
        user_role = #{userRole,jdbcType=CHAR},
      </if>
      <if test="isTrained != null">
        is_trained = #{isTrained,jdbcType=CHAR},
      </if>
      <if test="gender != null">
        gender = #{gender,jdbcType=CHAR},
      </if>
      <if test="grade != null">
        grade = #{grade,jdbcType=VARCHAR},
      </if>
      <if test="major != null">
        major = #{major,jdbcType=VARCHAR},
      </if>
      <if test="idCard != null">
        id_card = #{idCard,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.mingxu.entity.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      mobile = #{mobile,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      head_img = #{headImg,jdbcType=VARCHAR},
      open_id = #{openId,jdbcType=VARCHAR},
      id_bind = #{idBind,jdbcType=CHAR},
      insert_time = #{insertTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      real_name = #{realName,jdbcType=VARCHAR},
      user_role = #{userRole,jdbcType=CHAR},
      is_trained = #{isTrained,jdbcType=CHAR},
      gender = #{gender,jdbcType=CHAR},
      grade = #{grade,jdbcType=VARCHAR},
      major = #{major,jdbcType=VARCHAR},
      id_card = #{idCard,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>


  <select id="selectByMobile" parameterType="String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where mobile = #{mobile,jdbcType=VARCHAR}
  </select>
  <update id="updateByMobileSelective" parameterType="com.mingxu.entity.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="headImg != null">
        head_img = #{headImg,jdbcType=VARCHAR},
      </if>
      <if test="openId != null">
        open_id = #{openId,jdbcType=VARCHAR},
      </if>
      <if test="idBind != null">
        id_bind = #{idBind,jdbcType=CHAR},
      </if>
      <if test="insertTime != null">
        insert_time = #{insertTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="realName != null">
        real_name = #{realName,jdbcType=VARCHAR},
      </if>
      <if test="userRole != null">
        user_role = #{userRole,jdbcType=CHAR},
      </if>
      <if test="isTrained != null">
        is_trained = #{isTrained,jdbcType=CHAR},
      </if>
      <if test="gender != null">
        gender = #{gender,jdbcType=CHAR},
      </if>
      <if test="grade != null">
        grade = #{grade,jdbcType=VARCHAR},
      </if>
      <if test="major != null">
        major = #{major,jdbcType=VARCHAR},
      </if>
      <if test="idCard != null">
        id_card = #{idCard,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
    </set>
    where mobile = #{mobile,jdbcType=VARCHAR}
  </update>
  <select id="selectStudents" parameterType="String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where user_role='1' and is_trained='1'
    ORDER BY insert_time DESC
  </select>
</mapper>

3.service层

3.1接口定义

BaseService.java,此处与dao层统一

public interface BaseService<T,ID extends Serializable> {
	
	boolean removeByPrimaryKey(ID id);

	boolean add(T record);

	boolean addSelective(T record);

	T findByPrimaryKey(ID id);

	boolean saveByPrimaryKeySelective(T record);

	boolean saveByPrimaryKey(T record);
	
	List<T> findAll();
	
}
UserService.java

public interface UserService extends BaseService<Dept, Integer> {

}
3.2接口实现

BaseServiceImpl.java这个类很重要,是数据库操作的实现

@Service
public abstract class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID> {

	public abstract BaseMapper<T, ID> getMapper();

	@Override
	public boolean removeByPrimaryKey(ID id) {

		return getMapper().deleteByPrimaryKey(id) > 0;
	}

	@Override
	public boolean add(T record) {
		return getMapper().insert(record) > 0;
	}

	@Override
	public boolean addSelective(T record) {
		return getMapper().insertSelective(record) > 0;
	}

	@Override
	public T findByPrimaryKey(ID id) {
		return getMapper().selectByPrimaryKey(id);
	}

	@Override
	public boolean saveByPrimaryKeySelective(T record) {

		return getMapper().updateByPrimaryKeySelective(record) > 0;
	}

	@Override
	public boolean saveByPrimaryKey(T record) {
		return getMapper().updateByPrimaryKey(record) > 0;
	}

	@Override
	public List<T> findAll() {
		return getMapper().selectAll();
	}

}
UserServiceImpl.java

@Service
public class UserServiceImpl extends BaseServiceImpl<Archives, Integer> implements ArchivesService{

	@Resource
	private UserMapper mapper;
	
	@Override
	public BaseMapper<Archives, Integer> getMapper() {
		return mapper;
	}

}


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 特性 无侵入:Mybatis-Plus 在 Mybatis 的基础上进行扩展,只做增强不做改变,引入 Mybatis-Plus 不会对您现有的 Mybatis 构架产生任何影响,而且 MP 支持所有 Mybatis 原生的特性 依赖少:仅仅依赖 Mybatis 以及 Mybatis-Spring 损耗小:启动即会自动注入基本CURD,性能基本无损耗,直接面向对象操作 预防Sql注入:内置Sql注入剥离器,有效预防Sql注入攻击 通用CRUD操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 多种主键策略:支持多达4种主键策略(内含分布式唯一ID生成器),可自由配置,完美解决主键问题 支持ActiveRecord:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可实现基本 CRUD 操作 支持代码生成:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 代码,支持模板引擎,更有超多自定义配置等您来使用(P.S. 比 Mybatis 官方的 Generator 更加强大!) 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere ) 支持关键词自动转义:支持数据库关键词(order、key……)自动转义,还可自定义关键词 内置分页插件:基于Mybatis物理分页,开发者无需关心具体操作,配置好插件之后,分页等同于普通List查询 内置性能分析插件:可输出Sql语句以及其执行时间,建议开发测试时启用该功能,能有效解决慢查询 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,预防误操作
### 回答1: Mybatis-Plus是一个基于Mybatis的增强工具,提供了许多实用的功能,如自动生成代码、分页查询、条件构造器、性能分析等。Mybatis-Plus ServiceMybatis-Plus的一个模块,提供了一些常用的Service接口和实现类,如IServiceServiceImpl等,方便开发者快速构建Service。 ### 回答2: Mybatis-Plus是一个开源的、能够和Mybatis无缝衔接并扩展出更多实用功能的框架。在实际项目开发中,通常会使用到基于Mybatis-Plus的Service,以下是关于Mybatis-Plus Service的一些介绍。 Mybatis-Plus的Service是基于Mybatis-Plus框架进行封装的,旨在简化开发者编Service代码的流程。使用Mybatis-Plus Service可以有效地减少重复代码的编,提高开发效率。 在Mybatis-Plus Service中,通常包含了一些常见的CRUD方法,如查询列表、根据ID查询、插入、更新和删除等。我们可以通过继承BaseService或者IService来使用这些方法。同时,Mybatis-Plus Service还提供了一些强大的查询构建器,如LambdaQueryWrapper和QueryWrapper等,可以快速构建复杂的查询条件。 另外,Mybatis-Plus Service还支持事务管理。它提供了一种@Transactional注解来实现声明式事务,我们只需在Service的方法上添加该注解,即可完成事务的配置。 除了基本的CRUD操作和事务管理外,Mybatis-Plus Service还具有其他扩展的功能,例如分页查询、批量操作、逻辑删除等。这些功能都能够极大地简化开发者的编码工作。 总而言之,Mybatis-Plus Service是一种基于Mybatis-Plus框架的封装,用于简化Service代码的编。它提供了一系列的CRUD方法、事务管理以及其他实用功能,可以提高开发效率并减少冗余代码的编。使用Mybatis-Plus Service,开发者能够更加专注于业务逻辑的实现,提高开发效率和代码质量。 ### 回答3: MyBatis-Plus是一个在MyBatis基础上的增强工具,提供了更加便捷的CRUD操作方式。其中的ServiceMyBatis-Plus提供的一封装,主要用于处理业务逻辑和数据库操作之间的关系。 MyBatis-Plus的Service主要有以下几个功能: 1. 提供通用的CRUD方法:Service提供了常见的增删改查方法,如save、remove、update等,可以直接调用这些方法来操作数据库,无需手SQL语句。 2. 封装条件构造器:Service的方法中可以使用MyBatis-Plus提供的条件构造器来进行查询条件的组装,例如可以使用eq、like、in等方法来构建查询条件。 3. 支持分页查询:Service提供了分页查询的方法,可以方便地进行数据分页查询操作。可以设置每页的数量、当前页码等参数来实现分页效果。 4. 支持事务控制:Service可以通过注解的方式来对方法添加事务控制,保证在业务逻辑中的多个操作要么全部成功,要么全部失败。可以使用@Transactional注解来标记需要进行事务控制的方法。 总的来说,MyBatis-Plus的Service提供了一种更加便捷的数据库操作方式,简化了开发过程中的数据库操作代码,提高了开发效率。同时,它还具备一些高级功能,如条件构造器、分页查询和事务控制,使得开发者可以更加灵活和方便地进行业务逻辑的处理和数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值