• SSM 框架基于ORACLE集成TKMYBATIS 和GENERATOR自动生成代码(Github源码)


    基于前一个博客搭建的SSM框架

    https://www.cnblogs.com/jiangyuqin/p/9870641.html

    源码:https://github.com/JHeaven/ssm-easy-demo

    实现基于oracle数据库集成tkmybatis 和 gengerator自动生成代码

    先看下完成之后的目录结构

     

    首先pom.xml里面引入相应的包和插件

    1      <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
    2         <dependency>
    3             <groupId>tk.mybatis</groupId>
    4             <artifactId>mapper</atifactId>
    5             <version>${tkmybatis.version}</version>
    6         </dependency>    

     

    引入插件包的时候,注意标签的位置,观察和pluginManagement标签的相对位置。搞错了可是出不来的,我尽量给出较多的代码给大家参考。

     1 <build>
     2         <finalName>ssm-sc</finalName>
     3         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
     4             <plugins>
     5                 <plugin>
     6                     <artifactId>maven-clean-plugin</artifactId>
     7                     <version>3.0.0</version>
     8                 </plugin>
     9                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
    10                 <plugin>
    11                     <artifactId>maven-resources-plugin</artifactId>
    12                     <version>3.0.2</version>
    13                 </plugin>
    14                 <plugin>
    15                     <artifactId>maven-compiler-plugin</artifactId>
    16                     <version>3.7.0</version>
    17                 </plugin>
    18                 <plugin>
    19                     <artifactId>maven-surefire-plugin</artifactId>
    20                     <version>2.20.1</version>
    21                 </plugin>
    22                 <plugin>
    23                     <artifactId>maven-war-plugin</artifactId>
    24                     <version>3.2.0</version>
    25                 </plugin>
    26                 <plugin>
    27                     <artifactId>maven-install-plugin</artifactId>
    28                     <version>2.5.2</version>
    29                 </plugin>
    30                 <plugin>
    31                     <artifactId>maven-deploy-plugin</artifactId>
    32                     <version>2.8.2</version>
    33                 </plugin>
    34 
    35             </plugins>
    36         </pluginManagement>
    37         <plugins>
    38             <!-- 自动生成代码插件 -->
    39             <plugin>
    40                 <groupId>org.mybatis.generator</groupId>
    41                 <artifactId>mybatis-generator-maven-plugin</artifactId>
    42                 <version>${mybatis.generator.version}</version>
    43                 <configuration>
    44                     <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
    45                     <verbose>true</verbose>
    46                     <overwrite>true</overwrite>
    47                 </configuration>
    48 
    49                 <dependencies>
    50                     <dependency>
    51                         <groupId>org.mybatis.generator</groupId>
    52                         <artifactId>mybatis-generator-core</artifactId>
    53                         <version>${mybatis.generator.version}</version>
    54                     </dependency>
    55                     <dependency>
    56                         <groupId>com.oracle</groupId>
    57                         <artifactId>ojdbc6</artifactId>
    58                         <version>${oracle.version}</version>
    59                     </dependency>
    60                     <dependency>
    61                         <groupId>tk.mybatis</groupId>
    62                         <artifactId>mapper</artifactId>
    63                         <version>${tkmybatis.version}</version>
    64                     </dependency>
    65                 </dependencies>
    66             </plugin>
    67         </plugins>
    68     </build>

    还得在pom.xml里面设置一些变量,主要是给自动生成代码的配置文件generatorConfig.xml用的,具体是啥看generatorConfig.xml里面的注释。

     1 <!-- MyBatis Generator -->
     2         <mybatis.generator.version>1.3.7</mybatis.generator.version>
     3         <tkmybatis.version>4.0.4</tkmybatis.version>
     4 
     5         <mapper.plugin>tk.mybatis.mapper.generator.MapperPlugin</mapper.plugin>
     6         <mapper.Mapper>com.jiangyuqin.utils.MyMapper</mapper.Mapper>
     7 
     8         <ModelTargetPackage>com.jiangyuqin.model</ModelTargetPackage>
     9         <ModelTargetProject>src/main/java</ModelTargetProject>
    10 
    11         <sqlMapperTargetPackage>mapper</sqlMapperTargetPackage>
    12         <sqlMapperTargetProject>src/main/resources</sqlMapperTargetProject>
    13 
    14         <clientTargetPackage>com.jiangyuqin.mapper</clientTargetPackage>
    15         <clientTargetProject>src/main/java</clientTargetProject>

    新建自己的mapper类,用来被自动生成的mapper继承,注意这个类需要被放到不可扫描到的位置。

     1 package com.jiangyuqin.utils;
     2 
     3 import tk.mybatis.mapper.common.Mapper;
     4 import tk.mybatis.mapper.common.MySqlMapper;
     5 
     6 /**
     7  * 基础 mapper
     8  * 不可被扫描到
     9  * BaseMapper<T>
    10  * MySqlMapper<T>
    11  * IdsMapper<T>
    12  * ConditionMapper<T>
    13  * ExampleMapper<T>
    14  * @param <T>
    15  */
    16 public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T> {
    17     
    18 }

    新建配置文件

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
     3         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
     4 <generatorConfiguration>
     5 
     6     <properties resource="generator/db.properties"/>
     7 
     8     <context id="Oracle" targetRuntime="MyBatis3Simple" defaultModelType="flat">
     9         <!-- TKmybatis配置 -->
    10         <property name="javaFileEncoding" value="UTF-8"/>
    11         <property name="beginningDelimiter" value="`"/>
    12         <property name="endingDelimiter" value="`"/>
    13         <plugin type="${mapper.plugin}">
    14             <property name="mappers" value="${mapper.Mapper}"/>
    15         </plugin>
    16 
    17         <commentGenerator>
    18             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
    19             <property name="suppressAllComments" value="true"/>
    20         </commentGenerator>
    21         <!-- 数据库链接URL、用户名、密码 -->
    22         <jdbcConnection driverClass="${db.driver}"
    23                         connectionURL="${db.url}"
    24                         userId="${db.user}"
    25                         password="${db.password}" />
    26 
    27         <javaTypeResolver>
    28             <property name="forceBigDecimals" value="false" />
    29         </javaTypeResolver>
    30         <!-- 生成模型的包名和位置 -->
    31         <javaModelGenerator targetPackage="${ModelTargetPackage}" targetProject="${ModelTargetProject}" />
    32         <!-- 生成的映射文件包名和位置 -->
    33         <sqlMapGenerator targetPackage="${sqlMapperTargetPackage}" targetProject="${sqlMapperTargetProject}" />
    34         <!-- 生成DAO的包名和位置 -->
    35         <javaClientGenerator targetPackage="${clientTargetPackage}" targetProject="${clientTargetProject}" type="XMLMAPPER" />
    36         <!-- 要生成的那些表 -->
    37 
    38         <table tableName="Z_DBA_DATASOURCE" domainObjectName="DbaDatasource" >
    39             <generatedKey column="UUID" sqlStatement="SELECT 1 FROM DUAL" identity="true"/>
    40         </table>
    41         <table tableName="Z_SYS_LOG" domainObjectName="SysLog" >
    42             <generatedKey column="UUID" sqlStatement="SELECT 1 FROM DUAL" identity="true"/>
    43         </table>
    44         <table tableName="Z_USR_LOGINLOG" domainObjectName="UsrLoginLog" >
    45             <generatedKey column="UUID" sqlStatement="SELECT 1 FROM DUAL" identity="true"/>
    46         </table>
    47         <table tableName="Z_USR_ADMIN" domainObjectName="UsrAdmin" >
    48             <generatedKey column="UUID" sqlStatement="SELECT 1 FROM DUAL" identity="true"/>
    49         </table>
    50     </context>
    51 </generatorConfiguration>
    其中table标签中sqlStatement这个参数,作废,因为oracle没有自增功能,与其去适应复杂的框架来实现妥协的不够保险的自增,还不如在java里面用UUID传参插入。
    (或者以后自己写一个生成UUID的方法,暂时直接调用JAVA提供的。)

    db.properties就不给出了,无非就是一些数据连接。
    到这基本就可以开始生成代码了。

    生成就结束了。
    让我们测试一下。
    1.全量查询

     

     

    2.插入

     

    报错了,查看日志。

     

    插入成功,回查UUID的时候出错了,说明我们在generatorConfig.xml中配的 sqlStatement="select 1 from dual" 没起作用
    那就先手动修改吧
    在实体类中,GeneratedValue注释中,添加参数 generator = "SELECT SYS_GUID() FROM DUAL",这个回传的值没有太大的意义,可以写为 select 1 from dual

    尝试插入
    成功。


  • 相关阅读:
    C#基础视频教程5.3 如何编写简单的超级热键
    spring boot中注入jpa时报could not autowire.No beans of 'PersonRepository' type found
    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别
    idea如何搭建springboot框架
    Fiddler建好代理后,能连到手机,但手机不能上网了是什么原因
    如何用Fiddler对Android应用进行抓包
    【fiddler】抓取https数据失败,全部显示“Tunnel to......443”
    将excel的数据导入到数据库后都乱码了是怎么回事
    java保存繁体字到数据库时就报错Incorrect string value: 'xF0xA6x8Dx8BxE5xA4...' for column 'name' at row 1
    将爬取的网页数据保存到数据库时报错不能提交JPA,Caused by: java.sql.SQLException: Incorrect string value: 'xF0x9Fx98xB6 xE2...' for column 'content' at row 1
  • 原文地址:https://www.cnblogs.com/jiangyuqin/p/9883244.html
Copyright © 2020-2023  润新知