第一阶段 dubboX和mybatis逆向工程

用了四天时间才完成品优购项目前两天的任务。

1、其中主要遇到的坑就是zookeeper服务消费者无法调用的问题。造成这个问题的主要原因就是忽略了dubbo的不同版本(阿里巴巴的在maven中央仓库有,2.8.4的一般则是当当网的)。后来改用了当当网的jar包,zookeeper服务消费者调用成功。这里特此强调,一定要注意dubbo的版本。

2、接触了新的前端框架AngularJS,这个前端框架非常方便。需要记住一些指令,做为一个后台。我没有精力去深入学习这些,能copy并update就行了。

3、接触了mybatis逆向工程,觉得sql使用Criteria类似与hibernate那一套。在使用链表,复杂查询时还是要用自己写的sql语句,效率应该会更高。还有包含一对多返回类型也是个问题,暂时没碰到。

这里的逆向工程主要是通过数据库生成java代码,缺点是链表查询要自己弥补(创建综合实体类 或 自己写链表查询SQL并映射链表结果集)。

当然还有一种是根据java实体类逆向生成数据库(外键关联关系也能创建),但是没什么意思,毕竟创建数据库简单而且也很重要。

这里对mybatis逆向生成Java代码做一个记录。

第一步,引入依赖的jar包。

!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>

第二步,创建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>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 #######修改1:修改成自己的数据库名,用户及密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql:///pinyougoudb" userId="root"
password="111111">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- targetProject:生成PO类的位置 #######修改2:修改生成实体类文件的包名-->
<javaModelGenerator targetPackage="com.mybatis.model"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 #######修改3:mapper.xml文件的包名-->
<sqlMapGenerator targetPackage="dbconfig"
targetProject=".\src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 #######4:mapper.xml文件的路径-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.mybatis.mapper"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_address"></table>
<table schema="" tableName="tb_areas"></table>
<table schema="" tableName="tb_brand"></table>
<table schema="" tableName="tb_cities"></table>
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_freight_template"></table>
<table schema="" tableName="tb_goods"></table>
<table schema="" tableName="tb_goods_desc"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_pay_log"></table>
<table schema="" tableName="tb_provinces"></table>
<table schema="" tableName="tb_seckill_goods"></table>
<table schema="" tableName="tb_seckill_order"></table>
<table schema="" tableName="tb_seller"></table>
<table schema="" tableName="tb_specification"></table>
<table schema="" tableName="tb_specification_option"></table>
<table schema="" tableName="tb_type_template"></table>
<table schema="" tableName="tb_user"></table>
</context>
</generatorConfiguration>

第三步,创建方法,执行逆向工程生成代码。

package com.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List; /**
* @Auther: lanhaifeng
* @Date: 2019/5/21 0021 18:20
* @Description: 生成逆向工程类
* @statement:
*/
public class GeneratorSqlmap {
public void generator() throws Exception{ List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null); }
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
} }
}

运行这个方法即可生成需要的实体类,以及mapper文件,mapper.xml。这个只能生成简单的sql语句。

期间配到两个错误:

1、com.mysql.cj.jdbc.exceptions.SQLError.createSQLException

把mysql-connector-java的版本改成 5.1.46即可

2、找不到配置文件:generatorConfig.xml

注意这个配置文件默认是放在工程目录下,不是resources下。如果放在src下,读取路径要加上src/generatorConfig.xml

生成成功

当然这个sql写的很一般,复杂的比如链表还是要自己写,优点是稳定(应该不会报错)。还有一点不能实现乐观锁,并发是个问题。

所以这个会不会都无所谓,不是加分项。懒人必备工具。

最新文章

  1. jquery中的ajax方法参数总是记不住,这里记录一下。
  2. MVC 基架不支持 Entity Framework 6 或更高版本
  3. 通过命令行连接Wifi
  4. HttpHandler过滤请求..
  5. AutoReleasePool 和 ARC 以及Garbage Collection
  6. [Java] 过滤流BufferedInputStream和BufferedOutputStream
  7. jenkins系列1--自动打tag
  8. PHP $_SERVER的详细参数及说明
  9. 如何制作网页小动画?——gif or png
  10. 浅谈href 和 src的区别
  11. MYSQL 的 6 个返回时间日期函数
  12. 转:Loadrunner上传文件解决办法(大文件)
  13. electron 学习笔记
  14. Android studio 2 版本升级 Android studio 3 版本注意事项
  15. Lua + Redis 解决高并发
  16. Lambda根据属性名字选择或筛选
  17. Node入门教程(6)第五章:node 模块化(上)模块化演进
  18. Django 实现登陆验证码
  19. java过滤器Filter笔记
  20. 【URLOS应用开发基础】10分钟制作一个nginx静态网站环境应用

热门文章

  1. 题解 洛谷P2258 【子矩阵】
  2. csr_matrix
  3. 《少年先疯队》第八次团队作业:Alpha冲刺第一天
  4. Dubbo官方文档
  5. Django REST framework认证权限和限制 源码分析
  6. img src防缓存
  7. IList&lt;&gt; IEnumerable&lt;&gt; ReadOnlyCollection&lt;&gt; 使用方向
  8. RookeyFrame Bug 线上创建的DLL被删除了 模块无法删除 临时解决
  9. Django+element ui前后端不分离的博客程序
  10. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解