spring-boot 简易实现RESTful 接口
目录结构
1 导入pom依赖
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<flyway.version>5.2.1</flyway.version>
<mysql_version>8.0.13</mysql_version>
</properties>
<dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>${flyway.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql_version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2 配置文件
application.properties
server.port=8888
spring.jpa.show-sql = true
logging.level.org.springframework.data=DEBUG
spring.jpa.hibernate.ddl-auto=
spring.datasource.url=jdbc:mysql://localhost:3306/kxs_jpa_test?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3 entity实体类
User.class
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;
}
@Override
public String toString()
{
return this.id + "," + this.name + "," + this.address;
}
}
4 repository层
UserRepository.class
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<User, Long>
{
List<User> findByNameAndAddress(String name, String address);
@Query(value = "from User u where u.name=:name")
List<User> findByName1(@Param("name") String name);
@Query(value = "select * from #{#entityName} u where u.name=?1", nativeQuery = true)
List<User> findByName2(String name);
List<User> findByName(String name);
}
5 service层
IUserService.class
package com.example.service;
import java.util.List;
import com.example.domain.User;
public interface IUserService
{
public List<User> findAll();
public void saveUser(User book);
public User findOne(long id);
public void delete(long id);
public List<User> findByName(String name);
}
UserServiceImpl.class 实现类
package com.example.service.impl;
import java.util.List;
import java.util.Optional;
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<User> findAll()
{
return userJpaRepository.findAll();
}
public List<User> findByName(String name)
{
List<User> userList1 = userRepository.findByName1(name);
List<User> userList2 = userRepository.findByName2(name);
List<User> userList3 = userRepository.findByNameAndAddress(name, "3");
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");
Optional<User> SS = userJpaRepository.findById(id);
if(SS.isPresent())
{
return SS.get();
}
return null;
}
public void delete(long id)
{
userJpaRepository.deleteById(id);
}
}
6 Controller层
UserController.class
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<User> 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<User> getBookByName(@PathVariable String name)
{
List<User> users = userService.findByName(name);
return users;
}
}
7 数据库表设计
命名:遵循flyway规则
V2019.02.06.20.14.01__create_engineparent_related_tables.sql
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`address` varchar(1000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
8 启动类
SpringDataJpaExampleApplication.class
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);
}
}