数据库表medicine_info存放的是某机构的药品信息:
CREATE TABLE `medicine_info` (
`medicine_no` int(11) NOT NULL AUTO_INCREMENT COMMENT '药品编码',
`brand` varchar(50) NOT NULL COMMENT '品牌',
`medicine_generic_name` varchar(50) NOT NULL COMMENT '药品通用名称',
`medicine_name` varchar(50) NOT NULL COMMENT '药品名称',
`specification` varchar(50) NOT NULL COMMENT '规格',
`category` varchar(50) NOT NULL COMMENT '类别',
`usage` varchar(50) NOT NULL COMMENT '用法',
`dosage` varchar(50) NOT NULL COMMENT '使用剂量',
`users` varchar(50) NOT NULL COMMENT '适用人群',
`specific` varchar(1) NOT NULL COMMENT '是否特效药(0:是,1:不是)',
`date` datetime NOT NULL COMMENT '更新日期',
PRIMARY KEY (`medicine_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
表中现有一条数据如下:
创建一个新的SpringBoot项目,然后实现向药品信息表medicine_info中添加记录的功能。
1、实体类MedicineInfo.java
package com.cn.demo.dbkeywords.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "medicine_info")
public class MedicineInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "medicine_no")
private Integer medicineNo;
private String brand;
@Column(name = "medicine_generic_name")
private String medicineGenericName;
@Column(name = "medicine_name")
private String medicineName;
private String specification;
private String category;
private String usage;
private String dosage;
private String users;
private String specific;
private Date date;
public Integer getMedicineNo() {
return medicineNo;
}
public void setMedicineNo(Integer medicineNo) {
this.medicineNo = medicineNo;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getMedicineGenericName() {
return medicineGenericName;
}
public void setMedicineGenericName(String medicineGenericName) {
this.medicineGenericName = medicineGenericName;
}
public String getMedicineName() {
return medicineName;
}
public void setMedicineName(String medicineName) {
this.medicineName = medicineName;
}
public String getSpecification() {
return specification;
}
public void setSpecification(String specification) {
this.specification = specification;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUsage() {
return usage;
}
public void setUsage(String usage) {
this.usage = usage;
}
public String getDosage() {
return dosage;
}
public void setDosage(String dosage) {
this.dosage = dosage;
}
public String getUsers() {
return users;
}
public void setUsers(String users) {
this.users = users;
}
public String getSpecific() {
return specific;
}
public void setSpecific(String specific) {
this.specific = specific;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
2、接口处MedicineInfoRepository.java
package com.cn.demo.dbkeywords.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.cn.demo.dbkeywords.entity.MedicineInfo;
public interface MedicineInfoRepository extends JpaRepository{
}
3、服务层MedicineInfoService.java
package com.cn.demo.dbkeywords.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cn.demo.dbkeywords.entity.MedicineInfo;
import com.cn.demo.dbkeywords.repository.MedicineInfoRepository;
@Service
public class MedicineInfoService {
@Autowired
private MedicineInfoRepository medicineInfoRepository;
public void addMedicine(MedicineInfo medicineInfo) {
medicineInfoRepository.save(medicineInfo);
}
}
4、控制层MedicineInfoController.java
package com.cn.demo.dbkeywords.controller;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.cn.demo.dbkeywords.entity.MedicineInfo;
import com.cn.demo.dbkeywords.service.MedicineInfoService;
@RestController
@RequestMapping("/api")
public class MedicineInfoController {
@Autowired
private MedicineInfoService medicineInfoService;
@RequestMapping(value = "/addMedicine", method = RequestMethod.POST)
public void addMedicine(@RequestBody MedicineInfo medicineInfo) {
medicineInfo.setDate(new Date());
medicineInfoService.addMedicine(medicineInfo);
}
}
运行程序,使用postman测试:
响应结果如下:
控制台错误信息如下:
Hibernate: select medicinein0_.medicine_no as medicine1_2_0_, medicinein0_.brand as brand2_2_0_, medicinein0_.category as category3_2_0_, medicinein0_.date as date4_2_0_, medicinein0_.dosage as dosage5_2_0_, medicinein0_.medicine_generic_name as medicine6_2_0_, medicinein0_.medicine_name as medicine7_2_0_, medicinein0_.specific as specific8_2_0_, medicinein0_.specification as specific9_2_0_, medicinein0_.usage as usage10_2_0_, medicinein0_.users as users11_2_0_ from medicine_info medicinein0_ where medicinein0_.medicine_no=?
Hibernate: insert into medicine_info (brand, category, date, dosage, medicine_generic_name, medicine_name, specific, specification, usage, users) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2019-10-25 16:35:00.498 WARN 40996 --- [nio-9000-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2019-10-25 16:35:00.499 ERROR 40996 --- [nio-9000-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'specific, specification, usage, users) values ('京都念慈菴', '中药', '201' at line 1
2019-10-25 16:35:00.506 ERROR 40996 --- [nio-9000-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/test] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'specific, specification, usage, users) values ('京都念慈菴', '中药', '201' at line 1
可以看到,提示在specific的附近。
specific是MySQL数据库的关键字,不能使用关键字作为数据库中的字段名。将其修改如下:
同时修改实体类MedicineInfo.java中的specific如下:
重新生成get和set方法:
重启程序,再次测试,控制台报错如下:
2019-10-25 17:02:21.923 INFO 50572 --- [nio-9000-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/test] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-10-25 17:02:21.923 INFO 50572 --- [nio-9000-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-10-25 17:02:21.930 INFO 50572 --- [nio-9000-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms
Hibernate: select medicinein0_.medicine_no as medicine1_2_0_, medicinein0_.brand as brand2_2_0_, medicinein0_.category as category3_2_0_, medicinein0_.date as date4_2_0_, medicinein0_.dosage as dosage5_2_0_, medicinein0_.is_specific as is_speci6_2_0_, medicinein0_.medicine_generic_name as medicine7_2_0_, medicinein0_.medicine_name as medicine8_2_0_, medicinein0_.specification as specific9_2_0_, medicinein0_.usage as usage10_2_0_, medicinein0_.users as users11_2_0_ from medicine_info medicinein0_ where medicinein0_.medicine_no=?
Hibernate: insert into medicine_info (brand, category, date, dosage, is_specific, medicine_generic_name, medicine_name, specification, usage, users) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2019-10-25 17:02:22.244 WARN 50572 --- [nio-9000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2019-10-25 17:02:22.244 ERROR 50572 --- [nio-9000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'usage, users) values ('京都念慈菴', '中药', '2019-10-25 17:02:22.002', '' at line 1
2019-10-25 17:02:22.259 ERROR 50572 --- [nio-9000-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/test] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'usage, users) values ('京都念慈菴', '中药', '2019-10-25 17:02:22.002', '' at line 1
提示usage也是数据库关键字,按上面的方式修改数据库,实体类修改如下:
重启再次测试
数据库中多了一条记录:
总结:specific和usage都是MySQL数据库的关键字,date不是。在设计数据库表时要避免数据库的关键字!!!