idea2021创建springboot+mybatis+generator

数据库创建

CREATE SCHEMA test DEFAULT CHARACTER SET utf8 ;
CREATE TABLE t1 (
id int NOT NULL AUTO_INCREMENT,
name varchar(45) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

创建项目

打开IntelliJ IDEA 2021
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

安装MyBatis插件

在这里插入图片描述
在这里插入图片描述

装上Properties To Yaml Converter插件并转换application.properties为application.yml

在这里插入图片描述
在这里插入图片描述

用Mybatis Generator生成模型和DAO

修改pom.xml,在<build><;plugins>里加入插件描述:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
</plugin>
在pom.xml鼠标右键,选择Maven-Reload Project,刷新Manven库:
在这里插入图片描述

在src/main/resources新建mybatis generator配置文件generatorConfig.xml,内容:
<?xml version=“1.0” encoding=“UTF-8” ?>
<!DOCTYPE generatorConfiguration PUBLIC “-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN” “http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd” >
<generatorConfiguration>
<!—这里驱动的路径改为自己的,以下targetProject都要改成自己的路径->
<classPathEntry location=“C:/Users/wiseyao/.m2/repository/mysql/mysql-connector-java/8.0.25/mysql-connector-java-8.0.25.jar”/>
<context id=“productRelease”>
<property name=“mergeable” value=“false” />
<plugin type=“org.mybatis.generator.plugins.SerializablePlugin” />
<jdbcConnection driverClass=“com.mysql.jdbc.Driver”
connectionURL=“jdbc:mysql://localhost:3306/test”
userId=“root” password=“123456” />
<javaModelGenerator targetPackage=“com.example.demo.model”
targetProject=“D:/WiseSoft/project/2021/BaJinSi/BaoGuan/03code/demo/src/main/java” />
<sqlMapGenerator targetPackage=“mapper”
targetProject=“D:/WiseSoft/project/2021/BaJinSi/BaoGuan/03code/demo/src/main/resources” />
<javaClientGenerator targetPackage=“com.example.demo.dao”
targetProject=“D:/WiseSoft/project/2021/BaJinSi/BaoGuan/03code/demo/src/main/java” type=“XMLMAPPER” />
<table tableName=“t1” enableCountByExample=“true” enableDeleteByExample=“true” enableDeleteByPrimaryKey=“true” enableInsert=“true” enableSelectByExample=“true” enableSelectByPrimaryKey=“true” enableUpdateByExample=“true” enableUpdateByPrimaryKey=“true”/>
</context>
</generatorConfiguration>

运行Mybatis Generator:
在这里插入图片描述

修改启动类DemoApplication

@SpringBootApplication
@MapperScan(“com.example.demo.dao”)
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

}

加上了:@MapperScan(“com.example.demo.dao”)

测试类

路径:com.example.demo.controller.TestController,内容:
@RestController
public class TestController {

@Autowired
private T1Mapper t1Mapper;

@GetMapping("testDao")
public List<T1> testDao(){
    List<T1> list=t1Mapper.selectByExample(null);
    return list;
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值