SpringData JPA环境的搭建

 

                                                      

一  Maven:

   
        5.1.6.RELEASE
        5.0.7.Final
        1.6.6
        1.2.12
        0.9.1.2
        5.1.6
    

    
        
        
            junit
            junit
            3.8.1
            test
        

        
        
            org.aspectj
            aspectjweaver
            1.6.8
        

        
            org.springframework
            spring-aop
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        

        
            org.springframework
            spring-context-support
            ${spring.version}
        

        
            org.springframework
            spring-orm
            ${spring.version}


        

        
            org.springframework
            spring-beans
            ${spring.version}
        

        
            org.springframework
            spring-core
            ${spring.version}
        

        

        
        
            org.hibernate
            hibernate-core
            ${hibernate.version}
            
                
                    dom4j
                    dom4j
                
            
        
        
            org.hibernate
            hibernate-entitymanager
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-validator
            5.2.1.Final
            
                
                    jboss-logging
                    org.jboss.logging
                
            
        
        

        
        
            org.apache.commons
            commons-dbcp2
            2.6.0
        
        

        
        
            log4j
            log4j
            ${log4j.version}
        

        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        

        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
            
                
                    log4j
                    log4j
                
            
        
        


        
            mysql
            mysql-connector-java
            ${mysql.version}
        

        
            org.springframework.data
            spring-data-jpa
            1.9.0.RELEASE
            
                
                    spring-beans
                    org.springframework
                
                
                    spring-core
                    org.springframework
                
                
                    spring-aop
                    org.springframework
                
                
                    slf4j-api
                    org.slf4j
                
                
                    spring-tx
                    org.springframework
                
                
                    spring-orm
                    org.springframework
                
                
                    jcl-over-slf4j
                    org.slf4j
                
                
                    spring-context
                    org.springframework
                
            
        

        
            org.springframework
            spring-test
            5.1.6.RELEASE
        

        
        
            javax.el
            javax.el-api
            2.2.4
        

        
            org.glassfish.web
            javax.el
            2.2.4
        
        
            junit
            junit
            4.12
            test
        
        
    

 

二  ApplicationContext.xml 配置:





    
    

        
        
        
        

        
            
        

        

        
            
                
                
                
                
                
                
                
                
            
        


        

        
           
        
        
        
            
                update
            
        
    

    
     
        
        
        
        
    

    
    
    

    
    
        
    

    

    
    

三 实体类的配置

.说明 :1 .Customer类(客户)与  LinkMan(联系人)是一对多的关系

             2  Role类(角色) 与    User类  (用户) 是多对多的关系

3.1 Customer类(客户类)

package cn.xtkj.domain;

/*
 * 1.实体类和表的映射关系
 * @Etity
 * @Table
 * 2.类中属性和表中字段的映射关系
 * @Id
 * @GeneratedValue
 * @Column
 *
 * */

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name="cst_customer")//主表:一对多中的一
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="cust_id")
    private Long custId;
    @Column(name="cust_address")
    private String custAddress;
    @Column(name="cust_industry")
    private String custIndustry;
    @Column(name="cust_level")
    private String custLevel;
    @Column(name="cust_name")
    private String custName;
    @Column(name="cust_phone")
    private String custPhone;
    @Column(name="cust_source")
    private String custSource;


    // @Column(name="")
    // 配置客户和练习人之间的关系(一堆多的关系)
    /*
     * 使用注解的形式配置多表关系
     *       1.声明关系
     *           @OneToMang:配置 一对多关系
     *               targetEntity:对方对象的字节码对象
     *       2.配置外键(中间表)
     *          @JoinColumn
     *               name:外键字段名称
     *               referencedColumnName:参照主表的主键字段名称
     *
     * *在客户实体类上(一的一方)添加了外键配置,所以对于客户而言,也具备了维护外键的作用
     * */

  /*
   因为同时操作 一的一方数据 和多的一方数据,在操作一的一方数据时,会多出来一条修改语句 ,所以就不需要
    @OneToMany(targetEntity = LinkMan.class)
    @JoinColumn(name = "lkm_cust_id",referencedColumnName = "cust_id")//lkm_cust_id从表  cust_id主表
 */
  /*
  * 放弃外键维护权
  *     mappedBy:对方配置关系的属性名称
  *     cascade:配置级联(多以配置到多表的映射关系的注解上面)
  *     CascadeType.all      :所有
  *                 MERGE    :更新
  *                 PERSIST  : 保存
  *                 REMOVE   : 删除*
  *
  *
  *   fetch: 配置关联对象的加载方式
  *         EAGER :立即加载
  *         LAZY  : 延迟加载
  * fetch = FetchType.EAGER
  * */
  @OneToMany(mappedBy = "customer",cascade = CascadeType.ALL)//放弃描述直接参照多的一方的Customer对象中的数据即可
  private Set linkMENs = new HashSet();



    public Set getLinkMENs() {
        return linkMENs;
    }

    public void setLinkMENs(Set linkMENs) {
        this.linkMENs = linkMENs;
    }

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

   @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custAddress='" + custAddress + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custName='" + custName + '\'' +
                ", custPhone='" + custPhone + '\'' +
                ", custSource='" + custSource + '\'' +
                '}';
    }
}

3.2  LinkMan(联系人类)

package cn.xtkj.domain;

