当前位置:   article > 正文

springboot通过Jpa操作mysql数据库_spring jpa操作mysql时的本地数据库的地址名称

spring jpa操作mysql时的本地数据库的地址名称
新建工程

采用idea的spring boot创建向导创建web工程demo

pom依赖
<dependency>
   <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--自动生成getter,setter-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
application.yml配置
# 数据库url地址
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    dbcp2:
      max-idle: 10
      max-wait-millis: 10000
      min-idle: 5
      initial-size: 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
数据库
CREATE TABLE `employee` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `sex` char(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

实体

@Data
@Entity
@Table(name = "employee")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private char sex;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
dao接口
public interface EmployeeDao extends JpaRepository<Employee, Serializable> {
}
  • 1
  • 2
service层

接口

public interface EmployeeSerivce {
    Employee getEmployee(Long id);
    List<Employee> list();
    int update(Employee employee);
    int insert(Employee employee);
    int delete(Long id);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

实现类

@Service
public class EmployeeServiceImpl implements EmployeeSerivce {

    @Autowired
    EmployeeDao dao;

    @Override
    public Employee getEmployee(Long id) {
        return dao.getOne(id);
    }

    @Override
    public List<Employee> list() {
        return dao.findAll();
    }

    @Override
    public int update(Employee employee) {
        dao.save(employee);
        return 1;
    }

    @Override
    public int insert(Employee employee) {
        dao.save(employee);
        return 1;
    }

    @Override
    public int delete(Long id) {
        dao.deleteById(id);
        return 1;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
访问controller
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeSerivce employeeSerivce;

    @GetMapping(value = "/emp/{id}")
    public Employee getEmp(@PathVariable(value = "id") Long id){
        return employeeSerivce.getEmployee(id);
    }

    @GetMapping(value = "/emp/list")
    public List<Employee> getEmpList(){
        return employeeSerivce.list();
    }

    @PostMapping(value = "/emp")
    public String insert(Employee employee){
        employeeSerivce.insert(employee);
        return "success";
    }

    @PutMapping(value = "/emp")
    public String update(Employee employee){
        employeeSerivce.update(employee);
        return "success";
    }

    @DeleteMapping(value = "/delete/{id}")
    public String delete(@PathVariable(value = "id") Long id){
        employeeSerivce.delete(id);
        return "success";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
结果

访问http://localhost:8080/emp/list


[{"id":1,"name":"admin","sex":"1"},{"id":2,"name":"guese","sex":"1"},{"id":3,"name":"whc","sex":"1"}]
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/954918
推荐阅读
相关标签
  

闽ICP备14008679号