CrudRepository接口实战

一 pom


     4.0.0
     org.fkit
     crudrepositorytest
     0.0.1-SNAPSHOT
     jar
     crudrepositorytest
     http://maven.apache.org
     
     
           org.springframework.boot
           spring-boot-starter-parent
           2.0.0.RELEASE
           
     
     
           UTF-8
           UTF-8
           1.8
     
     
           
           
                org.springframework.boot
                spring-boot-starter-web
           
           
           
                org.springframework.boot
                spring-boot-starter-thymeleaf
           
           
           
                mysql
                mysql-connector-java
           
           
           
                org.springframework.boot
                spring-boot-starter-data-jpa
           
           
                junit
                junit
                test
           
     

二 启动类

package org.fkit.crudrepositorytest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {
        // SpringApplication 用于从main方法启动Spring应用的类。
        SpringApplication.run(App.class, args);
    }
}

三 持久化类

package org.fkit.crudrepositorytest.bean;


import java.io.Serializable;


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


@Entity
// 用于标记持久化类,Spring Boot项目加载后会自动根据持久化类建表
@Table(name="tb_user")
public class User implements Serializable{


    private static final long serialVersionUID = 1L;


    /**
     * 使用@Id指定主键. 使用代码@GeneratedValue(strategy=GenerationType.AUTO)
     * 指定主键的生成策略,mysql默认的是自增长。
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;// 主键.


    private String username;// 姓名 username


    private String loginName;


    private char sex;// 性别


    private int age; // 年龄


    public int getId() {
        return id;
    }


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


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public String getLoginName() {
        return loginName;
    }


    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }


    public char getSex() {
        return sex;
    }


    public void setSex(char sex) {
        this.sex = sex;
    }


    public int getAge() {
        return age;
    }


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

四 控制器

package org.fkit.crudrepositorytest.controller;


import javax.annotation.Resource;


import org.fkit.crudrepositorytest.bean.User;
import org.fkit.crudrepositorytest.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {
    
    // 注入UserService
    @Resource
    private UserService userService;


    @RequestMapping("/save")
    public String save() {
        User user = new User();
        user.setLoginName("dlei");
        user.setUsername("徐磊");
        user.setSex('男');
        user.setAge(3);
        user = userService.save(user);
        System.out.println("保存数据成功,返回的结果:" + user);
        return "保存数据成功!";
    }


    @RequestMapping("/update")
    public String update() {
        // 修改的对象必须是持久化对象,所以先从数据库查询出id为1的对象进行修改
        User user = userService.getById(1);
        userService.update(user);
        return "修改数据成功!";
    }


    @RequestMapping("/delete")
    public String delete() {
        userService.delete(1);
        return "删除数据成功!";
    }


    @RequestMapping("/getAll")
    public Iterable getAll() {
        return userService.getAll();
    }
}

五 Service层

package org.fkit.crudrepositorytest.service;


import java.util.Optional;


import javax.annotation.Resource;
import javax.transaction.Transactional;


import org.fkit.crudrepositorytest.bean.User;
import org.fkit.crudrepositorytest.repository.UserRepository;
import org.springframework.stereotype.Service;


@Service
public class UserService {


    // 注入UserRepository
    @Resource
    private UserRepository userRepository;


    /**
     * save,update ,delete 方法需要绑定事务. 使用@Transactional进行事务的绑定.
     *
     * 保存对象
     * @param User
     * @return 包含自动生成的id的User对象        
     */
    @Transactional
    public User save(User User) {
        return userRepository.save(User);
    }


    /**
     * 根据id删除对象
     *
     * @param id
     */
    @Transactional
    public void delete(int id) {
        userRepository.deleteById(id);
        
    }


    /**
     * 查询所有数据
     *
     * @return 返回所有User对象
     */
    public Iterable getAll() {
        return userRepository.findAll();
    }
    
    /**
     * 根据id查询数据
     *
     * @return  返回id对应的User对象
     */
    public User getById(Integer id) {
        // 根据id查询出对应的持久化对象
        Optional op = userRepository.findById(id);
         return op.get();
    }
    /**
     * 修改用户对象数据,持久化对象修改会自动更新到数据库
     *
     * @param user
     */
    @Transactional
    public void update(User user) {
        // 直接调用持久化对象的set方法修改对象的数据
        user.setUsername("孙悟空");
        user.setLoginName("swk");
    }


}

六 repository层

package org.fkit.crudrepositorytest.repository;


import org.fkit.crudrepositorytest.bean.User;
import org.springframework.data.repository.CrudRepository;


public interface UserRepository extends CrudRepository{


}

七 创建数据库springdatajpa

八 测试

CrudRepository接口实战_第1张图片

CrudRepository接口实战_第2张图片

CrudRepository接口实战_第3张图片

CrudRepository接口实战_第4张图片

 

你可能感兴趣的:(Spring,Boot)