MBG的简单使用


本博客介绍了MBG的整个使用过程,以及xml文件的使用技巧

Mybatis

在写好pojo之后,编写dao类,此时有两种方式使用mybatis来测试

方式一:注解@Mapper+sql语句注解 当然@Mapper可以不写,但是要在主程序里面协商@MapperScan(从java包下面开始写)

方式二:xml方式,xml文件放在一般放在resources下的mapper文件夹下,此时需要在yml文件夹里面配置mapper路径:

mabatis:
  mapper-locations: classpath:mapper/*xml
  configuration:
    map-underscore-to-camel-case: true #驼峰式命名

xml格式类似这种:可以在mybatis官网找到模板

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imoke.mall.dao.CategoryMapper">
    <select id="queryById" resultType="com.imoke.mall.pojo.Category">
        select * from mall_category where id = #{id}
    </select>
</mapper>
  • namespace中填写对应的mapper的完整包名
  • id中填写方法名
  • resultType中填写返回值类型
  • 完成sql语句,别用select *,把字段都打出来,防止之后表新加了字段,程序没变但是查出了新的字段,另外也使sql语句具有可读性。把所有字段定义在外面,再在sql语句中通过refid引入。

改进

<mapper namespace="com.imooc.mall.dao.CategoryMapper">
    <sql id="Base_Column_List">
        id, parent_id, name, status, sort_order, create_time, update_time
    </sql>
 
    <select id="queryById" resultType="com.imooc.mall.pojo.Category">
        select
        <include refid="Base_Column_List"/>
        from mall_category
        where id = #{id}
    </select>
</mapper>

Mybatis-generator

Mybatis-generator可以帮助开发人员快速实现从表到java对象的生成工作,让开发人员解放出来更专注于业务逻辑的开发。

  1. 在pom.xml中添加插件,并配置对表重复生成时会覆盖
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
 
    <configuration>
        <overwrite>true</overwrite>
    </configuration>
</plugin>
  1. 在resources目录下新建generatorConfig.xml,从官网http://mybatis.org/generator/configreference/xmlconfig.html中粘贴示例,再根据自身项目修改

  2. generatorConfig.xml配置详解:http://www.imooc.com/article/21444

    1. 配置连接数据库所用的驱动,这里选用8.0.23版本
    <classPathEntry location="D:\Users\RD\Download\mysql-connector-java-8.0.23.jar" />
    1. 配置不再追加xml内容和不添加注释
    <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
     
    <commentGenerator>
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    1. 配置数据库的相关设置
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                    connectionURL="jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf-8&amp;userSSL=false&amp;serverTimezone=Asia/Shanghai"
                    userId="root"
                    password="wu980416">
    </jdbcConnection>
     4. 配置pojo即实体类的生成位置
    
    <javaModelGenerator targetPackage="com.imoke.mall.pojo" targetProject="src/main/java">
        <property name="enableSubPackages" value="true" />
        <property name="trimStrings" value="true" />
    </javaModelGenerator>
    
     5. 配置mapper,即Mapper.xml的位置
    
    <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
                <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    1. 配置dao,即接口位置
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.imoke.mall.dao"  targetProject="src/main/java">
           <property name="enableSubPackages" value="true" />
       </javaClientGenerator>
    1. 配置要生成的表并确定生成的前缀,取消一些example方法
    <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
  3. 开始生成,在终端中输入

mvn mybatis-generator:generate

补充

有时候数据库的字段类型对不上,可以使用columnOverride标签对其进行配置,例如

<table tableName="mall_product" domainObjectName="Product" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">
           <columnOverride column="detail" jdbcType="VARCHAR"/>
           <columnOverride column="sub_images" jdbcType="VARCHAR"/>
 </table>

项目中整个MBG文件

<?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="D:\Users\RD\Download\mysql-connector-java-8.0.23.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf-8&amp;userSSL=false&amp;serverTimezone=Asia/Shanghai"
                        userId="root"
                        password="wu980416">
        </jdbcConnection>


        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.imoke.mall.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.imoke.mall.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

<!--        <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
<!--        <table tableName="mall_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
<!--        <table tableName="mall_category" domainObjectName="Category" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
        <!--<table tableName="mall_product" domainObjectName="Product" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">
            <columnOverride column="detail" jdbcType="VARCHAR"/>
            <columnOverride column="sub_images" jdbcType="VARCHAR"/>
        </table>-->
<!--        <table tableName="mall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
        <table tableName="mall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>

文章作者: RD
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 RD !
评论
评论
  目录