发表于: 2016-10-17 15:37:36
0 2059
一、今天完成
1.需要一个程序用于快速创建DB的测试数据,字段需要根据po类的变量类型生成。
考虑到如果要方法能够适应所有的po类,因为不知道传入值,考虑使用java-RTTI的方式解决,入参设置为Class类型,需要识别出:
1)有多少个变量:需求动态变化,以后新建表还要加;
2)入参是什么:需根据入参选择生成方法,需要调用的serviceImpl名字,及创建数据条数;
3)怎么实现需求:根据名字调用service接口方法生成pojo实例及数据,依赖dao接口方法的插入数据(dao接口通过IOC注入)
2.需要一个程序基于excel表或mysql中的表生成po类
在网上找到一个基于mybatis生成pojo-dao的方法,在pom.xml中添加了mybatis插件mybatis-generator-core
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/mybatis/generator.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
插件映射一个generator.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>
<!-- 数据库驱动包位置 -->
<classPathEntry location="D:\.m2\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar" />
<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.222:3306/codetrainclass" userId="admin" password="admin">
<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类的包名和位置,这里配置将生成的实体类放在cn.chejinbao.ecommerce.entity这个包下 -->
<javaModelGenerator targetPackage="org.oyyd.po" targetProject="D:\IdeaProjects\Task4\src\main\java\">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在cn.chejinbao.ecommerce.mapping这个包下 -->
<sqlMapGenerator targetPackage="org.oyyd.mapping" targetProject="D:\IdeaProjects\Task4\src\main\java\">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置,这里配置将生成的dao类放在cn.chejinbao.ecommerce.dao这个包下 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="org.oyyd.dao" targetProject="D:\IdeaProjects\Task4\src\main\java\">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="web_users" domainObjectName="WebUsers" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="class" domainObjectName="Classes" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="student" domainObjectName="Students" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="graduate" domainObjectName="Graduates" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
最后运行maven插件 mybatios-generator generate生成po类、mapper的dao类、mapper.xml
二、明天计划
1.实现下生成数据的代码,这个部分应该涉及到一些有用的java和spring知识,顺便学习下
。
三、遇到问题
1.测试几次用RTTI,突然发现我的Dao想依赖Ioc,如程序里面直接Class.newInstance没办法给Dao的bean完成注入,取舍后觉得还是依赖spring去尝试,加深框架认识。
四、收获
无
评论