`

两个不同系统之间数据同步

阅读更多

PS:走着走着忘记了自己做了什么,有时间就就记录下自己的脚印。。。。。。。  涛声一笑

场景:这几天在做前后台数据同步时,遇到这种情况,前台系统的数据表结构与后台的数据表结构不相同,但前台只需要后台表中某些数据的字段,以下记录下自己处理的原理过程。

技术支持:mybatis、JPA、spring工具类BeanUtils

首先双方约定好,前台定义接口以及实现,后台通过前台提供的接口包传输数据。

1、后台系统对应表结构的实体类

 

public class Person {
    private Integer userId;
    private String userName;
    private Integer userAge;
    private String address;
    
    public Integer getUserId() {
           return userId;
    }
    public void setUserId(Integer userId) {
	this.userId = userId;
    }
    public String getUserName() {
	return userName;
    }
    public void setUserName(String userName) {
	this.userName = userName;
     }
......
}

2、前台系统对应表结构的实体类

 

 

public class Person{    
    private Integer id;
    private String name;
    private Integer age;
    private String  sno;
    。。。。。。

 3、通过对比1、2,前台与后台的JavaBean属性字段不相同,假设前台需要同步后台Person中的userId(id),userName(name),userAge(age)属性。

 

4、将后台的JavaBean拷贝到前台工程中,保留前台需要字段

 

public class Person4Backstage{
    private Integer userId;
    private String userName;
    private Integer userAge;
    
    public Integer getUserId() {
           return userId;
    }
    public void setUserId(Integer userId) {
	this.userId = userId;
    }
    public String getUserName() {
	return userName;
    }
    public void setUserName(String userName) {
	this.userName = userName;
     }
    public String getUserAge(){
        return userAge;
   }
   public void setUserAge(){
      this.userAge = userAge;
  }
}

 5、由于后台的JavaBean与前台JavaBean对应的属性名不一致,因此通过JPA将Person4Backstage中的属性与前台表中字段属性保持一致

 

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "person")
public class Person4Person4Backstage implements Serializable{
	
   
    private static final long serialVersionUID = 1L;

    private Integer userId;

    private String userName;

    private Integer userAge;

    @Column(name="id")
	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	@Column(name="name")
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Column(name="age")
	public Integer getUserAge() {
		return userAge;
	}

	public void setUserAge(Integer userAge) {
		this.userAge = userAge;
	}
    
}

 6、在pom.xml文件加入对应jar依赖配置文件

 

 

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>

 7、在PersonMapper.xml加入以下配置

 

 

<update id="updatePerson4Backstage" parameterType="com.zlt.pojo.Person4Backstage">
    update person
    <set>
      <if test="userName != null">
        name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="userAge != null">
        age = #{userAge,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{userId,jdbcType=INTEGER}
  </update>

 8、在PersonMapper接口中加入以下方法:

 

 

 int updatePerson4Backstage(Person4Backstage person);

 9、在PersonService接口中加入以下方法:

 

 

 int updatePerson4Backstage(Person person);注此参数Person是后台JavaBean Person

 10、在PersonServiceImpl中实现:

public int updatePerson4Backstage(Person person){
		 int count = 0;
		 Person4Backstage obj = new Person4Backstage();
		 BeanUtils.copyProperties(person, obj);//将后台传过来的属性通过copyProperties方法,实现属性拷贝,属性不一样不会拷贝
		 count = personMapper.updatePerson4Backstage(obj);
		 return count;
	 }

 以上简单记录下,自己处理过程,如有不够全,请多多指教!

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics