美文网首页
SpringBoot2 - CRUD

SpringBoot2 - CRUD

作者: 深入浅出 | 来源:发表于2018-06-20 10:50 被阅读0次

    构建项目

    访问start.spring.io/

    项目目录结构

    导入开发工具

    配置数据源以及JPA

    1. 新建application.yml配置文件


    配置文件中不要使用TAB键

    spring:
     datasource:
      url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8
      driverClassName: com.mysql.jdbc.Driver
      username: root
      password: 123456
     jpa:
      database: MySQL
      show-sql: true
      hibernate:
       nameing_strategy: org.hibernate.cfg.ImprovedNamingStrategy
    
    1. 创建用户表
    DROP TABLE IF EXISTS `t_user`;
    
    CREATE TABLE `t_user` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `name` varchar(50) DEFAULT NULL COMMENT '名字',
      `age` int DEFAULT '0' COMMENT '年龄',
      `address` varchar(200) DEFAULT NULL COMMENT '地址',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    创建实体

    package com.gala.jpa.entity;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "t_user")
    public class User {
    
        private Long id;
    
        private String name;
    
        private Integer age;
    
        private String address;
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
    }
    

    创建JPA

    package com.gala.jpa.repository.jpa;
    
    import java.io.Serializable;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
    
    import com.gala.jpa.entity.User;
    
    public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User>, Serializable {
    
    }
    

    创建控制器

    package com.gala.jpa.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.gala.jpa.entity.User;
    import com.gala.jpa.repository.jpa.UserDao;
    
    @RestController
    @RequestMapping(value = "/user")
    public class UserController {
    
        @Autowired
        private UserDao userDao;
    
        /**
         * 查询
         */
        @RequestMapping(value = "/list", method = RequestMethod.GET)
        public List<User> list() {
            return userDao.findAll();
        }
    
        /**
         * 新增或修改
         */
        @RequestMapping(value = "/save", method = RequestMethod.GET)
        public User save(User User) {
            return userDao.save(User);
        }
    
        /**
         * 删除
         */
        @RequestMapping(value = "/delete", method = RequestMethod.GET)
        public List<User> delete(Long id) {
            userDao.deleteById(id);
            return userDao.findAll();
        }
    
    }
    

    访问项目

    启动项目

    1. 添加更新用户,请求地址:http://127.0.0.1:8080/user/save?name=admin&age=30&address=beijing

    2. 查询用户,请求地址:http://127.0.0.1:8080/user/list

    1. 删除用户,请求地址:http://127.0.0.1:8080/user/delete?id=1

    相关文章

      网友评论

          本文标题:SpringBoot2 - CRUD

          本文链接:https://www.haomeiwen.com/subject/knytyftx.html