mybatis shardbatis分表策略

一、Pom.xml添加maven依赖

<dependency>
<groupId>org.shardbatis</groupId>
<artifactId>shardbatis</artifactId>
<version>2.0.0B-NEW</version>

</dependency>

二、mybatis-config.xml配置文件配置sharding插件,指定shardingConfig即sharding_config.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!-- 分表策略插件 -->
<plugin interceptor="com.google.code.shardbatis.plugin.ShardPlugin">
<property name="shardingConfig" value="sharding_config.xml" />
</plugin>
</plugins>

</configuration>

三、实现分表策略接口,Link实体对应t_link表,自定义分表策略,表名=baseTableName_*

package util;
import com.google.code.shardbatis.strategy.ShardStrategy;
/**   
 * Description:  
 * @author guoshanglei  
 * @date 2018-5-2  
 */
public class LinkStrategyImpl implements ShardStrategy {
@Override
public String getTargetTableName(String baseTableName, Object params, String mapperId) {
baseTableName=baseTableName+"_"+DateUtils.dateToString(null, "yyyyMM");
return baseTableName;
}

}

四、配置分表策略的sharding配置文件sharding_config.xml

注意红色部分:E:shardbatis-config.dtd文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT shardingConfig (ignoreList?,parseList?,strategy*)>
<!ELEMENT ignoreList (value+)>
<!ELEMENT parseList (value+)>
<!ELEMENT value (#PCDATA)>
<!ELEMENT strategy EMPTY>
<!ATTLIST strategy
tableName CDATA #REQUIRED
strategyClass CDATA #REQUIRED
>

无法获取暂时用本地file获取,建议改成服务器下http://image.91banban.com/shardbatis-config.dtd方式获取

sharding_config.xml:

<?xml version="1.0" encoding="UTF-8" ?> 

<!DOCTYPE shardingConfig 
PUBLIC "-//shardbatis.googlecode.com//DTDShardbatis 2.0//EN" 
"
file:///E:shardbatis-config.dtd">
<shardingConfig>
<!-- parseList可选配置 如果配置了parseList,只有在parseList范围内的方法,并且不再ignoreList内的方法,sql表名才会被解析和修改 -->
<parseList>
<!-- <value>dao.LinkMapper.insert</value>  -->
<value>dao.LinkMapper.insertSelective</value> 
<value>dao.LinkMapper.selectByPrimaryKey</value> 
<value>dao.LinkMapper.updateByPrimaryKeySelective</value> 
<value>dao.LinkMapper.selectByPrimaryKey</value> 
</parseList>
<ignoreList>
<value>dao.LinkMapper.selectByPrimaryKey</value> 
</ignoreList>
<!-- 配置分表策略,指定自定义的LinkStrategyImpl类,实现了ShardStrategy 接口 -->
<strategy tableName="t_link" strategyClass="util.LinkStrategyImpl" />

</shardingConfig> 

五:补充LinkMapper.xml分表前后没有任何区别,加入分表策略后对原代码无侵入,执行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="dao.LinkMapper" >
  <resultMap id="BaseResultMap" type="model.Link" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="department" property="department" jdbcType="VARCHAR" />
    <result column="position" property="position" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, department, position, phone, email
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_link
    where id = #{id,jdbcType=INTEGER}
  </select>
  <select id="getAllLink" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_link
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_link
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="model.Link" >
    insert into t_link (id, name, department, 
      position, phone, email
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{department,jdbcType=VARCHAR}, 
      #{position,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="model.Link" >
    insert into t_link
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="department != null" >
        department,
      </if>
      <if test="position != null" >
        position,
      </if>
      <if test="phone != null" >
        phone,
      </if>
      <if test="email != null" >
        email,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="department != null" >
        #{department,jdbcType=VARCHAR},
      </if>
      <if test="position != null" >
        #{position,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="model.Link" >
    update t_link
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="department != null" >
        department = #{department,jdbcType=VARCHAR},
      </if>
      <if test="position != null" >
        position = #{position,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="model.Link" >
    update t_link
    set name = #{name,jdbcType=VARCHAR},
      department = #{department,jdbcType=VARCHAR},
      position = #{position,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <insert id="insertLinks" parameterType="java.util.List">
    insert into t_link (id, name, department, 
      position, phone, email
      )
    values
    <foreach collection="list" item="itm" index="index" separator=",">
       (#{itm.id,jdbcType=INTEGER}, #{itm.name,jdbcType=VARCHAR}, #{itm.department,jdbcType=VARCHAR}, 
      #{itm.position,jdbcType=VARCHAR}, #{itm.phone,jdbcType=VARCHAR}, #{itm.email,jdbcType=VARCHAR}
      )
    </foreach>
 </insert>
</mapper>

阅读更多
个人分类: mybatis相关
想对作者说点什么? 我来说一句

spring+mybatis分表插件shardbatis

2017年04月26日 17.3MB 下载

没有更多推荐了,返回首页

不良信息举报

mybatis shardbatis分表策略

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