栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Data JPA快速入门,java面试常见问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Data JPA快速入门,java面试常见问题

org.aspectj

aspectjweaver

1.6.8

org.springframework

spring-aop

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-orm

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-core

${spring.version}

org.hibernate

hibernate-core

${hibernate.version}

org.hibernate

hibernate-entitymanager

${hibernate.version}

org.hibernate

hibernate-validator

5.2.1.Final

c3p0

c3p0

${c3p0.version}

mysql

mysql-connector-java

${mysql.version}

org.springframework.data

spring-data-jpa

1.9.0.RELEASE

org.springframework

spring-test

4.2.4.RELEASE

javax.el

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ifactId>javax.el-api

2.2.4

org.glassfish.web

javax.el

2.2.4

2.搭建Spring Data JPA的开发环境

新建applicationContext.xml文件,整合Spring Data JPA与Spring

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:jdbc=“http://www.springframework.org/schema/jdbc” xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:jpa=“http://www.springframework.org/schema/data/jpa” xmlns:task=“http://www.springframework.org/schema/task”

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

create

transaction-manager-ref=“transactionManager”

entity-manager-factory-ref=“entityManagerFactory”>

tx:attributes

aop:config

.(…))” />

3. 创建Customer实体类对象,​​​​​​​使用JPA注解配置映射关系

实体类与表的映射关系:

  • @Entity:声明实体类

  • @Table:建立实体类和表的映射关系

类中属性与表中字段的映射关系:

  • @Id:声明当前私有属性为主键

  • @GeneratedValue:配置主键的生成策略

  • @Column:配置与表中字段的映射关系

@Entity //声明实体类

@Table(name=“cst_customer”) //建立实体类和表的映射关系

public class Customer {

@Id//声明当前私有属性为主键

@GeneratedValue(strategy=GenerationType.IDENTITY) //配置主键的生成策略

@Column(name=“cust_id”) //指定和表中cust_id字段的映射关系

private Long custId;

@Column(name=“cust_name”) //指定和表中cust_name字段的映射关系

private String custName;

@Column(name=“cust_source”)//指定和表中cust_source字段的映射关系

private String custSource;

@Column(name=“cust_industry”)//指定和表中cust_industry字段的映射关系

private String custIndustry;

@Column(name=“cust_level”)//指定和表中cust_level字段的映射关系

private String custLevel;

@Column(name=“cust_address”)//指定和表中cust_address字段的映射关系

private String custAddress;

@Column(name=“cust_phone”)//指定和表中cust_phone字段的映射关系

private String custPhone;

}

3. 编写符合Spring Data JPA规范的Dao层接口

Spring Data JPA是spring提供的一款对于数据访问层(Dao层)的框架,使用Spring Data JPA,只需要按照框架的规范提供dao接口,不需要实现类就可以完成数据库的增删改查、分页查询等方法的定义,极大的简化了我们的开发过程。

定义符合规范的Dao层接口:

  • 1. 创建一个Dao层接口,不需要实现类

  • 2. 继承JpaRepository和JpaSpecificationExecutor,提供相应的泛型

JpaRepository<操作实体的类型,实体类中主键属性的类型>

JpaSpecificationExecutor<操作实体的类型>

public interface CustomerDao extends JpaRepository, JpaSpecificationExecutor {

}

4.​​​​​​​测试代码:

完成了Spring Data JPA的环境搭建,并且编写了符合Spring Data JPA 规范的Dao层接口之后,就可以使用定义好的Dao层接口进行客户的基本CRUD操作。

4.1 定义测试类CustomerDaoTest,并配置spring单元测试的支持

@RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试

@ContextConfiguration(locations = “classpath:applicationContext.xml”)//指定spring容器的配置信息

public class CustomerDaoTest {

@Autowired

private CustomerDao customerDao;

}

4.2 测试根据id查询

根据id从数据库查询:

  • findOne:立即加载

  • getOne:延迟加载,返回的是一个客户端的动态代理对象, 什么时候用什么时候查询

@Test

public void testFindOne(){

Customer customer=customerDao.findOne(1l);

System.out.println(customer);

}

@Test

@Transactional

public void testGetOne(){

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644120.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号