工作已签,瞬间感觉没目标了。颓废了几天之后决定还是继续踏上Java编程之路,接下来几篇记录Spring Data的学习过程。
Spring Data : Spring 的一个子项目。用于简化数据库访问,支持NoSQL和关系数据存储。其主要目标是使数据库的访问变得方便快捷。
SpringData项目所支持 NoSQL存储:
SpringData项目所支持的关系数据存储技术:–JDBC–JPA
JPA Spring Data : 致力于减少数据访问层 (DAO) 的开发量. 开发者唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!
框架怎么可能代替开发者实现业务逻辑呢?
比如:当有一个 UserDao.findUserById()? 这样一个方法声明,大致应该能判断出这是根据给定条件的 ID 查询出满足条件的 User? 对象。SpringData JPA 做的便是规范方法的名字,根据符合规范的名字来确定方法需要实现什么样的逻辑。
使用 Spring Data JPA 进行持久层开发需要的四个步骤:
–配置Spring 整合JPA
–在 Spring 配置文件中配置 Spring Data,让 Spring 为声明的接口创建代理对象。配置了 后,Spring 初始化容器时将会扫描 base-package? 指定的包目录及其子目录,为继承Repository 或其子接口的接口创建代理对象,并将代理对象注册为Spring Bean,业务层便可以通过 Spring 自动封装的特性来直接使用该对象。
–声明持久层的接口,该接口继承? Repository,Repository 是一个标记型接口,它不包含任何方法,如必要,SpringData 可实现 Repository其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法。
–在接口中声明需要的方法。SpringData 将根据给定的策略(具体策略稍后讲解)来为其生成实现代码。
首先从万能的Hello world开始~~
项目的目录结构如下图
wKioL1YsRDrARvLUAAD8DAsPm6o372.jpg
applicationContext.xml配置如下
? ? 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/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
? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
?
? ?
? ?
?
? ?
? ?
?
? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
?
? ? ? ?
? ?
?
? ?
? ? ? ? ? ? class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
? ? ? ?
? ? ? ?
? ? ? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? org.hibernate.cfg.ImprovedNamingStrategy
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? org.hibernate.dialect.MySQL5InnoDBDialect
? ? ? ? ? ? ? ? true
? ? ? ? ? ? ? ? true
? ? ? ? ? ? ? ? update
? ? ? ? ? ?
? ? ? ?
? ?
?
? ?
? ?
? ? ? ?
? ?
?
? ?
? ?
?
? ?
? ?
? ?
? ? ? ? ? ? entity-manager-factory-ref="entityManagerFactory">
?
db.properties配置如下
jdbc.user=root
jdbc.password=
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
实体类User
?@Table(name="user")
@Entity
public class User {
? ? @GeneratedValue
? ? @Id
? ? private Integer id;
? ? private String username;
? ? private String password;
?
? ? //省略getter setter
?
? ? @Override
? ? public String toString() {
? ? ? ? return "User [id=" + id + ", username=" + username + ", password="
? ? ? ? ? ? ? ? + password + "]";
? ? }
?
}
UserRepository接口
?public interface UserRepository extends Repository{
?
? ? public User getByUsername(String username);
? ?
? ? public User getById(Integer id);
}
测试类
?public class Test {
?
? ? private ApplicationContext applicationContext = null;
? ?
? ? {
? ? ? ? applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
? ? }
? ?
? ? @org.junit.Test
? ? public void testDataSource() {
? ? ? ? DataSource dataSource = applicationContext.getBean(DataSource.class);
? ? ? ? System.out.println(dataSource);
? ? }
? ?
? ? @org.junit.Test
? ? public void testJPA() {
? ? ? ? //测试自动生产数据库表
? ? ? ?
? ? }
? ?
? ? @org.junit.Test
? ? pub