这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
分享:技术:数据源:spring_mybatis_多数据源配置_1 [2016/01/11 15:39] gxx |
分享:技术:数据源:spring_mybatis_多数据源配置_1 [2016/01/12 14:06] (当前版本) gxx |
||
---|---|---|---|
行 7: | 行 7: | ||
- 适合两个数据库没有相关性的情况 | - 适合两个数据库没有相关性的情况 | ||
- 不适合master-slave性的多数据源的配置,需要根据业务类型进行细致配置,比如增删改在master上,不允许有主从同步时间延时的查询在master上,耗时查询在slave上等等 | - 不适合master-slave性的多数据源的配置,需要根据业务类型进行细致配置,比如增删改在master上,不允许有主从同步时间延时的查询在master上,耗时查询在slave上等等 | ||
+ | - vo,dao,mapper,service都需要两套 | ||
===== 主要代码 ===== | ===== 主要代码 ===== | ||
==== application-context.xml ==== | ==== application-context.xml ==== | ||
行 197: | 行 198: | ||
slave.local.jdbc.password=root | slave.local.jdbc.password=root | ||
</file> | </file> | ||
+ | ==== MasterUserService.java ==== | ||
+ | <file java MasterUserService.java> | ||
+ | package com.gxx.record.service.master; | ||
+ | |||
+ | import com.gxx.record.base.master.vo.User; | ||
+ | |||
+ | /** | ||
+ | * <dl> | ||
+ | * <dt><b>Title:</b></dt> | ||
+ | * <dd> | ||
+ | * 用户服务接口 | ||
+ | * </dd> | ||
+ | * <dt><b>Description:</b></dt> | ||
+ | * <dd> | ||
+ | * <p>none | ||
+ | * </dd> | ||
+ | * </dl> | ||
+ | * | ||
+ | * @author Administrator | ||
+ | * @version 1.0, 2015年6月18日 | ||
+ | * @since record | ||
+ | * | ||
+ | */ | ||
+ | public interface MasterUserService { | ||
+ | /** | ||
+ | * 新增用户 | ||
+ | * @param user | ||
+ | */ | ||
+ | public void doSaveUser(User user); | ||
+ | |||
+ | /** | ||
+ | * 根据姓名查用户 | ||
+ | * @param name | ||
+ | * @return | ||
+ | */ | ||
+ | public User getUserByName(String name); | ||
+ | } | ||
+ | </file> | ||
+ | ==== MasterUserServiceImpl.java ==== | ||
+ | <file java MasterUserServiceImpl.java> | ||
+ | package com.gxx.record.service.master.impl; | ||
+ | |||
+ | import org.springframework.beans.factory.annotation.Autowired; | ||
+ | import org.springframework.beans.factory.annotation.Qualifier; | ||
+ | import org.springframework.stereotype.Service; | ||
+ | |||
+ | import com.gxx.record.base.master.dao.UserMapper; | ||
+ | import com.gxx.record.base.master.vo.User; | ||
+ | import com.gxx.record.service.master.MasterUserService; | ||
+ | |||
+ | /** | ||
+ | * <dl> | ||
+ | * <dt><b>Title:</b></dt> | ||
+ | * <dd> | ||
+ | * 用户服务实现类 | ||
+ | * </dd> | ||
+ | * <dt><b>Description:</b></dt> | ||
+ | * <dd> | ||
+ | * <p>none | ||
+ | * </dd> | ||
+ | * </dl> | ||
+ | * | ||
+ | * @author Administrator | ||
+ | * @version 1.0, 2015年6月18日 | ||
+ | * @since record | ||
+ | * | ||
+ | */ | ||
+ | @Service("masterUserService") | ||
+ | public class MasterUserServiceImpl implements MasterUserService { | ||
+ | |||
+ | @Autowired | ||
+ | @Qualifier("masterUserMapper") | ||
+ | private UserMapper userDao; | ||
+ | |||
+ | /** | ||
+ | * 新增用户 | ||
+ | * @param user | ||
+ | */ | ||
+ | public void doSaveUser(User user) { | ||
+ | userDao.insert(user); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 根据姓名查用户 | ||
+ | * @param name | ||
+ | * @return | ||
+ | */ | ||
+ | public User getUserByName(String name) { | ||
+ | return userDao.getUserByName(name); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </file> | ||
+ | ==== UserMapper.java ==== | ||
+ | <file java UserMapper.java> | ||
+ | package com.gxx.record.base.master.dao; | ||
+ | |||
+ | import org.springframework.stereotype.Repository; | ||
+ | |||
+ | import com.gxx.record.base.master.vo.User; | ||
+ | |||
+ | @Repository("masterUserMapper") | ||
+ | public interface UserMapper { | ||
+ | int deleteByPrimaryKey(Integer id); | ||
+ | |||
+ | int insert(User record); | ||
+ | |||
+ | int insertSelective(User record); | ||
+ | |||
+ | User selectByPrimaryKey(Integer id); | ||
+ | |||
+ | int updateByPrimaryKeySelective(User record); | ||
+ | |||
+ | int updateByPrimaryKey(User record); | ||
+ | |||
+ | /** | ||
+ | * 根据姓名查用户 | ||
+ | * @param name | ||
+ | * @return | ||
+ | */ | ||
+ | User getUserByName(String name); | ||
+ | } | ||
+ | </file> | ||
+ | ==== User.java ==== | ||
+ | <file java User.java> | ||
+ | package com.gxx.record.base.master.vo; | ||
+ | |||
+ | public class User { | ||
+ | private Integer id; | ||
+ | |||
+ | private String name; | ||
+ | |||
+ | private String password; | ||
+ | |||
+ | private String createDate; | ||
+ | |||
+ | private String createTime; | ||
+ | |||
+ | public Integer getId() { | ||
+ | return id; | ||
+ | } | ||
+ | |||
+ | public void setId(Integer id) { | ||
+ | this.id = id; | ||
+ | } | ||
+ | |||
+ | public String getName() { | ||
+ | return name; | ||
+ | } | ||
+ | |||
+ | public void setName(String name) { | ||
+ | this.name = name == null ? null : name.trim(); | ||
+ | } | ||
+ | |||
+ | public String getPassword() { | ||
+ | return password; | ||
+ | } | ||
+ | |||
+ | public void setPassword(String password) { | ||
+ | this.password = password == null ? null : password.trim(); | ||
+ | } | ||
+ | |||
+ | public String getCreateDate() { | ||
+ | return createDate; | ||
+ | } | ||
+ | |||
+ | public void setCreateDate(String createDate) { | ||
+ | this.createDate = createDate == null ? null : createDate.trim(); | ||
+ | } | ||
+ | |||
+ | public String getCreateTime() { | ||
+ | return createTime; | ||
+ | } | ||
+ | |||
+ | public void setCreateTime(String createTime) { | ||
+ | this.createTime = createTime == null ? null : createTime.trim(); | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
+ | ==== UserMapper.xml ==== | ||
+ | <file xml 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.gxx.record.base.master.dao.UserMapper" > | ||
+ | <resultMap id="BaseResultMap" type="com.gxx.record.base.master.vo.User" > | ||
+ | <id column="id" property="id" jdbcType="INTEGER" /> | ||
+ | <result column="name" property="name" jdbcType="VARCHAR" /> | ||
+ | <result column="password" property="password" jdbcType="VARCHAR" /> | ||
+ | <result column="create_date" property="createDate" jdbcType="VARCHAR" /> | ||
+ | <result column="create_time" property="createTime" jdbcType="VARCHAR" /> | ||
+ | </resultMap> | ||
+ | <sql id="Base_Column_List" > | ||
+ | id, name, password, create_date, create_time | ||
+ | </sql> | ||
+ | <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > | ||
+ | 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.gxx.record.base.master.vo.User" > | ||
+ | insert into user (id, name, password, | ||
+ | create_date, create_time) | ||
+ | values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, | ||
+ | #{createDate,jdbcType=VARCHAR}, #{createTime,jdbcType=VARCHAR}) | ||
+ | </insert> | ||
+ | <insert id="insertSelective" parameterType="com.gxx.record.base.master.vo.User" > | ||
+ | insert into user | ||
+ | <trim prefix="(" suffix=")" suffixOverrides="," > | ||
+ | <if test="id != null" > | ||
+ | id, | ||
+ | </if> | ||
+ | <if test="name != null" > | ||
+ | name, | ||
+ | </if> | ||
+ | <if test="password != null" > | ||
+ | password, | ||
+ | </if> | ||
+ | <if test="createDate != null" > | ||
+ | create_date, | ||
+ | </if> | ||
+ | <if test="createTime != null" > | ||
+ | create_time, | ||
+ | </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="password != null" > | ||
+ | #{password,jdbcType=VARCHAR}, | ||
+ | </if> | ||
+ | <if test="createDate != null" > | ||
+ | #{createDate,jdbcType=VARCHAR}, | ||
+ | </if> | ||
+ | <if test="createTime != null" > | ||
+ | #{createTime,jdbcType=VARCHAR}, | ||
+ | </if> | ||
+ | </trim> | ||
+ | </insert> | ||
+ | <update id="updateByPrimaryKeySelective" parameterType="com.gxx.record.base.master.vo.User" > | ||
+ | update user | ||
+ | <set > | ||
+ | <if test="name != null" > | ||
+ | name = #{name,jdbcType=VARCHAR}, | ||
+ | </if> | ||
+ | <if test="password != null" > | ||
+ | password = #{password,jdbcType=VARCHAR}, | ||
+ | </if> | ||
+ | <if test="createDate != null" > | ||
+ | create_date = #{createDate,jdbcType=VARCHAR}, | ||
+ | </if> | ||
+ | <if test="createTime != null" > | ||
+ | create_time = #{createTime,jdbcType=VARCHAR}, | ||
+ | </if> | ||
+ | </set> | ||
+ | where id = #{id,jdbcType=INTEGER} | ||
+ | </update> | ||
+ | <update id="updateByPrimaryKey" parameterType="com.gxx.record.base.master.vo.User" > | ||
+ | update user | ||
+ | set name = #{name,jdbcType=VARCHAR}, | ||
+ | password = #{password,jdbcType=VARCHAR}, | ||
+ | create_date = #{createDate,jdbcType=VARCHAR}, | ||
+ | create_time = #{createTime,jdbcType=VARCHAR} | ||
+ | where id = #{id,jdbcType=INTEGER} | ||
+ | </update> | ||
+ | | ||
+ | <select id="getUserByName" resultMap="BaseResultMap" parameterType="java.lang.String" > | ||
+ | select | ||
+ | <include refid="Base_Column_List" /> | ||
+ | from user | ||
+ | where name = #{name,jdbcType=VARCHAR} | ||
+ | </select> | ||
+ | | ||
+ | </mapper> | ||
+ | </file> | ||
+ | ===== 附上代码 ===== | ||
+ | {{:分享:技术:数据源:record.zip|}} |