1.创建的时候导包带上mysql最好加上lombok,然后加上第三方整合包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- Apache Velocity 的依赖-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
2.配置application.properites
server.port=8001
#数据源配置相关
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis-plus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=system
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis配置相关
mybatis-plus.mapper-locations=mapper/*.xml
mybatis-plus.type-aliases-package=com.boot.entity
mybatis-plus.global-config.db-column-underline=true
#日志相关
logging.config=classpath:logback-spring.xml
3.启动类开启Mapper扫描@MapperScan("com.boot.mapper")
4.代码自动生成
//1. 全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(true) // 是否支持AR模式
.setAuthor("pangxiong") // 作者
.setOutputDir("F:\\workspace\\idea\\demo\\springboot-project\\springboot-mybatisplus\\src\\main\\java") // 生成路径 F:\workspace\idea\demo\springboot-project\springboot-mybatisplus
.setFileOverride(true) // 文件覆盖
.setIdType(IdType.AUTO) // 主键策略
.setServiceName("%sService") // 设置生成的service接口的名字的首字母是否为I
// IEmployeeService
.setBaseResultMap(true)
.setBaseColumnList(true);
//2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.setDbType(DbType.MYSQL) // 设置数据库类型
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/mybatis-plus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false")
.setUsername("root")
.setPassword("system");
// 3.策略配置
StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(true) //全局大写命名
.setDbColumnUnderline(true) // 指定表名 字段名是否使用下划线
.setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
.setTablePrefix("")
.setInclude("student"); // 生成的表
// 4.包名策略配置
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("com.boot")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("entity")
.setXml("mapper");
// 5.整合配置
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(config)
.setDataSource(dsConfig)
.setStrategy(stConfig)
.setPackageInfo(pkConfig);
//6. 执行
ag.execute();
5.对entity类做一点修改
@TableName("student")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student extends Model<Student> {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private Long studentId;
private String name;
private Integer age;
private String address;
private String email;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8") // 可以自动格式化查询出来的时间
private Date createTime;
@Override
protected Serializable pkVal() {
return null;
}
}
6.测试
studentService.insert(student);
List<Student> students = studentService.selectList(null);
Student student = studentService.selectById(1l);
user user = studentService.findMinAgeOne();//这个要自己写方法