1.创建SpringBoot项目
2.pom.xml添加JPA依赖,数据库MySQL依赖包
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tao</groupId> <artifactId>springboot_jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_jpa</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
3.在src/main/resouces配置application.properties
server.port: 9998
debug:true
#PRODUCTION
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/springboot_jpa?useUnicode=true&characterEncoding=UTF-8
spring.datasource.driverClassName: com.mysql.jdbc.Driver
spring.datasource.username: root
spring.datasource.password: root
spring.datasource.jmx-enabled=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1
spring.jpa.database = MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl:true
spring.jpa.hibernate.show_sql:true
#第一次create 以后update
spring.jpa.hibernate.ddl-auto:create
4.com.tao.springboot_jpa.model下建立User
package com.tao.springboot_jpa.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class User implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @Column(name = "name") private String name; private String password; 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
5.在com.tao.springboot_jpa.repository下建立UserRepository
package com.tao.springboot_jpa.repository; import java.util.List; import javax.transaction.Transactional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import com.tao.springboot_jpa.model.User; @Repository @Transactional public interface UserRepository extends JpaRepository<User, Long>{ /** * 通过用户名查询 * @param name * @return */ List<User> findByName(String name); /** * 自定义SQL查询 * @param id * @return */ @Query(value = "select id from user where id = ?1",nativeQuery = true) Long findId(Long id); }
6.在com.tao.springboot_jpa.action下建立UserAction
package com.tao.springboot_jpa.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.tao.springboot_jpa.model.User; import com.tao.springboot_jpa.service.UserService; @RestController @EnableAutoConfiguration @RequestMapping(value = "/users", method = RequestMethod.POST) public class UserAction { @Autowired private UserService userService; /** * 通过用户名查询用户 * @param name * @return */ @RequestMapping(value = "/findByName") public List<User> findByName(String name){ return this.userService.findByName(name); } /** * 通过ID查询ID * @param id * @return */ @RequestMapping(value = "/findId") public Long findId(Long id){ return this.userService.findId(id); } }
7.在com.tao.springboot_jpa.service下建立UserService
package com.tao.springboot_jpa.service; import java.util.List; import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; import com.tao.springboot_jpa.model.User; import com.tao.springboot_jpa.repository.UserRepository; @Service @Transactional public class UserService { private final UserRepository userRepository; @Autowired public UserService(UserRepository userRepository) { super(); this.userRepository = userRepository; } public JpaRepository<User, Long> getEntityRepository(){ return this.userRepository; } public List<User> findByName(String name){ return this.userRepository.findByName(name); } public Long findId(Long id){ return this.userRepository.findId(id); } }
8.运行测试
http://localhost:9998/users/findId?id=1
http://localhost:9998/users/findByName?name=tom