码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot JPA实现增删改查

时间:2020-09-14 19:20:49      阅读:50      评论:0      收藏:0      [点我收藏+]

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

pom.xml文件

可以拿走直接用

<dependencies>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- druid数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

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<User,Integer> {

    List<User> 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<User> 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<User> 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<User> 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!