/*
 * 1.实体类和表的映射关系
 * @Etity
 * @Table
 * 2.类中属性和表中字段的映射关系
 * @Id
 * @GeneratedValue
 * @Column
 *
 * */

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name="cst_customer")//主表:一对多中的一
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="cust_id")
    private Long custId;
    @Column(name="cust_address")
    private String custAddress;
    @Column(name="cust_industry")
    private String custIndustry;
    @Column(name="cust_level")
    private String custLevel;
    @Column(name="cust_name")
    private String custName;
    @Column(name="cust_phone")
    private String custPhone;
    @Column(name="cust_source")
    private String custSource;


    // @Column(name="")
    // 配置客户和练习人之间的关系(一堆多的关系)
    /*
     * 使用注解的形式配置多表关系
     *       1.声明关系
     *           @OneToMang:配置 一对多关系
     *               targetEntity:对方对象的字节码对象
     *       2.配置外键(中间表)
     *          @JoinColumn
     *               name:外键字段名称
     *               referencedColumnName:参照主表的主键字段名称
     *
     * *在客户实体类上(一的一方)添加了外键配置,所以对于客户而言,也具备了维护外键的作用
     * */

  /*
   因为同时操作 一的一方数据 和多的一方数据,在操作一的一方数据时,会多出来一条修改语句 ,所以就不需要
    @OneToMany(targetEntity = LinkMan.class)
    @JoinColumn(name = "lkm_cust_id",referencedColumnName = "cust_id")//lkm_cust_id从表  cust_id主表
 */
  /*
  * 放弃外键维护权
  *     mappedBy:对方配置关系的属性名称
  *     cascade:配置级联(多以配置到多表的映射关系的注解上面)
  *     CascadeType.all      :所有
  *                 MERGE    :更新
  *                 PERSIST  : 保存
  *                 REMOVE   : 删除*
  *
  *
  *   fetch: 配置关联对象的加载方式
  *         EAGER :立即加载
  *         LAZY  : 延迟加载
  * fetch = FetchType.EAGER
  * */
  @OneToMany(mappedBy = "customer",cascade = CascadeType.ALL)//放弃描述直接参照多的一方的Customer对象中的数据即可
  private Set linkMENs = new HashSet();



    public Set getLinkMENs() {
        return linkMENs;
    }

    public void setLinkMENs(Set linkMENs) {
        this.linkMENs = linkMENs;
    }

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

   @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custAddress='" + custAddress + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custName='" + custName + '\'' +
                ", custPhone='" + custPhone + '\'' +
                ", custSource='" + custSource + '\'' +
                '}';
    }
}

3.3 创建Role(角色类)

package cn.xtkj.domain;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;


@Entity
@Table(name="sys_role")
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private Long roleID;
    @Column(name="role_name")
    private String roleName;

    //配置多对多关系

  /*
    @ManyToMany(targetEntity = User.class)
    @JoinTable(name= "sy_user_role",
            //joinColumns, 当前表在中间表中的外键
            joinColumns={@JoinColumn(name="sys_role_id",referencedColumnName = "role_id")},
            //inverseJoinColumn,对方对象在中间表的外键
            inverseJoinColumns = {@JoinColumn(name="sys_user_id",referencedColumnName = "user_id")}
    )*/

    @ManyToMany(mappedBy = "roles") //放弃外键维护权 从多的一方的表中 的 该类对象中的映射获得(User中的Role对象 roles) 维护关系。 (*   roles  取决于对方映射关系 的属性名称   *)
    private Set users=new HashSet();

    public Long getRoleID() {
        return roleID;
    }

    public void setRoleID(Long roleID) {
        this.roleID = roleID;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public Set getUsers() {
        return users;
    }

    public void setUsers(Set users) {
        this.users = users;
    }
}

3.4创建User(用户类 )

package cn.xtkj.domain;


import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "sys_user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "user_id")
    private Long userId;
    @Column(name = "user_Name")
    private String userName;
    @Column(name = "user_age")
    private  Integer age;



    /*
    * 配置用户到角色的多对多关系
    *       配置多对多的映射关系
    *         1.声明表关系
     *             @ManyToMany(targetEntity = Role.class)//多对多
    *                   targetEntity:对方的实体类字节码
     *
    *         2.配置中间表(包含两个外键)
     *               @JoinTable
    *                   name: 中间表的名称
    *                   joinColumns:配制当前对象在中间表的外键
    *                       @JoinColumn的数组
    *                           name:外键名
    *                           referencedColumnName:参照的主表的主键名
    *
    *
    *
    * */
    @ManyToMany(targetEntity = Role.class,cascade = CascadeType.ALL)
    @JoinTable(name= "sy_user_role",
            //joinColumns, 当前表在中间表中的外键
            joinColumns={@JoinColumn(name="sys_user_id",referencedColumnName = "user_id")},
            //inverseJoinColumn,对方对象在中间表的外键
            inverseJoinColumns = {@JoinColumn(name="sys_role_id",referencedColumnName = "role_id")}
    )
    private Set roles =new HashSet();


    public Long getUserId() {
        return userId;
    }

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

    public String getUserName() {
        return userName;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
    public Set getRoles() {
        return roles;
    }

    public void setRoles(Set roles) {
        this.roles = roles;
    }

}

 

 

你可能感兴趣的:(章标签:SpringData,SpringData)