SpringBoot JPA实现增删改查

2021-03-29 21:26

阅读:702

标签:hiberna   roo   interface   wired   web   class   stp   else   vax   

pom.xml文件

可以拿走直接用

org.springframework.boot
            spring-boot-starter-web
        org.springframework.boot
            spring-boot-starter-test
            testorg.junit.vintage
                    junit-vintage-engine
                org.springframework.boot
            spring-boot-starter-data-jdbc
        org.springframework.boot
            spring-boot-starter-data-jpa
        org.springframework.boot
            spring-boot-starter-jdbc
        mysql
            mysql-connector-java
            runtimecom.alibaba
            druid
            1.1.10org.projectlombok
            lombok
            true

application.yml

注意这里需要先改一下后缀名

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root


  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

因为程序中碰到了bug

这里将sql语句打印出来了jpa:show-sql:true

User.java

package com.along.jpa_user.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "t_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

UserDao.java

package com.along.jpa_user.dao;

import com.along.jpa_user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface UserDao extends JpaRepository {

    List findAll();

    @Modifying
    @Transactional
    @Query(value = "insert into t_user value(?,?,?)", nativeQuery = true)
    int insertUser(@Param("id") int id,@Param("name")String name,@Param("age")int age);

    @Modifying
    @Transactional
    @Query(value = "delete from t_user where id=:id")
    int deleteById(@Param("id") int id);

    @Modifying
    @Transactional
    @Query(value = "update t_user t set t.name=:name,t.age=:age where t.id=:id")
    int updateUser(@Param("id") int id,@Param("name") String name,@Param("age") int age);
}

这里也可以使用jpa父接口自定好的方法

UserService.java

package com.along.jpa_user.service;

import com.along.jpa_user.entity.User;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserService {
    List findAll();
    int insertUser(int id,String name,int age);
    int deleteById(int id);
    int updateUser(int id,String name,int age);
}

UserServiceImpl.java

package com.along.jpa_user.service.Impl;

import com.along.jpa_user.dao.UserDao;
import com.along.jpa_user.entity.User;
import com.along.jpa_user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public List findAll() {
        return userDao.findAll();
    }

    @Override
    public int insertUser(int id,String name, int age) {
        return userDao.insertUser(id,name,age);
    }

    @Override
    public int deleteById(int id) {
        return userDao.deleteById(id);
    }

    @Override
    public int updateUser(int id, String name, int age) {
        return userDao.updateUser(id,name,age);
    }

}

UserController.java

package com.along.jpa_user.controller;

import com.along.jpa_user.entity.User;
import com.along.jpa_user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/findAll")
    public List findAll(){
        return userService.findAll();
    }

    @GetMapping("/insert")
    public String insert(@RequestParam("id")int id,@RequestParam("name") String name,@RequestParam("age") int age){
        int result=userService.insertUser(id,name,age);
        if(result>=1){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }

    @GetMapping("/delete")
    public String delete(@RequestParam("id")int id){
        int result=userService.deleteById(id);
        if(result>=1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @GetMapping("/update")
    public String update(@RequestParam("id")int id,@RequestParam("name")String name,@RequestParam("age")int age){
        int result=userService.updateUser(id, name, age);
        if(result>=1){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }
}

项目运行地址 http://localhost:8080/findAll

SpringBoot JPA实现增删改查

标签:hiberna   roo   interface   wired   web   class   stp   else   vax   

原文地址:https://www.cnblogs.com/alongg/p/13598270.html


评论


亲,登录后才可以留言!