Springboot2整合JPA

本文介绍了如何使用Springboot2整合JPA和MySQL,通过配置application.properties文件,设置数据库连接信息,选择hibernate.ddl-auto属性。接着引入相关依赖,创建User实体类,定义接口UserRepository继承JpaRepository,实现CRUD操作,并创建控制器UserController完成用户操作接口。这是一个完整的Springboot JPA数据库操作的基础测试环境配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置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);
    }
}

本文完

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人海中的海盗

你的打赏将是对我的激励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值