mybatis逆向工程之一键生成数据库表对应的实体对象和mapper.xml映射文件

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 (用于扫描配置文件)


    org.mybatis.generator
    mybatis-generator-maven-plugin
    1.3.2
    
        true
        true
    

resource插件 也在


    
        
            src/main/java
            
                **/*.xml
            
            true
        
        
            src/main/resources
            
                **/*.properties
            
        
    

 在resource 目录下(也可以自定义目录,配置相关路径需要更改)创建 generatorConfig.xml 其内容为:相关注释已经加上


DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">



















<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">









targetProject="src/main/java">







targetProject="src/main/resources">





targetProject="src/main/java">





enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">

 

配置文件中 有一个 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包自行下载,把电脑里的路径拷贝过来就可以

双击运行插件 :

mybatis逆向工程之一键生成数据库表对应的实体对象和mapper.xml映射文件_第1张图片

运行日志:

[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

 我生成的持久层的 mapper路径: com.technologies.bear.dao   在xml的配置文件中配了

在resource目录下会多一个mapper文件夹,存放的是mapper.xml映射文件

恭喜 SUCCESS!

你可能感兴趣的:(mybatis逆向工程之一键生成数据库表对应的实体对象和mapper.xml映射文件)