Spring Data JPA例子代码[基于Spring Boot、Mysql]

网友投稿 225 2023-02-10


Spring Data JPA例子代码[基于Spring Boot、Mysql]

关于Spring Data

Spring社区的一个顶级工程,主要用于简化数据(关系型&非关系型)访问,如果我们使用Spring Data来开发程序的话,那么可以省去很多低级别的数据访问操作,如编写数据查询语句、DAO类等,我们仅需要编写一些抽象接口并定义相关操作即可,Spring会在运行期间的时候创建代理实例来实现我们接口中定义的操作。

关于Spring Data子项目

Spring Data拥有很多子项目,除了Spring Data Jpa外,还有如下子项目。

Spring Data Commons

Spring Data MongoDB

Spring Data Redis

Spring Data Solr

Spring Data Gemfire

Spring Data REST

Spring Data Neo4j

关于Spring Data Jpa

Spring Data Jpa是Spring Data的一个子项目,主要用于简化数据访问层的实现,使用Spring Data Jpa可以轻松实现增删改查、分页、排序等。

例子,Spring Boot + Spring Data Jpa

1、添加POM.FZgfTtXML文件

如下所示:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

demo

0.0.1-SNAPSHOT

jar

spring-data-jpa-example

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.4.4.RELEASE

UTF-8

UTF-8

1.7

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

mysql

mysql-connector-java

org.springframework.boot

spring-boot-starter-cache

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

demo

0.0.1-SNAPSHOT

jar

spring-data-jpa-example

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.4.4.RELEASE

UTF-8

UTF-8

1.7

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

mysql

mysql-connector-java

org.springframework.boot

spring-boot-starter-cache

org.springframework.boot

spring-boot-maven-plugin

其中,spring-boot-starter-parent会加载Spring Boot应用所需的所有默认配置;

spring-boot-starter-data-jpa会下载所有Spring Data Jpa所需的依赖;

添加spring-boot-starter-web是因为我们的工程是一个Web应用;

另外我们的数据库是mysql,所以还需要mysql-connector-java依赖;

由于使用了缓存,所以再添加一个spring-boot-starter-cache依赖;

2、编写实体类User

package com.example.domain;

import java.io.Serializable;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.NamedQuery;

@Entity

@NamedQuery(name = "User.findByName", query = "select name,address from User u where u.name=?1")

public class User implements Serializable

{

private static final long serialVersionUID = 1L;

@Id

long id;

@Column(name = "name")

String name;

@Column(name = "address")

String address;

public long getId()

{

return id;

}

public void setId(long id)

{

this.id = id;

}

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public String getAddress()

{

return address;

}

public void setAddress(String address)

{

this.address = address;

}

}

其它没啥好说的,注意下这里的@NamedQuery注解,大致意思就是让我们在Repository接口中定义的findByName方法不使用默认的查询实现,取而代之的是使用这条自定义的查询语句去查询,如果这里没有标注的话,会使用默认实现的。

3、编写Repository接口

这里将编写两个Repository接口,仅仅用于示例,实际中可以合并成一个:

UserJpaRepository

package com.example.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.domain.User;

public interface UserJpaRepository extends JpaRepository {

}

这里的UserJpaRepository接口实现了JpaRepository接口;

实际上JpaRepository实现了PagingAndSortingRepository接口,PagingAndSortingRepository接口实现了CrudRepository接口,CrudRepository接口实现了Repository接口;

简单说明下:

Repository接口是一个标识接口,里面是空的;

CrudRepository接口定义了增删改查方法;

PagingAndSortingRepository接口用于分页和排序;

由于JpaRepository接口继承了以上所有接口,所以拥有它们声明的所有方法;

另外注意下,以findAll方法为例,JpaRepository接口返回的是List, PagingAndSortingRepository和CrudRepository返回的是迭代器;

UserRepository

package com.example.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;

import org.springframework.data.repository.Repository;

