发表于: 2018-02-01 20:26:01

1 644


今天完成的事情:

今天是准备动态生成然后再进行项目搭建的。但是不知道为什么,自动生成的东西奇奇怪怪,出了很多问题。

于是重新学习了一下自动生成:

首先需要在POM.XML文件下添加两个相关的依赖:

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

然后在文件中引入MybatisGenerator的插件:

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
完成这两步之后,添加关于MybatisGenerator的配置文件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>
<!-- 配置mysql 驱动jar包路径.用了绝对路径 -->
<classPathEntry location="A:/cangku/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" />

<context id="wangyongzhi_mysql_tables" targetRuntime="MyBatis3">
<!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
   <commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>

<!-- 数据库连接 -->
   <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                   connectionURL="jdbc:mysql://127.0.0.1:3306/db_zsl?useUnicode=true&amp;characterEncoding=UTF-8"
                   userId="root"
                   password="lucifer">
</jdbcConnection>

<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- 数据表对应的model层 -->
   <javaModelGenerator targetPackage="me.zhige.domain" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<!-- sql mapper 映射配置文件 -->
   <sqlMapGenerator targetPackage="me.zhige.mapper" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<!-- mybatis3中的mapper接口 -->
   <javaClientGenerator type="XMLMAPPER" targetPackage="me.zhige.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<!-- 数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO -->
   <table schema="db_zsl" tableName="user_t" domainObjectName="User"
          enableCountByExample="false" enableUpdateByExample="false"
          enableDeleteByExample="false" enableSelectByExample="false"
          selectByExampleQueryId="false">
</table>

</context>
</generatorConfiguration>

建立好之后,就新建服务类,最后的项目结构如下:

明天计划的事情:

明天好好的建立一下前端。

遇到的问题:

1.Mybatis Generator 生成的mapper只有insert方法 

– 首先检查generatorConfig.xml中table项中的属性 
enableSelectByPrimaryKey=”true” 
enableUpdateByPrimaryKey=”true” 
enableDeleteByPrimaryKey=”true” 
看看这几个属性是否设置成了false,默认的值是true。 

– 如果不是的话,那么就是你的表没有主键,无法根据primaryKey生成select、update、delete方法。

仔细检查之后发现主表没有建设主键。

2.生成的主类会很复杂。

<!-- 数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO -->
   <table schema="db_zsl" tableName="user_t" domainObjectName="User"
          enableCountByExample="false" enableUpdateByExample="false"
          enableDeleteByExample="false" enableSelectByExample="false"
          selectByExampleQueryId="false">

可以通过下面的属性来设置是否生成相关的类,可以设置为false关闭。

enableCountByExample

3.如何扫描多个SQLXML文件

<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
   <property name="dataSource" ref="dataSource"/>
<!-- 扫描model包 使用别名 -->
   <property name="typeAliasesPackage" value="com.jnshu.model"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
   <property name="mapperLocations">
<array>
<value>classpath:com/jnshu/mapper/CountMapper.xml</value>
<value>classpath:com/jnshu/mapper/ExcellentMapper.xml</value>
<value>classpath:com/jnshu/mapper/PositionMapper.xml</value>
</array>
</property>

可以通过<array>属性来扫描多个,而且在打字的时候,是可以直接回车输入的。

<array>
<value>classpath:com/jnshu/mapper/CountMapper.xml</value>
<value>classpath:com/jnshu/mapper/ExcellentMapper.xml</value>
<value>classpath:com/jnshu/mapper/PositionMapper.xml</value>
</array>


收获:

算是彻底掌握了自动生成,对项目的构建有了新的认识。


进度:任务四的基本架构已经成型。



返回列表 返回列表
评论

    分享到