mybatis怎样自动生成java类,配置文件?
其实没有什么东西是可以自动生成的,只不过是别人已经写好了,你调用罢了。
所以想要mybatis自动生成java类,配置文件等,就必须要一些配置和一些jar包。当然这些配置也很简单。
为了有个初步的认识,首先我列出了所需要的文件:

其中标红的比较重要。好了,让我们开始吧
1.首先需要在数据库建好表,随便建几个就好。
2.下载mybatis-generator-core包
然后搜索mybatis-generator-core下载即可
3.下载mysql-connector-java包
想必大家这个包都很熟悉,因为它 就是mysql的java语言驱动包
因为我们是根据数据库表来生产配置文件,当然需要连接到数据库了,那也就是需要驱动包了。
然后搜索mysql-connector-java,选择需要的版本下载即可
4.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>
<!-- clas<font size="" color=""></font>sPathEntry:数据库的JDBC驱动 -->
<classPathEntry location="D:\study\spring-mvc-mybatis-maven\mysql-connector-java-5.0.3-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库的url 用户名 密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/sy"
userId="root"
password="mysql">
</jdbcConnection> <javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- 生成模型的包名和位置:自动生成代码的位置, -->
<javaModelGenerator targetPackage="com.sy.model" targetProject="D:\study\spring-mvc-mybatis-maven\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator> <!-- 自动生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.sy.mapping" targetProject="D:\study\spring-mvc-mybatis-maven\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Dao 的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.sy.dao" targetProject="D:\study\spring-mvc-mybatis-maven\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="tbug" domainObjectName="Bug" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tmenu" domainObjectName="Menu" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tonline" domainObjectName="Online" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tresource" domainObjectName="Resource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="trole" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="trole_tresource" domainObjectName="RoleResource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tuser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tuser_trole" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context> </generatorConfiguration>
generator.xml
其中generator.xml文件里都有很详细的注释,大家一看就明白,我们需要做的有
(1) 修改mysql驱动的路径,小编的路径如下:
<classPathEntry location="D:\study\spring-mvc-mybatis-maven\mysql-connector-java-5.0.3- bin.jar" />
(2)修改 数据库的url 用户名 密码
(3)修改保存自动生成文件的路径,小编的路径为
<javaModelGenerator targetPackage="com.sy.model" targetProject="D:\study\spring-mvc-mybatis- maven\src">
这里大家可以建一个src空文件夹,然后把全路径复制覆盖小编的 路径即可。
注:这里的model,mapper,dao三个路径都需要修改,否则就找不到路径了
5.好了,准备工作都做好了,一个命令即可生成
首先cmd进入到generator.xml这个文件的路径下 如
cd d:
cd study\spring-mybatis-maven
(win 7 下有快捷方式进入:
在generator.xml目录下,单击右键:在此处打开命令窗口(W))
运行命令:
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
是不是成功了?快去看看刚刚新建的src文件夹吧,看是不是已经有代码了。
mybatis怎样自动生成java类,配置文件?的更多相关文章
- Springboot mybatis generate 自动生成实体类和Mapper
https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...
- java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件
前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...
- mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件
Mybatis的逆向工程非常简单,只要一个配置文件和一个Main方法就可以实现,下面以maven工程为例: (1)在pom.xml中引入依赖包 <dependency> <group ...
- mybatis generator自动生成 实体类, sqlmap配置文件 详细介绍
我使用的是Eclipse Luna 装了自己常用的插件, generator也是其中一个推荐下载 MyBatis_Generator_1.3.1.zip离线安装包 <?xml version=& ...
- 利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件
1. mybatis-generator-core-1.3.5.jar 下载地址:https://github.com/mybatis/generator/releases 2. msyql-conn ...
- 运用Hibernate-Tools自动生成Java类和schema时,出现not found while looking for property...异常
问题描述: 在使用Hibernate-tools时出现not found while looking for property...(具体信息省略). 问题分析: 我找到出错对应的hbm.xml文件, ...
- MyBatis代码自动生成(利用命令)
这几天在学习springmvc,需要用到mybatis,所以研究了一下mybatis自动代码生成,当然也可以手动敲,但是那样效率非常的慢,并且出错率也是很高的,利用MyBatis生成器自动生成实体类. ...
- MyBatis代码自动生成
MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实 ...
- 使用MyBatis Generator自动生成MyBatis的代码
这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...
随机推荐
- 求用1g、2g、3g的砝码(每种砝码有无穷多个)称出10g的方案有几种
#include <iostream> using namespace std; // ; // sup是保存多项式的数组,sup[n]中的值代表指数为i的系数 ,下标i是x的指数 // ...
- Go语言基础之10--面向对象编程2之方法
一.方法的定义 之前我们学习了结构体(struct),其仅仅是对数据的封装,并没有行为方法,还不是一个完全的面向对象的思路,所以现在我们来学习在结构体的基础上如何去定义一个方法.结构体(类)+方法=完 ...
- js css 点亮 星级评分
利用css 和 js 实现星级评分 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- 使用Spring和JQuery实现视频文件的上传和播放
Spring MVC可以很方便用户进行WEB应用的开发,实现Model.View和Controller的分离,再结合Spring boot可以很方便.轻量级部署WEB应用,这里为大家介绍如何使用Spr ...
- testlink数据库访问密码修改
testlink重启后数据库连接不上将会报错 错误:1045 - Access denied for user 'my_db '@'localhost' (using password: YES) 怎 ...
- SVN更改通知的工具commitmonitor
SVN更改通知的工具commitmonitor 下载地址: https://sourceforge.net/projects/commitmonitor/files/latest/download 工 ...
- Git钩子设置自动构建Jenkins
打开Git仓库,找到对应的项目,点击“仓库设置”,左侧点击“管理Git钩子”,如下图所示: 因为是push之后触发自动构建的,选择“post-receive”进行编辑 #!/bin/bash #提取分 ...
- my.梦幻手游_XP
1.http://my.netease.com/thread-459708-1-1.html 2. 3.
- swiper控件(回调函数)
来源 属性: swiper.slides.length 1.onInit(swiper): function(){...} swiper初始化完成,会调回调 onInit 方法 获取当前swiper ...
- HBase 的Get(读),Put(写),Delete(删),Scan(扫描)和Increment(列值递增)
一.HBase介绍 1.基本概念 HBase是一种Hadoop数据库,经常被描述为一种稀疏的,分布式的,持久化的,多维有序映射,它基于行键.列键和时间戳建立索引,是一个可以随机访问的存储和检索数据的平 ...