您当前的位置:中国车市网要闻正文

SpringBoot整合Mybatis实现自动生成代码

放大字体  缩小字体 2019-10-08 16:00:07  阅读:6759 来源:自媒体 作者:奔驰GLC级

MyBatis 是一款优异的耐久层结构,它支撑定制化 SQL、存储进程以及高档映射。MyBatis 避免了简直一切的 JDBC 代码和手动设置参数...

MyBatis 是一款优异的耐久层结构,它支撑定制化 SQL、存储进程以及高档映射。MyBatis 避免了简直一切的 JDBC 代码和手动设置参数以及获取成果集。MyBatis 能够运用简略的 XML 或注解来装备和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,一般旧式 Java 目标)为数据库中的记载。

MyBatis官网:https://mybatis.org/mybatis-3/zh/index.html

信任咱们对MyBatis都不生疏,在实践开发运用中,运用的比较多,它常常与SpringBoot结构,Spring结构 进行整合,从而开发相关的网站以及服务。

MyBatis有一个强壮且有用的功用,便是在通过装备之后,能够协助咱们一键生成代码,削减相关的开发工作量。

下面就运用MyBatis的代码生成器东西(mybatis-generator),为咱们生成一套代码,生成的代码完结了CRUD功用,咱们就能够直接运用它。具体操作过程如下:

Mybatis-generator文档:http://mybatis.org/generator/index.html

1. 创立SpringBoot工程项目,并创立dao,domain,mapper目录,如下:

2. 创立数据库相关的表,这儿省掉SQL句子

2. 增加pom.xml装备依靠,并修正pom.xml文件

修正pom.xml文件的build结构,完结mybatis-generator的装备,替换为如下装备

3. 装备特点application.properties文件,如下:

4. 编写mybatis-generator.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

!--驱动包的途径--

!--classPathEntry location=C:Userslhf.m2repositorymysqlmysql-connector-java5.1.46mysql-connector-java-5.1.46.jar /--

!--数据库衔接--

context id=DB2Tables targetRuntime=MyBatis3

!--注释--

commentGenerator

property name=suppressAllComments value=true/

property name=suppressDate value=true/

/commentGenerator

!--数据库衔接地址及账号密码--

jdbcConnection driverClass=com.mysql.jdbc.Driver

connectionURL=jdbc:mysql://127.0.0.1:3306/seckill

userId=root

password=root

/jdbcConnection

javaTypeResolver

property name=forceBigDecimals value=false /

/javaTypeResolver

!--生成entity类寄存方位--

javaModelGenerator targetPackage=com.lhf.springboot.domain targetProject=src/main/java

!--是否对model增加结构函数--

property name=constructorbased value=false/

!--是否答应子包--

property name=enableSubPackages value=true/

!--树立的model目标是否不可变,也便是生成的model没有setter办法--

property name=immutable value=false/

property name=trimStrings value=true/

/javaModelGenerator

!--生成映射文件寄存方位--

sqlMapGenerator targetPackage=mapper targetProject=src/main/resources

property name=enableSubPackages value=true /

/sqlMapGenerator

!--生成Dao类的寄存方位--

!-- 客户端代码,生成易于运用的正对Model目标和XML装备文件的代码

type=ANNOTATEDMAPPER, 生成Java Model和依据注解的Mapper目标

type=MIXEDMAPPER, 生成依据注解的Java Model和相应的Mapper目标

type=XMLMAPPER, 生成SQLMap XML文件和独立的Mapper接口

--

javaClientGenerator type=XMLMAPPER targetPackage=com.lhf.springboot.dao targetProject=src/main/java

property name=enableSubPackages value=true /

/javaClientGenerator

!--请依据自己数据库对应的表名进行装备修正,这儿仅供参考--

!--生成对应表及类名--

table schema=mybatis tableName=user_info domainObjectName=User

enableInsert=true enableSelectByExample=false

enableDeleteByPrimaryKey=false enableDeleteByExample=false

enableCountByExample=false enableUpdateByExample=false

enableSelectByPrimaryKey=true enableUpdateByPrimaryKey=true/

table tableName=item domainObjectName=Item enableCountByExample=false

enableUpdateByExample=false enableDeleteByExample=false

enableSelectByExample=false selectByExampleQueryId=false

enableInsert=true enableDeleteByPrimaryKey=false /table

table tableName=item_stock domainObjectName=ItemStock enableCountByExample=false

enableUpdateByExample=false enableDeleteByExample=false

enableSelectByExample=false selectByExampleQueryId=false

enableInsert=true enableDeleteByPrimaryKey=false /table

table tableName=order_info domainObjectName=OrderInfo enableCountByExample=false

enableUpdateByExample=false enableDeleteByExample=false

enableSelectByExample=false selectByExampleQueryId=false

enableInsert=true enableDeleteByPrimaryKey=false /table

/context

/generatorConfiguration

5. 装备履行mybatis-generator文件,装备操作过程:

Run - Edit Configurations - + - Maven - 装备,如图

6. 履行生成代码,Run - Run 'mybatis-generator', 点击履行即可生成代码,如图

7. 看到日志成功了,到此就完结了代码的主动生成功用了!生成的代码如图:

是不是很简略呢?关于SpringBoot与MyBatis整合相关的知识点,请自行研讨。