多平台统一管理软件接口,如何实现多平台统一管理软件接口
271
2022-11-25
Spring Data环境搭建实现过程解析
本节作为主要讲解Spring Data的环境搭建
JPA Spring Data :致力于减少数据访问层(DAO)的开发量。开发者唯一要做的就是声明持久层的接口,其他都交给Spring Data JPA来帮你完成!
使用Spring Data JPA进行持久层开发需要的四个步骤:
配置Spring 整合 JPA
在Spring配置文件中配置Spring Data,让Spring 为声明的接口创建代理对象。配置了
声明持久层的接口,该接口继承Repository。Repository是一个标记型接口,它不包含任何方法,如必要,Spring Data 可实现Repository其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法。
在接口中声明需要的方法。Spring Data将根据给定的策略,来为其生成实现代码。
环境搭建
1.所需要的包
① 加入spring包
spring-aop-4.3.8.RELEASE.jar
spring-aspects-4.3.8.RELEASE.jar
spring-beans-4.3.8.RELEASE.jar
spring-context-4.3.8.RELEASE.jar
spring-core-4.3.8.RELEASE.jar
spring-expression-4.3.8.RELEASE.jar
spring-jdbc-4.3.8.RELEASE.jar
spring-orm-4.3.8.RELEASE.jar
spring-tx-4.3.8.RELEASE.jar
spring-web-4.3.8.RELEASE.jar
spring-webmvc-4.3.8.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
② 加入hibernate包
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.11.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
③ 加jpa的包
hibernate-entitymanager-4.3.11.Final.jar
④ 加c3p0的包
c3p0-0.9.2.1.jar
hibernate-c3p0-4.3.11.Final.jar
mchange-commons-java-0.2.3.4.jar
⑤ 加mysql的驱动
mysql-connector-java-5.1.7-bin.jar
⑥加入springData
spring-data-commons-1.6.2.RELEASE.jar
spring-data-jpa-1.4.2.RELEASE.jar
⑦加入 slf4j-api-1.6.1.jar
2.Spring Bean配置文件
applicationContext.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xmlns:jpa="http://springframework.org/schema/data/jpa" xsi:schemaLocation="http://springframework.org/schema/beans http://wwwTAdlwjs.springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd http://springframework.org/schema/data/jpa http://springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd"> <http://;property name="password" value="${jdbc.password}"> entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx" xmlns:jpa="http://springframework.org/schema/data/jpa"
xsi:schemaLocation="http://springframework.org/schema/beans http://wwwTAdlwjs.springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd
http://springframework.org/schema/data/jpa http://springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd">
<http://;property name="password" value="${jdbc.password}">
entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager">
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager">
applicationContext.xml
3.数据库持久类
Person.java
package com.ntjr.springdata;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "JPA_PERSONS")
@Entity
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;
}
public String getLastName() {
return lastName;
}
@Column(name = "LAST_NAME")
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Person [id=" + id + ", lastName=" + lastName + ", email=" + email + ", birth=" + birth + "]";
}
}
4.DAO
PersonRepository.java
package com.ntjr.springdata;
import org.springframework.data.repository.Repository;
/**
*
* 1、实现Repository接口
* 2、通过注解的方式@RepositoryDefinition将一个bean定义为Repository接口
*/
public interface PersonRepsitory extends Repository
// 根据lastName获取对应的person
Person getByLastName(String lastName);
}
5.测试
SpringDataTest.java
package com.ntjr.springdata.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ntjr.springdata.Person;
import com.ntjr.springdata.PersonRepsitory;
public class SpringDataTest {
private ApplicationContext ctx = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void getPersonForLastName() {
PersonRepsitory personRepsitory = ctx.getBean(PersonRepsitory.class);
Person person = personRepsitory.getByLastName("AA");
System.out.println(person);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~