# SpringBoot + JPA 操作数据库
# 导入依赖:
SpringBoot 版本:2.2.6.RELEASE
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jpa</artifactId> | |
</dependency> |
# 测试:
创建数据库
/* | |
Navicat Premium Data Transfer | |
Source Server : windows | |
Source Server Type : MySQL | |
Source Server Version : 80028 | |
Source Host : localhost:3306 | |
Source Schema : ihrm | |
Target Server Type : MySQL | |
Target Server Version : 80028 | |
File Encoding : 65001 | |
Date: 10/05/2024 09:25:10 | |
*/ | |
SET NAMES utf8mb4; | |
SET FOREIGN_KEY_CHECKS = 0; | |
-- ---------------------------- | |
-- Table structure for co_company | |
-- ---------------------------- | |
DROP TABLE IF EXISTS `co_company`; | |
CREATE TABLE `co_company` ( | |
`id` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'ID', | |
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '公司名称', | |
`manager_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '企业登录账号ID', | |
`version` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '当前版本', | |
`renewal_date` datetime NULL DEFAULT NULL COMMENT '续期时间', | |
`expiration_date` datetime NULL DEFAULT NULL COMMENT '到期时间', | |
`company_area` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司地区', | |
`company_address` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '公司地址', | |
`business_license_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '营业执照-图片ID', | |
`legal_representative` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '法人代表', | |
`company_phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司电话', | |
`mailbox` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮箱', | |
`company_size` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司规模', | |
`industry` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '所属行业', | |
`remarks` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '备注', | |
`audit_state` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '审核状态', | |
`state` tinyint NOT NULL DEFAULT 1 COMMENT '状态', | |
`balance` double NOT NULL COMMENT '当前余额', | |
`create_time` datetime NOT NULL COMMENT '创建时间' | |
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '企业表' ROW_FORMAT = Dynamic; | |
-- ---------------------------- | |
-- Records of co_company | |
-- ---------------------------- | |
INSERT INTO `co_company` VALUES ('1', '123', '1', '1', '2024-05-07 08:49:04', '2024-05-25 08:49:16', '123123', '1231', '12', '7182', '32312', '23', '12', '213', '123', '123', 1, 323, '2024-05-15 08:48:51'); | |
SET FOREIGN_KEY_CHECKS = 1; |
创建实体类
@Data | |
@NoArgsConstructor | |
@Entity // 声明这个表是个实体类 | |
@Table(name = "co_company") | |
public class Company implements Serializable | |
{ | |
private static final long serialVersionUID = 594829320797158219L; | |
//ID @Id 告诉程序 哪个字段是 主键,如果不指定注解会提示错误信息 | |
@Id | |
private String id; | |
/** | |
* 公司名称 | |
*/ | |
private String name; | |
/** | |
* 企业登录账号 ID | |
*/ | |
private String managerId; | |
/** | |
* 当前版本 | |
*/ | |
private String version; | |
/** | |
* 续期时间 | |
*/ | |
private Date renewalDate; | |
/** | |
* 到期时间 | |
*/ | |
private Date expirationDate; | |
/** | |
* 公司地区 | |
*/ | |
private String companyArea; | |
/** | |
* 公司地址 | |
*/ | |
private String companyAddress; | |
/** | |
* 营业执照 - 图片 ID | |
*/ | |
private String businessLicenseId; | |
/** | |
* 法人代表 | |
*/ | |
private String legalRepresentative; | |
/** | |
* 公司电话 | |
*/ | |
private String companyPhone; | |
/** | |
* 邮箱 | |
*/ | |
private String mailbox; | |
/** | |
* 公司规模 | |
*/ | |
private String companySize; | |
/** | |
* 所属行业 | |
*/ | |
private String industry; | |
/** | |
* 备注 | |
*/ | |
private String remarks; | |
/** | |
* 审核状态 | |
*/ | |
private String auditState; | |
/** | |
* 状态 | |
*/ | |
private Integer state; | |
/** | |
* 当前余额 | |
*/ | |
private Double balance; | |
/** | |
* * 创建时间 | |
*/ | |
private Date createTime; | |
} |
SpringBoot 启动类配置
@SpringBootApplication(scanBasePackages = {"com.ihrm"}) | |
//jpa 包扫描注解配置 (如果是 mybatis 则不需要扫描实体类) | |
@EntityScan("com.ihrm.domain.company") // 这里需要注意,如果是不同模块那么当前模块是否引入了实体类模块的 pom 模块,这里 ctrl 点击是进不去的但可以用,重点是你要思考 poom 的依赖问题 | |
public class CompanyApplication | |
{ | |
public static void main(String[] args) | |
{ | |
SpringApplication.run(CompanyApplication.class, args); | |
} | |
} |
@EntityScan 注解:
@EntityScan
注解用于告诉 Spring Boot 在哪里扫描 JPA 实体类。- 当你的 JPA 实体类不在主启动类所在的包及其子包下时,你需要显式地使用
@EntityScan
注解来指定实体类的扫描路径。 - 该注解的参数是一个字符串数组,可以指定一个或多个实体类的扫描路径。
创建 dao
@Repository// 这里也要考虑模块问题,是否需要引入实体类模块才能找到该实体类 | |
public interface CompanyDao extends JpaRepository<Company, String>, JpaSpecificationExecutor<Company> | |
{ | |
} |
配置文件
application.yml
# 服务配置 | |
server: | |
port: 9011 | |
# spring 配置 | |
spring: | |
application: | |
name: ihrm-company #指定服务名 | |
datasource: | |
driver-class-name: com.mysql.cj.jdbc.Driver | |
url: jdbc:mysql://localhost:3306/ihrm?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC | |
username: root | |
password: dkx.. | |
jpa: | |
database: MySQL | |
show-sql: true | |
open-in-view: true |
# 基本的增删改查操作:
dao
package com.ihrm.company.dao; | |
import com.ihrm.domain.company.Company; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | |
import org.springframework.stereotype.Repository; | |
@Repository | |
public interface CompanyDao extends JpaRepository<Company, String>, JpaSpecificationExecutor<Company> | |
{ | |
} |
service
package com.ihrm.company.service; | |
import com.ihrm.domain.company.Company; | |
import java.util.List; | |
public interface CompanyService | |
{ | |
void add(Company company); | |
void update(Company company); | |
void deleteById(String id); | |
Company findById(String id); | |
List<Company> findAll(); | |
} |
serviceimpl
package com.ihrm.company.service.impl; | |
import com.ihrm.common.util.IdWorker; | |
import com.ihrm.company.dao.CompanyDao; | |
import com.ihrm.company.service.CompanyService; | |
import com.ihrm.domain.company.Company; | |
import lombok.AllArgsConstructor; | |
import org.springframework.stereotype.Service; | |
import java.util.List; | |
@Service(value = "companyService") | |
@AllArgsConstructor | |
public class CompanyServiceImpl implements CompanyService | |
{ | |
private CompanyDao companyDao; | |
private IdWorker idWorker; | |
/** | |
* 保存企业 | |
* @param company | |
*/ | |
public void add(Company company) | |
{ | |
String id = idWorker.nextId() + ""; | |
company.setId(id); | |
company.setAuditState("0"); | |
company.setState(1); | |
companyDao.save(company); | |
} | |
/** | |
* 更新企业 | |
* @param company | |
* 根据 id 查询企业对象 | |
* 修改对象属性 | |
* 调用 dao 完成更新 | |
*/ | |
public void update(Company company) | |
{ | |
Company temp = companyDao.findById(company.getId()).get(); | |
temp.setName(company.getName()); | |
temp.setCompanyPhone(company.getManagerId()); | |
companyDao.save(temp); | |
} | |
/** | |
* 删除企业 | |
* @param id | |
*/ | |
public void deleteById(String id) | |
{ | |
companyDao.deleteById(id); | |
} | |
/** | |
* 根据 id 查询企业 | |
* @param id | |
* @return | |
*/ | |
public Company findById(String id) | |
{ | |
return companyDao.findById(id).get(); | |
} | |
/** | |
* 查询全部企业 | |
* @return | |
*/ | |
public List<Company> findAll() | |
{ | |
return companyDao.findAll(); | |
} | |
} |
controller
package com.ihrm.company.controller; | |
import com.ihrm.common.entity.Result; | |
import com.ihrm.common.entity.ResultCode; | |
import com.ihrm.company.service.CompanyService; | |
import com.ihrm.domain.company.Company; | |
import lombok.AllArgsConstructor; | |
import org.springframework.web.bind.annotation.*; | |
@RestController | |
@AllArgsConstructor | |
public class CompanyController | |
{ | |
private CompanyService companyService; | |
// 查询全部企业 | |
@GetMapping("/getAll") | |
public Result getCompany() | |
{ | |
return new Result(ResultCode.SUCCESS, companyService.findAll()); | |
} | |
// 保存企业 | |
@PostMapping("/add") | |
public Result save(@RequestBody Company company) | |
{ | |
companyService.add(company); | |
return new Result(ResultCode.SUCCESS); | |
} | |
// 根据 id 更新企业 | |
@PutMapping("/put") | |
public Result put(Company company) | |
{ | |
companyService.update(company); | |
return new Result(ResultCode.SUCCESS); | |
} | |
// 根据 id 删除企业 | |
@DeleteMapping("/del") | |
public Result del(String id) | |
{ | |
companyService.deleteById(id); | |
return new Result(ResultCode.SUCCESS); | |
} | |
// 根据 id 查询企业 | |
@GetMapping("/byId") | |
public Result byId(String id) | |
{ | |
return new Result(ResultCode.SUCCESS, companyService.findById(id)); | |
} | |
} |