<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 1.配置数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean>
<!-- 2.配置JPA的EntityMangerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<!--指定包扫描-->
<property name="packagesToScan" value="com.springdata.test"></property>
<property name="jpaProperties">
<props>
<!-- 二级缓存相关 -->
<!--
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop>
-->
<!-- 生成的数据表的列的映射策略 -->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<!-- hibernate 基本属性 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 3.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 4.配置支持注解的事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 5.配置SpringData -->
<!-- 加入 jpa 的命名空间 -->
<!-- base-package:扫描Repository Bean 所在的 package -->
<jpa:repositories base-package="com.springdata.test"
entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
</beans>
复制代码
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/jpa
复制代码
class SpringDataTest {
private ApplicationContext ctx = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}
复制代码
链接成功打印出链接信息mysql
@Entity
@Table(name="JPA_PERSONS")
public class Person {
private Integer id;
private String lastName;
private String email;
private Date birth;
@GeneratedValue
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
只截取了一部分
复制代码
@Test
public void testJpa() {
}
复制代码
启动无报错后就会在数据库中生成一个表(数据是后加的)spring
/**
* 1.Repository是一个空接口,便是一个标记接口
* 2.若咱们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean.
* 归入到IOC容器中,进而能够在该接口中定义知足必定规范的方法.
* 3.实际上,也能够经过@RepositoryDefinition 注解来替代继承Repository 接口
*
*/
//传入的两个泛型:1.要处理的实体类的类型2.主键的类型
@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)
public interface PersonRepsotory{
//根据lastName 来获取对应的Person
Person getByLastName(String lastName);
}
复制代码
@Test
public void testGet() {
PersonRepsotory personRepsotory = ctx.getBean(PersonRepsotory.class);
Person person = personRepsotory.getByLastName("tom");
System.out.println(person);
}
复制代码
查询结果sql
//WHERE lastName LIKE ?% AND id < ?(以一个模糊作开始和id小于的条件作查询)
List<Person> getByLastNameStartingWithAndIdLessThan(String lastName,Integer id);
//WHERE lastName LIKE %? AND id < ?(以一个模糊作结束和id小于的条件作查询)
List<Person> getByLastNameEndingWithAndIdLessThan(String lastName,Integer id);
复制代码
测试类调用数据库
/*
* 条件查询的方法:以某字符开头而且id小于?的方法
*/
@Test
public void testKeyWords() {
List<Person> person = personRepsotory.getByLastNameStartingWithAndIdLessThan("c", 6);
System.out.println(person);
//(以一个模糊作结束和id小于的条件作查询)方法
List<Person> person1 = personRepsotory.getByLastNameEndingWithAndIdLessThan("c", 6);
System.out.println(person1);
}
复制代码
//查询id值最大的那个Person对象
//使用@Query 注解能够自定义 JPQL 语句以实现灵活的查询
@Query("SELECT p FROM Person p WHERE p.id = (SELECT max(p2.id) FROM Person p2)")
Person getMaxIdPerson();
复制代码
// 为@Query 注解传递参数的方式1:使用占位符.这种方式参数顺序必须一致
@Query("SELECT p FROM Person p WHERE p.lastName = ? AND p.email = ?")
List<Person> testQueryAnnotationParms1(String lastName, String email);
// @Query注解传递参数方式2:命名参数的方式,对参数名字进行绑定,能够不按顺序
@Query("SELECT p FROM Person p WHERE p.lastName = :lastName AND p.email = :email")
List<Person> testQueryAnnotationParms2(@Param("email") String email, @Param("lastName") String lastName);
复制代码