码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis逆向工程

时间:2020-04-08 11:45:53      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:日志   ssi   generated   com   package   exist   内容   and   目录   

Mybatis简述一下其优缺点

mybatis是一种持久层框架,也属于ORM映射。前身是ibatis。
相比于hibernatehibernate为全自动化,配置文件书写之后不需要书写sql语句,但是欠缺灵活,很多时候需要优化;
mybatis为半自动化,需要自己书写sql语句,需要自己定义映射。增加了程序员的一些操作,但是带来了设计上的灵活,并且也是支持hibernate的一些特性,如延迟加载,缓存和映射等;对数据库的兼容性比hibernate差。移植性不好,但是可编写灵活和高性能的sql语句。
1.sql语句与代码分离,存放于xml配置文件中:
优点:便于维护管理,不用在java代码中找这些语句;

缺点: JDBC方式可以用用打断点的方式调试,但是Mybatis不能,需要通过log4j日志输出日志信息帮助调试,然后在配置文件中修改。

2.用逻辑标签控制动态SQL的拼接:
优点:用标签代替编写逻辑代码;

缺点:拼接复杂SQL语句时,没有代码灵活,拼写比较复杂。不要使用变通的手段来应对这种复杂的语句。

3.查询的结果集与java对象自动映射:
优点:保证名称相同,配置好映射关系即可自动映射或者,不配置映射关系,通过配置列名=字段名也可完成自动映射。

缺点:对开发人员所写的SQL依赖很强。

4.编写原声SQL:
优点:接近JDBC,比较灵活。

缺点:对SQL语句依赖程度很高;并且属于半自动,数据库移植比较麻烦,比如mysql数据库编程Oracle数据库,部分的sql语句需要调整。

————————————————————————————————————————————————————————

至此结束,介绍就到这里了,小编今日给大家介绍如何使用 Mybatis的逆向工程 一键生成 由数据库表——>实体,mapper.xml,mapper.java 一站式生成策略

从 pom 文件开始

添加 mybatis-generator-maven-plugin 依赖插件 以及相关配置文件插件 resource (用于扫描配置文件)

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

 


resource插件 也在 <build> 下

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

 


在resource 目录下(也可以自定义目录,配置相关路径需要更改)创建 generatorConfig.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>

<!--导入属性配置 在同级的目录下创建 generator.properties-->
<properties resource="generator.properties"></properties>

<!--指定特定数据库的jdbc驱动jar包的位置(绝对路径)-->
<classPathEntry location="${jdbc.driverLocation}"/>

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

<!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<!--是否去掉自动生成的注释 true:是-->
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!--jdbc的数据库连接:驱动类、链接地址、用户名、密码-->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>

<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="com.technologies.bear.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>


<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="mappers"
targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>

<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.technologies.bear.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!-- 数据表进行生成操作 tableName:表名; domainObjectName:对应的DO -->
<table tableName="bear_user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="bear_role" domainObjectName="Role"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>

 

配置文件中 有一个 properties 标签标注配置文件 generator.properties 在 generatorConfig.xml同级目录下创建

jdbc.driverLocation=D:\\Downloads\\mysql-connector-java-8.0.11\\mysql-connector-java-8.0.11\\mysql-connector-java-8.0.11.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://****:3306/db?useUnicode=true&characterEncoding=utf-8
jdbc.userId=***
jdbc.password=***

 


在 generator.properties 配置文件中有段 jdbc.driverLocation=url.jar 的配置,这个jar包自行下载,把电脑里的路径拷贝过来就可以

双击运行插件 :

 技术图片

运行日志:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.technologies:bear >------------------------
[INFO] Building bear 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ bear ---
[INFO] Connecting to the Database
Wed Apr 08 10:46:38 CST 2020 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[INFO] Introspecting table bear_user
log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Introspecting table bear_role
[INFO] Introspecting table bear_permission
[INFO] Introspecting table bear_user_role
[INFO] Introspecting table bear_role_permission
[INFO] Generating Record class for table bear_user
[INFO] Generating Mapper Interface for table bear_user
[INFO] Generating SQL Map for table bear_user
[INFO] Generating Record class for table bear_role
[INFO] Generating Mapper Interface for table bear_role
[INFO] Generating SQL Map for table bear_role
[INFO] Generating Record class for table bear_permission
[INFO] Generating Mapper Interface for table bear_permission
[INFO] Generating SQL Map for table bear_permission
[INFO] Generating Record class for table bear_user_role
[INFO] Generating Mapper Interface for table bear_user_role
[INFO] Generating SQL Map for table bear_user_role
[INFO] Generating Record class for table bear_role_permission
[INFO] Generating Mapper Interface for table bear_role_permission
[INFO] Generating SQL Map for table bear_role_permission
[INFO] Saving file UserMapper.xml
[INFO] Saving file RoleMapper.xml
[INFO] Saving file PermissionMapper.xml
[INFO] Saving file UserRoleMapper.xml
[INFO] Saving file RolePermissionMapper.xml
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] Saving file Role.java
[INFO] Saving file RoleMapper.java
[INFO] Saving file Permission.java
[INFO] Saving file PermissionMapper.java
[INFO] Saving file UserRole.java
[INFO] Saving file UserRoleMapper.java
[INFO] Saving file RolePermission.java
[INFO] Saving file RolePermissionMapper.java
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.995 s
[INFO] Finished at: 2020-04-08T10:46:39+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

 

恭喜 SUCCESS!

mybatis逆向工程

标签:日志   ssi   generated   com   package   exist   内容   and   目录   

原文地址:https://www.cnblogs.com/wusiwee/p/12658771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!