SpringBoot+jpa简单的增删改查

 

1.application.yml

 

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/lhr?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
server:
  context-path: /demo

2.pom.xml

 



	4.0.0

	com.lhr
	demo
	0.0.1-SNAPSHOT
	jar

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.10.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

                
		
			mysql
			mysql-connector-java
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		

		
		
			org.projectlombok
			lombok
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



3.dao层

User.java

package com.lhr.dataobject;

import lombok.Data;

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

/**
 * Created by Administrator on 2018-02-24.
 */
@Entity
@Data
public class User {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    private Integer age;
}

UserRepository.java

package com.lhr.repository;

import com.lhr.dataobject.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * Created by Administrator on 2018-02-24.
 */
public interface UserRepository extends JpaRepository{

    //根据age查询
    public List findAllByAge(Integer age);
}

 

4.service层

 

UserService.java

package com.lhr.service;

import com.lhr.dataobject.User;

import java.util.List;

/**
 * Created by Administrator on 2018-02-24.
 */
public interface UserService {

    //获取所有用户列表
    List findAll();

    //新增,修改用户
    User save(User user);

    //根据id查询一个用户
    User findOne(Integer id);

    //根据ID删除一个用户
    void deleteOne(Integer id);

    //根据age查询用户
    List findByAge(Integer age);
}

UserServiceImpl.java

package com.lhr.service.impl;

import com.lhr.dataobject.User;
import com.lhr.repository.UserRepository;
import com.lhr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018-02-24.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserRepository repository;

    @Override
    public User save(User user) {
        return repository.save(user);
    }

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

    @Override
    public User findOne(Integer id) {
        return repository.findOne(id);
    }

    @Override
    public void deleteOne(Integer id) {
        repository.delete(id);
    }

    @Override
    public List findByAge(Integer age) {
        return repository.findAllByAge(age);
    }
}

 

5.controller层

 

UserController.java

package com.lhr.controller;

import com.lhr.dataobject.User;
import com.lhr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2018-02-24.
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public String login() {
        return "hello spring boot";
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @GetMapping("/userList")
    public List getUserList() {
        return userService.findAll();
    }

    /**
     * 新增一个用户
     *
     * @param name
     * @param age
     * @return
     */
    @PostMapping("/add")
    public User addUser(@RequestParam("name") String name,
                        @RequestParam("age") Integer age) {
        User user = new User();
        user.setName(name);
        user.setAge(age);

        return userService.save(user);
    }


    /**
     * 根据ID查询用户
     *
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public User findUser(@PathVariable("id") Integer id) {
        return userService.findOne(id);
    }

    @PutMapping("/user/{id}")
    public User updateUser(@PathVariable("id") Integer id,
                           @RequestParam("name") String name,
                           @RequestParam("age") Integer age) {

        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setAge(age);

        return userService.save(user);
    }

    /**
     * 根据ID删除用户
     *
     * @param id
     */
    @DeleteMapping("/user/{id}")
    public void deleteUser(@PathVariable("id") Integer id) {
        userService.deleteOne(id);
    }

    /**
     * 根据age查询用户
     *
     * @param age
     * @return
     */
    @GetMapping("/user/age/{age}")
    public List findByAge(@PathVariable("age") Integer age) {
        return userService.findByAge(age);
    }
}

 

你可能感兴趣的:(SpringBoot+jpa简单的增删改查)