从0开始码第一个Spring Boot项目之IDEA中通过Maven方式部署mybatis generator逆向工程

看过之前的文章,我们都可以看到我们项目中使用的是mybatis中的注解方式,然后在注解中编写语句,但是这种方式只能处理简单的CRUD,如果是复杂的多表联合查询和存储过程,mybatis官方文档建议使用XML方式

MBG seeks to make a major impact on the large percentage of database operations that are simple CRUD (Create, Retrieve, Update, Delete). 
You will still need to hand code SQL and objects for join queries, or stored procedures.

那么如何在IDEA中使用Mybatis逆向工程

1.在pom文件中添加Maven依赖插件
<plugin>
	<!-- mybatis generator插件 -->
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-maven-plugin</artifactId>
   <version>1.4.0</version>
   <!-- 添加需要逆向的数据库驱动 -->
   <dependencies>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.25</version>
       </dependency>
   </dependencies>
</plugin>
2.编写generatorConfig.xml文件放在resources目录下
<?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>

    <context id="Mysql" targetRuntime="MyBatis3">

        <!-- mybatis提供的分页插件 -->
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
        <!-- 去掉生成文件中的注释 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/community"
                        userId="root"
                        password="admin">
        </jdbcConnection>
        <!-- 支持java类型和数据库类型的转换 -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 根据数据库表自动生成的实体类(model)存放的路径 -->
        <javaModelGenerator targetPackage="wang.kingweb.community.model" targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 自动生成的XXXMapper.xml文件存放路径 -->
        <sqlMapGenerator targetPackage="mapper"  targetProject="src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 对应XXXMapper.xml文件的XXXMapper.java接口文件 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="wang.kingweb.community.mapper"  targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        
        <!-- 对应数据库表名以及生成的实体类名 -->
        <table tableName="user" domainObjectName="User" ></table>
        <table tableName="article" domainObjectName="Article" ></table>

    </context>
</generatorConfiguration>
3.生成命令

在IDEA左下角点击IDEA集成的命令行插件Terminal,如下:
命令行工具
然后输入如下命令:

mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

我们配置的项目路径下就会出现对应的实体类、XXMapper.java以及XXMapper.xml文件

运行所需配置:
在XXXApplication.java文件中添加@MapperScan(“xxx.xxx.mapper”)
这个注解是用来扫描XXXMapper.java文件所在路径,如下:

@SpringBootApplication
@MapperScan("wang.kingweb.community.mapper")
public class CommunityApplication {

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

}

然后在application.properties中做如下配置:

#配置对应别名的实体类路径
mybatis.type-aliases-package=wang.kingweb.community.model
#配置XXXMapper.xml文件路径
mybatis.mapper-locations=classpath:/mapper/*.xml

配置完成后,启动项目,查看项目是否运行成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值