Mybatis-Generator自动生成代码——Mybatis 深入浅出(二)

使用Mybatis-Generator ,可以自动生成Model、Dao、Mapping相关文件

根据已创建的数据库生成相映的文件 ,每一个数据表可以自动生成一个对应的model(pojo类),调用Dao类中的方法就可以直接对数据库进行增删改查的操作。

1、使用方法:

1.1、需要用到的文件

  1. 需要Mybatis-Generator jar包 ,关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
  2. 连接数据库的驱动jar包,由于我使用的是Mysql数据库,准备一个连接mysql数据库的驱动jar包

这里写图片描述

上面需要的这两个包,也可以通过maven依赖的方式去自动下载

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--generatorConfig.xml文件的相对于项目根目录的路径-->
                    <configurationFile>generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

1.2、generatorConfig.xml

自动生成代码,主要就是写这个配置文件,指定连接数据库的jar包位置,指定数据库的一些基本内容,指定生成文件的路径等

<?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="mysql-connector-java-5.0.8-bin.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="lcw.model" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成Mapping映射文件存放位置-->
        <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

1.3、生成Model、Dao、Mapping相关文件

如果在1.1中在maven中配置自动下载jar包,则运行package阶段就可以,自动生成。否则需要手动去生成相应文件

生成语句文件:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite


这里写图片描述

2、Mybatis Generator配置详解

Mybatis Generator最完整配置详解

3、常见的错误

错误一:

报错:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [spring-mybatis.xml]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': : Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource';

原因:因为mybatis generator 是根据已创建的数据库数据表生成相映的,出现上面这个错误一般是没有配置数据源,或者登陆名或密码错误

解决:

在yml配置文件中,添加下面配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 密码
    url: jdbc:mysql://数据库地址:端口/数据库?characterEncoding=UTF-8&useSSL=false

参考:

使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件

Spring boot 多数据源
数据库逆向框架代码生成工具:MyBatis Generator的使用

关注我的公众号,轻松了解和学习更多技术
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值