1、在pom.xml中添加mysql 和 jpa 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2、在application.properties中配置数据库连接信息
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boot_v2?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
spring.datasource.username=root
spring.datasource.password=root
#jpa
#第一次建表设置为create 后面改为update,不然每次重启工程会删除表并新建
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql: true
3、编写实体类:UserEntity
package com.ldy.bootv2.demo.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@Entity
@ApiModel
@Table(name="user")
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})
public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@ApiModelProperty(value="id,新建时不传,修改时传")
private Integer id;
@Column
@ApiModelProperty(value="名称")
private String name;
@Column
@ApiModelProperty(value="年龄")
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
4、编写dao:UserDao
package com.ldy.bootv2.demo.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.ldy.bootv2.demo.entity.UserEntity;
public interface UserDao extends JpaRepository<UserEntity,Integer> {
}
5、编写Controller:UserController
package com.ldy.bootv2.demo.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ldy.bootv2.demo.dao.UserDao;
import com.ldy.bootv2.demo.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@Api(tags = "用户信息管理接口-API")
@RestController
@RequestMapping("/user")
public class UserController {
private static Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
UserDao userDao;
@GetMapping("/findAll")
@ApiOperation("查询全部用户")
public List<UserEntity> findAll() {
logger.info("您调用了findAll接口");
return userDao.findAll();
}
@GetMapping("/getOne/{id}")
@ApiOperation("根据ID查询用户")
public UserEntity getOne(@ApiParam(value="用户id",required=true) @PathVariable("id") Integer id) {
logger.info("您调用了getOne接口");
return userDao.getOne(id);
}
@PutMapping("/saveOrUpdate")
@ApiOperation("新增或者修改用户")
public String saveOrUpdate(@ModelAttribute UserEntity entity) {
logger.info("您调用了saveOrUpdate接口");
try {
userDao.save(entity);
} catch (Exception e) {
logger.error("失败,原因:" + e.getMessage());
return "error";
}
return "success";
}
@DeleteMapping("/delete/{id}")
@ApiOperation("根据ID删除用户")
public String deleteById(@ApiParam(value="用户id",required=true) @PathVariable("id") Integer id) {
logger.info("您调用了delete接口");
try {
userDao.deleteById(id);
} catch (Exception e) {
logger.error("失败,原因:" + e.getMessage());
return "error";
}
return "success";
}
}
6、运行项目,打开swagger页面,测试接口正常,swagger的集成请查看:https://blog.csdn.net/LDY1016/article/details/83415640