一個電商專案的Web服務化改造4:方案和架構,通用介面的定義和實現

小雷FansUnion發表於2016-04-30
    最近一直在做一個電商專案,需要把原有單系統架構的專案,改造成基於服務的架構,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>  

 
   更多介面的定義和實現,後面再介紹。

相關文章