SpringData 完全入门指南

SpringData 笔记 1. 配置项目 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> <modelVersion>4.0.0</modelVersion> <groupId>com.lwen</groupId> <artifactId>SpringData</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--MySQL Driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.5.RELEASE</version> </dependency> <!--spring data jpa--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.8.0.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.6.Final</version> </dependency> </dependencies> </project> 2.bean.xml <?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/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--1 配置数据源--> <bean> <property value="com.mysql.jdbc.Driver"/> <property value="root"/> <property value=""/> <property value="jdbc:mysql://localhost:3306/spring_data"/> </bean> <!--2 配置EntityManagerFactory--> <bean> <property ref="dataSource"/> <property> <bean/> </property> <property value="com.lwen"/> <property> <props> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <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> <property ref="entityManagerFactory"/> </bean> <!--4 配置支持注解的事务--> <tx:annotation-driven/> <!--5 配置spring data--> <jpa:repositories base-package="com.lwen" entity-manager-factory-ref="entityManagerFactory"/> <context:component-scan base-package="com.lwen"/> </beans> 3.实体类 package com.lwen.entry; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * Student实体类 */ @Entity public class Student { @Id@GeneratedValue private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ",\'' + ", age=" + age + '}'; } }

这里注意的一点就是我们在使用注解的时候一定要注意导入的包,我们都是导入的javax中的类。

2.Repository 1. 使用

这个东西是SpringData的核心,但是我们实际去看的时候会发现他是一个空接口,也就是这个一个标记接口。我们自己的接口必须继承这个接口才会具备查询的功能,所以说我们的自定的查询器必须要继承这个接口。这个接口是泛型接口也就是我们需要输入两个参数,第一个就是我们查询器的类型,针对于那个表进行查询,另外一个就是表的Id的类型,这个类型必须是序列化接口的子类型,所以说不能使用基本类型,我们只能使用包装类型。

public interface EmployeeRepository extends Repository<Employee,Integer> { Employee findByName(String name); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxzsd.html