一个电商项目的Web服务化改造4:方案和架构,通用接口的定义和实现

    最近一直在做一个电商项目,需要把原有单系统架构的项目,改造成基于服务的架构,SOA。
    有点挑战,做完了,会有很大进步。

   上一篇,我们明确了我们的“规范和约定”。

   从本篇开始,写具体的方案和架构。

    本篇,重点阐述通用接口定义。

    既然做了分表,那么针对单表的CRUD等很多代码,都是一样的,类似的,因此,定义通用的接口和实现。
就可以配置模版,自动化生成代码啦,后面再详细介绍代码自动化生成。


  
BaseMapper通用的sql接口定义
 

   
 import java.util.List;

public interface BaseMapper<ID, Entity,Bean> {
	//read
	Entity get(ID id);
	
	List<Entity> listByIdList(List<String> idList);

	List<Entity> list(Bean bean);
	List<Entity> listAll();

	//write
	int add(Entity entity);

	int update(Entity entity);
	int updateByMemberId(Entity entity);
	
	int remove(ID id);
	
	int removeByIdList(List<ID> idList);

}



BrandMapper具体的Mybatis接口定义,继承BaseMapper,再添加自己特殊的sql映射接口定义
@Mapper
public interface BrandMapper extends BaseMapper<String, Brand,BrandBean> {
	//---------------------read-------------------------

	List<Brand> listByShopIdList(List<String> shopIdList);

	//---------------------write-------------------------

} 



BrandMapper.xml 品牌表的Mybatis sql语句
<?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.webservice.mapper.BrandMapper">
	<sql id="columns">
		id,name,logo,createTime,updateTime,isDelete
	</sql>

	<select id="get" resultType="Brand">
		select
		<include refid="columns" />
		from brand
		where id =
		#{id}
	</select>

	<select id="list" resultType="Brand">
		select
		<include refid="columns" />
		from brand where
		isDelete=0
		<if test="name != null and name !=''">
			and name like '%${name}%'
		</if>
		order by createTime desc
	</select>

	<select id="listAll" resultType="Brand">
		select
		<include refid="columns" />
		from brand where
		isDelete=0 order by createTime desc
	</select>

	<select id="listByShopIdList" parameterType="String" resultType="Brand">
		select
		<include refid="columns" />
		from brand where merchantId in
		<foreach collection="list" index="index" item="item" open="("
			separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="listByIdList" parameterType="String" resultType="Brand">
		select
		<include refid="columns" />
		from brand where id in
		<foreach collection="list" index="index" item="item" open="("
			separator="," close=")">
			#{item}
		</foreach>
	</select>


	<insert id="add" parameterType="Brand">
		insert into brand
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">
				id,
			</if>
			<if test="name != null">
				name,
			</if>
			<if test="logo != null">
				logo,
			</if>
			createTime,
			updateTime
		</trim>
		<trim prefix="values(" suffix=")" suffixOverrides=",">
			<if test="id != null">
				#{id},
			</if>
			<if test="name != null">
				#{name},
			</if>
			<if test="logo != null">
				#{logo},
			</if>
			now(),
			now()
		</trim>

	</insert>

	<update id="update" parameterType="Brand">
		update brand
		<set>
			<if test="name != null">
				name = #{name},
			</if>
			<if test="logo != null">
				logo = #{logo},
			</if>
			updateTime=now()
		</set>
		where id=#{id}
	</update>
	
	<update id="remove" parameterType="String">
		update brand set isDelete
		=
		1,updateTime=now() where id=#{id}
	</update>

	<update id="removeByIdList" parameterType="String">
		update brand
		set isDelete = 1,updateTime = now()
		where
		id in
		<foreach item="item" index="index" collection="list" open="("
			separator="," close=")">
			#{item}
		</foreach>
	</update>


</mapper>  

 
   更多接口的定义和实现,后面再介绍。
原文地址:https://www.cnblogs.com/qitian1/p/6462428.html