=====================================================================
hello , Data JPA
=====================================================================
--------------------------------------------------------
1/ 配置xml
----------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName" 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"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--1/ 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${pwd}"></property>
</bean>
<!--2/ 配置 entityManager -->
<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="jpatest"/>
<!--配置生成sql表时的流程范式-->
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</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>
<!--配置事务管理-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!--配置支持注解的事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--spring data -->
<jpa:repositories base-package="jpatest" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
</beans>
----------------------------------------------------
2/创建实体类
----------------------------------------------------
@Entity
public class Student {
/**
* @Parm 主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
* @Parm 学生姓名
*/
private String name;
/**
* @Parm 学生年龄
*/
private Integer age;
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
----------------------------------------------------
3/创建接口
----------------------------------------------------
按照Spring Data的规范的规范,查询方法以find | read | get 开头,涉及查询条件时,
条件的属性用条件关键字连接,要注意的是:条件属性以首字母大写。
------------------------------------------
public interface StudentDao {
public List<Student> getList();
public boolean saveStudent(Student student);
}
----------------------------------------------------
4/测试
----------------------------------------------------
public class StudentTest {
private ApplicationContext applicationContext = null;
@Before
public void getContext () {
applicationContext = new ClassPathXmlApplicationContext("beans-new.xml");
}
@After
public void close(){
applicationContext= null;
}
@Test
public void EntyTest() {
StudentRepository studentRepository = applicationContext.getBean(StudentRepository.class);
System.out.println(studentRepository.getByName("zhangsan"));
}
----------------------------------------------------
Hello, Spring_Data_JPA! 学习笔记(一)
DataJPA实战
最新推荐文章于 2023-04-20 18:53:13 发布
本文详细介绍如何使用DataJPA进行数据库操作,包括配置Spring Data JPA环境、创建实体类、定义接口以及进行简单的测试。
724

被折叠的 条评论
为什么被折叠?



