SpringBoot整合MybatisPlus

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();//这个要自己写方法

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值