MyBatis框架下Service层引入方式
1. 創建service, serviceImpl 結構包,對應BookService, BookServiceImpl
?
2. 獲取連接
?
3. 代理功能
Dao dao ?= session.getMapper(Dao.class);
4,通過SqlSession的代理功能,調用Dao層接口方法
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;
?? ?}
?
}
總結
以上是生活随笔為你收集整理的MyBatis框架下Service层引入方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Improving speech re
- 下一篇: AngularJs的一些知识点-1