JPA(二)JPA配置

JPA(二)JPA配置一 依赖导入 以 maven 工程导入坐标为例 lt properties gt lt project build sourceEncodi gt UTF 8 lt project build sourceEncodi gt lt project hibernate version gt 5 0 7 Final lt project hibe

一、依赖导入,以maven 工程导入坐标为例

 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.hibernate.version>5.0.7.Final</project.hibernate.version> </properties> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- hibernate 对 jpa 的支持包 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${project.hibernate.version}</version> </dependency> <!-- c3p0 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${project.hibernate.version}</version> </dependency> <!-- log 日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- Mysql and MariaDB --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies>

二、实体类

package com.it.jpa.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity //声明实体类 @Table(name="cst_customer")//建立实体类和表的映射关系 public class Customer { 
    @Id//声明当前私有属性为主键 @GeneratedValue(strategy=GenerationType.IDENTITY) //配置主键的生成策略 @Column(name="cust_id") //指定和表中 cust_id 字段的映射关系 private Long custId; @Column(name="cust_name") //指定和表中 cust_name 字段的映射关系 private String custName; @Column(name="cust_source")//指定和表中 cust_source 字段的映射关系 private String custSource; @Column(name="cust_industry")//指定和表中 cust_industry 字段的映射关系 private String custIndustry; @Column(name="cust_level")//指定和表中 cust_level 字段的映射关系 private String custLevel; @Column(name="cust_address")//指定和表中 cust_address 字段的映射关系 private String custAddress; @Column(name="cust_phone")//指定和表中 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; } 
常用注解的说明 @Entity 作用:指定当前类是实体类。 @Table 作用:指定实体类和表之间的对应关系。 属性: name:指定数据库表的名称 @Id 作用:指定当前字段是主键。 @GeneratedValue 作用:指定主键的生成方式。。 属性: generator:指定引用 hibernate 中声明的主键策略 strategy :指定主键生成策略。 1)、AUTO 自动选择一个最适合底层数据库的主键生成策略。如MySQL会自动对应auto increment。这个是默认选项,即如果只写@GeneratedValue,等价于@GeneratedValue(strategy=GenerationType.AUTO)。   2)、IDENTITY 表自增长字段,Oracle不支持这种方式。   3)、SEQUENCE 通过序列产生主键,MySQL不支持这种方式。   4)、TABLE 通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植。不同的JPA实现商生成的表名是不同的,如 OpenJPA生成openjpa_sequence_table表,Hibernate生成一个hibernate_sequences表,而TopLink则生成sequence表。这些表都具有一个序列名和对应值两个字段,如SEQ_NAME和SEQ_COUNT。 @Column 作用:指定实体类属性和数据库表之间的对应关系 属性: name:指定数据库表的列名称。 unique:是否唯一 nullable:是否可以为空 inserttable:是否可以插入 updateable:是否可以更新 columnDefinition: 定义建表时创建此列的 DDL secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在 从表的名字

三、JPA 核心配置文件

<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <!-- 在 maven 工程的 resources 路径下创建一个名为 META-INF 的文件夹, 在此文件夹下创建一个名为persistence.xml 的配置文件。 注意:META-INF文件夹名称不能修改。persistence.xml 文件名称不能改。 --> <!-- 配置持久化单元 name: 持久化单元名称 transaction-type: 事务类型 RESOURCE_LOCAL: 本地事务管理 JTA:分布式事务管理 --> <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL"> <!--配置 JPA 规范的服务提供商 --> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <!-- 数据库驱动 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <!-- 数据库地址 --> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/springdb" /> <!-- 数据库用户名 --> <property name="javax.persistence.jdbc.user" value="root" /> <!-- 数据库密码 --> <property name="javax.persistence.jdbc.password" value="root" /> <!--jpa 提供者的可选配置:我们的 JPA 规范的提供者为 hibernate,所以 jpa 的核心配 置中兼容 hibernate 的配 --> <!-- show_sql: 显示sql format_sql: 格式化sql hbm2ddl.auto: 自动创建|更新|验证数据库表结构 --> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233469.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Centos7关闭selinux命令「建议收藏」

    关闭selinux步骤0x01用vi修改selinux的配置文件vi/etc/selinux/config0x02修改#SELINUX=enforcing为SELINUX=disabled这里就不修改了,注释掉好了,再直接复制修改为SELINUX=disabledselinux的工作模式enforcing强制模式permissive宽容模式disabled关闭什么是selinux?selinux是Linux的一种安全子系统 Linux中的权限管..

    2022年4月18日
    230
  • 【LeetCode】Agorithms 题集(一)

    【LeetCode】Agorithms 题集(一)

    2022年2月3日
    49
  • 学习率衰减之余弦退火(CosineAnnealing)

    学习率衰减之余弦退火(CosineAnnealing)1引言当我们使用梯度下降算法来优化目标函数的时候,当越来越接近Loss值的全局最小值时,学习率应该变得更小来使得模型尽可能接近这一点,而余弦退火(Cosineannealing)可以通过余弦函数来降低学习率。余弦函数中随着x的增加余弦值首先缓慢下降,然后加速下降,再次缓慢下降。这种下降模式能和学习率配合,以一种十分有效的计算方式来产生很好的效果。在论文StochasticGradientDescentwithWarmRestarts中介绍主要介绍了带重启的随机梯度下降算法(SGDR),其

    2022年5月1日
    60
  • 前端MD5加密——js-md5[通俗易懂]

    前端MD5加密——js-md5[通俗易懂]1.概述是通过前台js加密的方式对密码等私密信息进行加密的工具2.js加密的好处(1)用js对私密信息加密可避免在网络中传输明文信息,被人截取数据包而造成数据泄露。(2)避免缓存中自动缓存密码。比如在使用谷歌浏览器登陆时,输入的用户名和密码会自动缓存,下次登陆时无需输入密码就可以实现登陆,这样就给别人留下漏洞,当别人用你电脑登陆或把input的type改为test那么你的密码就泄露…

    2022年7月11日
    24
  • Python字符串转换为日期时间– strptime()「建议收藏」

    Python字符串转换为日期时间– strptime()「建议收藏」Wecanconvertastringtodatetimeusingstrptime()function.Thisfunctionisavailableindatetimeandtimemodulestoparseastringtodatetimeandtimeobjectsrespectively.我们可以使用strptime()函数将字…

    2022年6月2日
    104
  • Spring StoredProcedure调用Oracle函数各种异常解决方法

    Spring StoredProcedure调用Oracle函数各种异常解决方法其实也不是各种异常解决方法,只是出现了太多的异常我实在不知道有哪些,下面列举一下吧:1.PLS-00306:wrongnumberortypesofargumentsincallto’QUERYUSER’ORA-06550:line1,column7:PL/SQL:Statementignored原因:这个问题是少参数,或者类型不对,我的原因是函数的…

    2022年7月26日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号