import org.springframework.data.repository.query.Param;

import com.example.domain.User;

public interface UserRepository extends Repository

{

List findByNameAndAddress(String name, String address);

@Query(value = "from User u where u.name=:name")

List findByName1(@Param("name") String name);

@Query(value = "select * from #{#entityName} u where u.name=?1", nativeQuery = true)

List findByName2(String name);

List findByName(String name);

}

这里的UserRepository接口主要定义了一些查询方法;

比如这里的findByNameAndAddress和findByName方法,我们是不需要额外定义其它查询语句就可以直接执行的,Spring Data Jpa会根据实体类的属性名字以及方法名自动实现该方法;PS:由于我们在实体类中声明了@NamedQuery注解,实际上findByName方法会使用@NamedQuery注解标注的查询语句去查询;

另外这里的findByName1方法使用了HQL语句查询;

findByName2方法使用了原始的sql语句查询;

4、编写Service

service接口:

package com.example.service;

import java.util.List;

import com.example.domain.User;

public interface IUserService

{

public List findAll();

public void saveUser(User book);

public User findOne(long id);

public void delete(long id);

public List findByName(String name);

}

接口实现类:

package com.example.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.example.domain.User;

import com.example.repository.UserRepository;

import com.example.repository.UserJpaRepository;

import com.example.service.IUserService;

@Service

@Transactional

public class UserServiceImpl implements IUserService

{

@Autowired

private UserJpaRepository userJpaRepository;

@Autowired

private UserRepository userRepository;

public List findAll()

{

return userJpaRepository.findAll();

}

public List findByName(String name)

{

List userList1 = userRepository.findByName1(name);

List userList2 = userRepository.findByName2(name);

List

System.out.println("userList1:" + userList1);

System.out.println("userList2:" + userList2);

System.out.println("userList3:" + userList3);

return userRepository.findByName(name);

}

public void saveUser(User book)

{

userJpaRepository.save(book);

}

@Cacheable("users")

public User findOne(long id)

{

System.out.println("Cached Pages");

return userJpaRepository.findOne(id);

}

public void delete(long id)

{

userJpaRepository.delete(id);

}

}

这个没啥好说的,调用Repository接口接口的方法即可。

5、编写Controller

Controller也没啥好说的,调用Service即可,注意下这里的Controller使用@RestController注解来标注,另外URL路径命名按照RESTful风格来命名;

package com.example.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.example.domain.User;

import com.example.service.IUserService;

@RestController

@RequestMapping(value = "/users")

public class UserController

{

@Autowired

private IUserService userService;

@RequestMapping(value = "/add/{id}/{name}/{address}")

public User addUser(@PathVariable int id, @PathVariable String name,

@PathVariable String address)

{

User user = new User();

user.setId(id);

user.setName(name);

user.setAddress(address);

userService.saveUser(user);

return user;

}

@RequestMapping(value = "/delete/{id}")

public void deleteBook(@PathVariable int id)

{

userService.delete(id);

}

@RequestMapping(value = "/")

public List getBooks()

{

return userService.findAll();

}

@RequestMapping(value = "/{id}")

public User getUser(@PathVariable int id)

{

User user = userService.findOne(id);

return user;

}

@RequestMapping(value = "/search/name/{name}")

public List getBookByName(@PathVariable String name)

{

List users = userService.findByName(name);

return users;

}

}

6、配置datasource

在application.properties文件中添加如下配置:

spring.jpa.show-sql = true

logging.level.org.springframework.data=DEBUG

spring.jpa.hibernate.ddl-auto=

spring.datasource.url=jdbc:mysql://localhost:3306/demo

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

如果你使用STS IDE的话,这些属性配置都会自动提示的,省的去查找。

想查看spring.datasource的配置,可以参考这个类:DataSourceProperties.java

7、编写启动类

比较简单,注意下该类所属的包级别要大于或等于其它类,以保证其它类的注解可以被扫描到。

