SpringBoot整合Mybatis Generator自动生成代码

网友投稿 1039 2022-12-16

SpringBoot整合Mybatis Generator自动生成代码

SpringBoot整合Mybatis Generator自动生成代码

目录1.创建SpringBoot项目2. mybatis-generator-maven插件的配置3. 项目结构构建4. APPlication.yml配置5. generatorConfig.xml配置7. 选择 Mybatis Generator 启动,自动在dao、entity、mapper包下生成代码

Mybatis是目前主流的ORM框架,相比于hibernate的全自动,它是半自动化需要手写sql语句、接口、实体对象,后来推出的Generator自动生成代码,可以帮我们提高开发效率。

本文目的:SpringBoot 整合 Mybatis Generator自动生成dao、entity、mapper.xml实现单表增删改查。

1.创建SpringBoot项目

File→New→Project… 选择Spring Initializr,选择JDK版本,默认初始化URL

填写项目名称,java版本,其他描述信息

选择项目存放路径

选择web、mybatis、mysql依赖

Next–>Finish完成项目创建

2. mybatis-generator-maven插件的配置

打开项目的pom.xml文件添加

org.mybatis.generator

mybatis-generator-maven-plugin

true

true

完整pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.xyz

mybatis

0.0.1-SNAPSHOT

jar

mybatis

Spring Boot 整合 Mybatis

org.springframework.boot

spring-boot-starter-parent

2.0.5.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.5

org.springframework.boot

spring-boot-maven-plugin

org.mybatis.generator

mybatis-generator-maven-plugin

true

true

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.xyz

mybatis

0.0.1-SNAPSHOT

jar

mybatis

Spring Boot 整合 Mybatis

org.springframework.boot

spring-boot-starter-parent

2.0.5.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.5

org.springframework.boot

spring-boot-maven-plugin

org.mybatis.generator

mybatis-generator-maven-plugin

true

true

3. 项目结构构建

在项目目录下(这里是mybatis)添加controller、service、dao、entity包,在resources下添加mapper包存放映射文件。

4. application.yml配置

#端口号配置

server:

port: 8088

spring:

#模板引擎配置

thymeleaf:

prefix: classpath:/templates/

suffix: .html

mode: HTML

encoding: UTF-8

cache: false

servlet:

content-type: text/html

#静态文件配置

resources:

static-locations: classpath:/static,classpath:/META-INF/resources,classpath:/templates/

#jdbc配置

datasource:

url: jdbc:mysql://localhost:3306/video?useUnicode=true&characterEncoding=utf8

username: xyz

password: xyz

driver-class-name: com.mysql.jdbc.Driver

#mybatis配置

mybatis:

#映射文件路径

mapper-locations: classpath:mapper/*.xml

#模型所在的保命

type-aliases-package: com.xyz.mybatis.entity

5. generatorConfig.xml配置

在resources文件下创建generatorConfig.xml文件,配置如下:

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

connectionURL="jdbc:mysql://localhost:3306/video?serverTimezone=UTC" userId="xyz"

password="xyz"/>

enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

enableDeleteByPrimaryKey="true">

connectionURL="jdbc:mysql://localhost:3306/video?serverTimezone=UTC" userId="xyz"

password="xyz"/>

enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"

enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"

enableDeleteByPrimaryKey="true">

注意:

classPathEntry location=“E:\IdeaProjects\mysql-connector-java-5.1.47.jar”,建议用5.X系列的,否则可能生成的接口会缺少

6. 在idea中添加一个mybatis generator maven插件启动选项,点击Run,选择Edit Configuration… 点击加号"+"添加,选择maven,填写名称(这里用mybatis generator),命令行:mybatis-generator:generate -e

7. 选择 Mybatis Generator 启动,自动在dao、entity、mapper包下生成代码

注意:

利用Mybatis Generator自动生成代码,对于已经存在的文件会存在覆盖和在原有文件上追加的可能性,不宜多次生成。如需重新生成,需要删除已生成的源文件。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信开发小程序官网(小程序开发 官网)
下一篇:微信信息统计小程序(微信信息统计小程序怎么做)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~