首页 技术 正文
技术 2022年11月22日
0 收藏 331 点赞 3,391 浏览 2405 个字

使用maven集成mybatis-generator插件生成Mybatis的实体类,DAO接口和Map映射文件

本例中,使用的是mysql数据库

前提:表已经建好

   mybatis框架的jar包,数据库驱动程序jar包以及MyBatis生成器jar包导好

一、配置Maven pom.xml 文件

在pom.xml增加以下插件:

            <plugin>                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.3.2</version>                <configuration>                    <verbose>true</verbose>                    <overwrite>true</overwrite>                </configuration>            </plugin>

二、创建配置文件:generatorConfig.xml

存放路径如下图:插件默认会读到src/main/resources目录下的generatorConfig.xml 文件。

mybatis自动生成代码

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><!-- 数据库驱动-->    <classPathEntry  location= "D:/mvnRepository/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar" />    <context id="DB2Tables"  targetRuntime="MyBatis3">        <commentGenerator>            <property name="suppressDate" value="true"/>            <!-- 是否去除自动生成的注释 true:是 : false:否 -->            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <!--数据库链接URL,用户名、密码 -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" userId="root" password="root">        </jdbcConnection>        <javaTypeResolver>            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <!-- 生成模型的包名和位置-->        <javaModelGenerator targetPackage="test.domain" targetProject="src/main/java">            <property name="enableSubPackages" value="true"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!-- 生成映射文件的包名和位置-->        <sqlMapGenerator targetPackage="test.mapping" targetProject="src/main/java">            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <!-- 生成DAO的包名和位置-->        <javaClientGenerator type="XMLMAPPER" targetPackage="test.IDao" targetProject="src/main/java">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <!-- 要生成的表 tableName是数据库中的表名或视图名 -->        <table tableName="student"></table>    </context></generatorConfiguration>    

三、生成代码

选择pom.xml文件,击右键先择Run AS——>Maven Build… ——>在Goals框中输入:mybatis-generator:generate 

选择项目 按 F5 刷新项目 出现生成的代码。

mybatis自动生成代码

注意:默认设置会生成一大堆罗哩罗嗦的Example类,主要是用各种不同的条件来操作数据库,大部分是用不到的

可通过以下配置取消Example

 <table tableName="student"                schema="general"                domainObjectName="Student"                enableCountByExample="false"                enableUpdateByExample="false"                enableDeleteByExample="false"                enableSelectByExample="false"                selectByExampleQueryId="false" >            <property name="useActualColumnNames" value="true"/>        </table>  
 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731