package com.example;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication

@EnableCaching

public class SpringDataJpaExampleApplication {

public static void main(String[] args) {

SpringApplication.run(SpringDataJpaExampleApplication.class, args);

}

}

运行、测试程序

启动main方法,或打成jar包运行;

浏览器输入以下URL,测试即可:

http://localhost:8080/users/

http://localhost:8080/users/add/100/110/111

http://localhost:8080/users/delete/100

http://localhost:8080/users/2

http://localhost:8080/users/search/name/2

程序源码

https://github.com/peterchenhdu/spring-data-jpa-example

参考资料

http://docs.spring.io/spring-data/jpa/docs/1.11.0.RELEASE/reference/html/

http://javabeat.net/spring-data-jpa/

https://spring.io/guides/gs/caching/

System.out.println("userList1:" + userList1);

System.out.println("userList2:" + userList2);

System.out.println("userList3:" + userList3);

return userRepository.findByName(name);

}

public void saveUser(User book)

{

userJpaRepository.save(book);

}

@Cacheable("users")

public User findOne(long id)

{

System.out.println("Cached Pages");

return userJpaRepository.findOne(id);

}

public void delete(long id)

{

userJpaRepository.delete(id);

}

}

这个没啥好说的,调用Repository接口接口的方法即可。

5、编写Controller

Controller也没啥好说的,调用Service即可,注意下这里的Controller使用@RestController注解来标注,另外URL路径命名按照RESTful风格来命名;

package com.example.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.example.domain.User;

import com.example.service.IUserService;

@RestController

@RequestMapping(value = "/users")

public class UserController

{

@Autowired

private IUserService userService;

@RequestMapping(value = "/add/{id}/{name}/{address}")

public User addUser(@PathVariable int id, @PathVariable String name,

@PathVariable String address)

{

User user = new User();

user.setId(id);

user.setName(name);

user.setAddress(address);

userService.saveUser(user);

return user;

}

@RequestMapping(value = "/delete/{id}")

public void deleteBook(@PathVariable int id)

{

userService.delete(id);

}

@RequestMapping(value = "/")

public List getBooks()

{

return userService.findAll();

}

@RequestMapping(value = "/{id}")

public User getUser(@PathVariable int id)

{

User user = userService.findOne(id);

return user;

}

@RequestMapping(value = "/search/name/{name}")

public List getBookByName(@PathVariable String name)

{

List users = userService.findByName(name);

return users;

}

}

6、配置datasource

在application.properties文件中添加如下配置:

spring.jpa.show-sql = true

logging.level.org.springframework.data=DEBUG

spring.jpa.hibernate.ddl-auto=

spring.datasource.url=jdbc:mysql://localhost:3306/demo

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

如果你使用STS IDE的话,这些属性配置都会自动提示的,省的去查找。

想查看spring.datasource的配置,可以参考这个类:DataSourceProperties.java

7、编写启动类

比较简单,注意下该类所属的包级别要大于或等于其它类,以保证其它类的注解可以被扫描到。

package com.example;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication

@EnableCaching

public class SpringDataJpaExampleApplication {

public static void main(String[] args) {

SpringApplication.run(SpringDataJpaExampleApplication.class, args);

}

}

运行、测试程序

启动main方法,或打成jar包运行;

浏览器输入以下URL,测试即可:

http://localhost:8080/users/

http://localhost:8080/users/add/100/110/111

http://localhost:8080/users/delete/100

http://localhost:8080/users/2

http://localhost:8080/users/search/name/2

程序源码

https://github.com/peterchenhdu/spring-data-jpa-example

参考资料

http://docs.spring.io/spring-data/jpa/docs/1.11.0.RELEASE/reference/html/

http://javabeat.net/spring-data-jpa/

https://spring.io/guides/gs/caching/


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于SpringBoot实现用户身份验证工具
下一篇:连接共享文件夹(如何共享文件给其他电脑)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~