Spring Data JPA Hibernate实现

实体类

@Entity --Specifies that the class is an entity. This annotation is applied to the entity class.
					@Table(name = "b_relation") --Specifies the primary table for the annotated entity. Additional tables may be specified using SecondaryTable or SecondaryTables annotation.
					@Id --The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.
					@GeneratedValue(strategy = GenerationType.AUTO)  	--Provides for the specification of generation strategies for the values of primary keys. 
																							javax.persistence.GenerationType   --Defines the types of primary key generation strategies.
																							GenerationType.AUTO 
																							GenerationType.IDENTITY
																							GenerationType.SEQUENCE
																							GenerationType.TABLE
					@Column(name = "id", unique = true, nullable = false, precision = StaticConstant.EIGHTEEN)

SQL执行接口UserRespository
在服务实现中直接调用其方法
继承JpaRepository<User, String>或CrudRepository<User, String>

@Modifying --Indicates a method should be regarded as modifying query.
@Query --Annotation to declare finder queries directly on repository methods.
															value ="sql语句", nativeQuery = true 是否为原生sql
					@Query(value = "select count(1) from b_relation a  where a.id = ?1 and  a.num = ?2", nativeQuery = true)
					BigInteger queryInfo(String id, String num);
					
					@Modifying
					@Query(value = "delete from b_relation where id = ?1", nativeQuery = true)
					void delInfo(String userid);

配置文件 persistence.xml

	<?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.0" 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">
 <persistence-unit name="test-unit" transaction-type="RESOURCE_LOCAL">
 <provider>org.hibernate.ejb.HibernatePersistence</provider>
 <non-jta-data-source>java:/comp/env/jdbc/test</non-jta-data-source>
 <class>com.wxx.entity.User</class>
 <properties>
 <property name="hibernate.format_sql" value="false" />
 <property name="hibernate.show_sql" value="false" />
 <property name="hibernate.use_sql_comments" value="false" />
 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
 </properties>
 </persistence-unit>
 </persistence>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值