配置application.properties
##数据库配置
##数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
##数据库用户名
spring.datasource.username=root
##数据库密码
spring.datasource.password=root
##数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##validate 加载hibernate时,验证创建数据库表结构
##create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
##create-drop 加载hibernate时创建,退出是删除表结构
##update 加载hibernate自动更新数据库结构
##none 启动时不做任何操作
spring.jpa.hibernate.ddl-auto=create
##控制台打印sql
spring.jpa.show-sql=true
配置pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- JPA依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- WEB依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
到这里整合结束,下面就是一个简单的测试开发环境是否OK。
创建User实体类
import javax.persistence.*;
@Entity
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false,unique = true)
private String userName;
@Column
private String userPassword;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public User(Long id,String userName, String userPassword) {
this.id = id;
this.userName = userName;
this.userPassword = userPassword;
}
public User(String userName, String userPassword) {
this.userName = userName;
this.userPassword = userPassword;
}
public User() {
}
}
创建接口UserRepository
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface UserRepository extends JpaRepository<User,Long> {
@Query("SELECT u FROM User u WHERE userName = :userName")
List<User> findAllByUserName(@Param("userName") String userName);
}
创建控制器类UserController
import com.springboot.repsitory.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
//http://localhost:8080/saveUser?userName=lisi&userPassword=123
@GetMapping(value = "/saveUser")
public void saveUser(String userName,String userPassword){
User user = new User(userName, userPassword);
userRepository.save(user);
}
//http://localhost:8080/updateUser?Id=1&userName=lisi&userPassword=1111
@GetMapping(value = "/updateUser")
public void updateUser(Long Id,String userName,String userPassword){
User user = new User(Id,userName, userPassword);
userRepository.save(user);
}
//http://localhost:8080/deleteUser?Id=1
@GetMapping(value = "/deleteUser")
public void deleteUser(Long Id){
userRepository.deleteById(Id);
}
//http://localhost:8080/getUserById?Id=1
@GetMapping(value = "/getUserById")
public Optional<User> getUserById(Long Id){
return userRepository.findById(Id);
}
//http://localhost:8080/getUserByUserName?userName=lisi
@GetMapping(value = "/getUserByUserName")
public List<User> getUserByUserName(String userName){
return userRepository.findAllByUserName(userName);
}
}
本文完