Spring Data JPA 基础第二篇

末点 2019-11-28

主要调用工具类JpaUtils类

package cn.itcast.utils;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;/** * 解决静态实体管理器工厂的浪费资源和耗时问题 *     通过静态代码块的形式,当程序第一次访问此工具类时,创建一个公共的实体管理器工厂对象 * * 第一次访问getEntityManager方法:经过静态代码块创建一个factory对象,再调用方法创建一个EntityManager对象 * 第二次访问getEntityManager方法:直接通过一个已经创建好的factory对象,创建EntityManager对象 */public class JpaUtils {    private  static EntityManagerFactory factory;    static {        //1.加载配置文件,创建entityManagerFactory       factory = Persistence.createEntityManagerFactory("myJpa");    }    /**     * 获取EntityManager对象     */    public  static EntityManager getEntityManager(){       return factory.createEntityManager();    }}主要在persistence.xml更改一下把之前的create插入改成update更新
<property name="hibernate.hbm2ddl.auto" value="create"/>

Spring Data JPA 基础第二篇

 主要的实体类

package cn.itcast.domain;import javax.persistence.*;/** * 客户实体类 *     配置映射关系 *        1、实体类和表的映射关系 *        2、实体类中属性和表中字段的映射关系 * @Entity:声明实体类 * @Table:配置实体类和表的映射关系 *       name:配置数据库标的名称 * 2.实体类中属性和表中字段的映射光线 */@Entity@Table(name = "cst_customer")public class Customer {    /**     * @Id:声明主键配置     * @GeneratedValue:配置主键的生成策略     *       GenerationType.IDENTITY:自增 MySQL     *                底层数据库必须支持自增长     *       GenerationType.SEQUENCE:序列 oracle     *                底层数据库必须支持序列     *       GenerationType.TABLE:     *             jpa提供的一种机制,通过一张数据库表的形式帮助我们完成自增长     *       GenerationType.AUTO:     *             有程序自动的帮助我们选择主键生成策略     * @Column:配置属性和字段的映射关系     *       name:数据库中字段的名称     */    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name = "cust_id")    private  Long custId;//客户主键    @Column(name = "cust_name")    private  String custName;//客户名称    @Column(name = "cust_source")    private  String custSource;//客户来源    @Column(name = "cust_industry")    private  String custIndustry;//客户级别    @Column(name = "cust_level")    private  String custLevel;//客户所属行业    @Column(name = "cust_address")    private  String custAddress;//客户地址    @Column(name = "cust_phone")    private  String custPhone;//客户联系方式    public Long getCustId() {        return custId;    }    public void setCustId(Long custId) {        this.custId = custId;    }    public String getCustName() {        return custName;    }    public void setCustName(String custName) {        this.custName = custName;    }    public String getCustSource() {        return custSource;    }    public void setCustSource(String custSource) {        this.custSource = custSource;    }    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 getCustAddress() {        return custAddress;    }    public void setCustAddress(String custAddress) {        this.custAddress = custAddress;    }    public String getCustPhone() {        return custPhone;    }    public void setCustPhone(String custPhone) {        this.custPhone = custPhone;    }    @Override    public String toString() {        return "Customer{" +                "custId=" + custId +                ", custName=‘" + custName + ‘\‘‘ +                ", custSource=‘" + custSource + ‘\‘‘ +                ", custIndustry=‘" + custIndustry + ‘\‘‘ +                ", custLevel=‘" + custLevel + ‘\‘‘ +                ", custAddress=‘" + custAddress + ‘\‘‘ +                ", custPhone=‘" + custPhone + ‘\‘‘ +                ‘}‘;    }}最后测试testFind类
/** * 根据id查询客户 */public  void testFind(){    //1根据工具类获取entityManager    EntityManager entityManager=JpaUtils.getEntityManager();    //3.获取事务对象,开启事务    EntityTransaction transaction=entityManager.getTransaction();//获取事务对象    transaction.begin();//开启事务    //3.增删改查--根据id查询客户    Customer customer=entityManager.find(Customer.class,1l);    System.out.print(customer);    //5.提交事务(回滚事务)    transaction.commit();    //6.释放资源    entityManager.close();}

相关推荐

踩风火轮的乌龟 / 0评论 2019-12-20