连接数据库
选择右侧“database”,输入相关信息并连接数据库
引入依赖
在pom文件中引入MySQL,mybatis,druid依赖
<!--mysql,jdbc驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!--以上MySQL和mybatis相关-->
配置文件
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#或spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis
#对应实体
mybatis.type-aliases-package=com.zhao.seckills.entity
#注册
mybatis.mapper-locations=classpath:com/zhao/seckills/mapper/*.xml
#log
logging.level.com.zhao.seckills=debug
自动执行.sql文件
需要的依赖在spring-boot-autoconfigure里面,可以通过spring-boot-devtools引入
-
把 schema-all.sql 和 data-all.sql文件放到类路径下面,作者使用的test做的,也可以放置main中的类路径:
-
配置yml文件,分析看注释
spring:
sql:
init:
encoding: utf-8
# 设置模式,不要never,不然不起作用
mode: always
platform: mysql
username: root
password: '你的密码'
#这个数据的sql脚本的位置
data-locations: classpath:callmachine-data.sql
# 这个是创建表的sql脚本的位置
schema-locations: classpath:callmachine-schema.sql
# 配置我们的数据源(这一步上面已经做了)
datasource:
url: jdbc:mysql://localhost:3306/res?serverTimezone=UTC
username: root
password: '你的密码'
driver-class-name: com.mysql.cj.jdbc.Driver
通过Easy Code生成代码
- 安装easy code插件
file→settings→plugins→在marketPlace中搜索easy code并安装 - 生成代码
在右侧的database处,选中表,右击
选择需要生成的文件,并开始生成文件