SpringBoot整合JPA数据源

    xiaoxiao2022-07-05  134

    1.添加依赖

    <!-- 整合spring-data-jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

    2.配置数据源和和JPA

    3.创建实体,JPA和Controller

    1.这里的实体需要加上注解,@Entity和@Table(name=“表名”),以及id和每个属性上都需要写上对应的字段,另外如果不是数据库的字段,添加属性需要写上 @Transient注解 2.新建user实体的jpa 定义接口,集成各种类就可以使用了 3.测试用的controller 我们UserJPA继承了JpaRepository接口(SpringDataJPA提供的简单数据操作接口)、JpaSpecificationExecutor(SpringDataJPA提供的复杂查询接口)、Serializable(序列化接口)。我们并不需要做其他的任何操作了,因为SpringBoot以及SpringDataJPA会为我们全部搞定,SpringDataJPA内部使用了类代理的方式让继承了它接口的子接口都以spring管理的Bean的形式存在,也就是说我们可以直接使用@Autowired注解在spring管理bean使用, 1.查询所有用户 2.新增用户 3.删除用户,查找用户

    4.在操作是查看控制台打印sql语句

    最新回复(0)