基于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;
	}

}



版权声明:本文为jinxiguo原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。