spring boot 整合 jpa

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

 源码下载地址:https://pan.baidu.com/s/1Z771VDiuabDBJJV445xLeA#list/path=%2Fspring%20boot%202.x%20%E4%BB%A3%E7%A0%81

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值