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生 ...
随机推荐
- Java 字节流和字符流
程序中都是以流的形式进行数据的传输和保存,在java.io包中数据流操作的两大类是字节流和字符流. 1. 字节流 InputStream和OutputStream是所有表示字节流的类的父类,它们都是抽 ...
- python练习六十二:文件处理,往文件中所有添加指定的前缀
往文件中所有添加指定的前缀 方法一:open方法 f_r = open('text.txt') f_w = open('text_new.txt','w+') i = 0 while True: i ...
- from表单,图片预览,和表单提交
<form> <input id="file" class="topsub-file" type="file" name= ...
- JVM调优总结-Xmx -Xms -Xmn -Xss
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...
- python 横向比较最大值 贴标签
df['dfda']=pd.Series(df[['a','b','v']].idxmax(axis=1),index=df.index)#横向比较最大值并返回列名,比循环快N倍
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- javascript中typeof与instanceof的区别
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前 ...
- 3d Max 2015安装失败怎样卸载3dsmax?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- linux工具:快速返回某级父目录--bd
当我们在linux服务器上切换父目录时,通常使用cd ../../,有几级目录就输入几次"../",如果目录嵌套的过深,就会有点晕菜...因此,本次介绍的这款工具,可以快速的返回指 ...
- Git 设置 Hook
Git 设置 hook Hook 就是钩子,在需要的时候调用,根据每个钩子脚本(函数)的返回值决定下一步的操作. 在使用 Git 的过程中,有时候需要定制 Git 以便满足实际的需求. 需求 在一个项 ...