- 首先你得有一份待操作的数据库及对应数据表,如数据库:test,数据表: area_code_id,有:id,user_id,box_id三个字段。
- 创建一个 spring boot 项目,名称:connect-db。选择 web、jdbc、mysql三个模块的依赖,点击next。
- 项目创建成功后,去设置配置文件,打开src/main/resources->application.properties,配置如下:
server.port=8076
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
注意:mysql-connector-java 版本在6.0以上,驱动得用 com.mysql.cj.jdbc.Driver,6.0以下还是用:com.mysql.jdbc.Driver。
- 接下来我们分类创建包,项目结构如下图
主要三个框中的,加上入口类。 - 创建 area_code_id 表对应的实体类:AreaCodeId.java
package com.bai.model;
import javax.persistence.*;
@Entity
public class AreaCodeId {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String userId;
// @Column(name = "box_id")
private String boxId;
public AreaCodeId() {
}
public AreaCodeId(Integer id, String userId, String boxId) {
this.id = id;
this</