使用Mybatis-Generator自动生成Dao、entity、Mapping教程
来源:互联网 发布:sql注入是什么 编辑:程序博客网 时间:2024/05/29 08:06
eclipse版本
1.给你的ide下载这个插件,并确定成功可用。具体百度
(eclipse直接搜索安装就行)
2.建议建立一个新的maven项目,然后生成的文件直接移过去。如图:
3.pom配置文件 加依赖呗
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mybatis-generator.demo</groupId> <artifactId>mybatis-generator</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mybatis-generator Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!--mybatis 数据访问层 start --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <finalName>mybatis-generator</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> <version>3.3</version> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> <configuration> <!--配置文件的路径--> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </pluginManagement> </build></project>
4.Mybatis-Generator的配置 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="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/> <context id="test" targetRuntime="MyBatis3"> <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表; 一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 --> <property name="autoDelimitKeywords" value="false"/> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- ↓↓↓↓↓↓↓↓↓↓是否启用重写Equals HashCode 的插件↓↓↓↓↓↓↓↓ --> <!--<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> --> <!-- ↓↓↓↓↓↓↓↓↓↓是否启用序列化的插件↓↓↓↓↓↓↓↓ --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <!-- ↓↓↓↓↓↓↓↓↓↓是否启用ToString插件↓↓↓↓↓↓↓↓ --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 --> <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true --> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/datebook" userId="root" password="root"> </jdbcConnection> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 false:默认, scale>0;length>18:使用BigDecimal; scale=0;length[10,18]:使用Long; scale=0;length[5,9]:使用Integer; scale=0;length<5:使用Short; --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- java模型创建器,是必须要的元素 负责:1,key类(见context的defaultModelType);2,java类;3,查询类 targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制; targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录 --> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.generator.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> <!--<property name="useActualColumnNames" value="true" />--> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="com.generator.mapping" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.generator.dao" implementationPackage="com.generator.dao.impl" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素 选择的table会生成一下文件: 1,SQL map文件 2,生成一个主键类; 3,除了BLOB和主键的其他字段的类; 4,包含BLOB的类; 5,一个用户生成动态查询的条件类(selectByExample, deleteByExample),可选; 6,Mapper接口(可选) tableName(必要):要生成对象的表名; 注意:大小写敏感问题。正常情况下,MBG会自动的去识别数据库标识符的大小写敏感度,在一般情况下,MBG会 根据设置的schema,catalog或tablename去查询数据表,按照下面的流程: 1,如果schema,catalog或tablename中有空格,那么设置的是什么格式,就精确的使用指定的大小写格式去查询; 2,否则,如果数据库的标识符使用大写的,那么MBG自动把表名变成大写再查找; 3,否则,如果数据库的标识符使用小写的,那么MBG自动把表名变成小写再查找; 4,否则,使用指定的大小写格式查询; 另外的,如果在创建表的时候,使用的""把数据库对象规定大小写,就算数据库标识符是使用的大写,在这种情况下也会使用给定的大小写来创建表名; 这个时候,请设置delimitIdentifiers="true"即可保留大小写格式; 可选: 1,schema:数据库的schema; 2,catalog:数据库的catalog; 3,alias:为数据表设置的别名,如果设置了alias,那么生成的所有的SELECT SQL语句中,列名会变成:alias_actualColumnName 4,domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面; 5,enableInsert(默认true):指定是否生成insert语句; 6,enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get); 7,enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update); 8,enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete); (会建个example实体类) 9,enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句; 10,enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句; 11,enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询); 12,enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性); 13,modelType:参考context元素的defaultModelType,相当于覆盖; 14,delimitIdentifiers:参考tableName的解释,注意,默认的delimitIdentifiers是双引号,如果类似MYSQL这样的数据库,使用的是`(反引号,那么还需要设置context的beginningDelimiter和endingDelimiter属性) 15,delimitAllColumns:设置是否所有生成的SQL中的列名都使用标识符引起来。默认为false,delimitIdentifiers参考context的属性 注意,table里面很多参数都是对javaModelGenerator,context等元素的默认属性的一个复写; --> <!-- 要生成哪些表 --> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="book" domainObjectName="Book" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context></generatorConfiguration>
5.对项目右键Maven—>updateProject
(一定要建立相应的文件夹,插件不会自动建立文件夹。如这个的src/main/java)
安装完成之后,(第一次的话输入命令:mybatis-generator:generate)
然后刷新项目就行了。整体结构如图:
IDEA版本
1.新建maven项目
2.修改pom文件
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mybatis-generator.demo</groupId> <artifactId>mybatis-generator</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--mybatis 数据访问层 start --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> <!--<scope>runtime</scope> --> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--配置文件的位置--> <configurationFile>src/main/resources/generatorConfig.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.5</version> </dependency> </dependencies> </plugin> </plugins> </build></project>
3.新建配置文件
<?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="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/> <context id="test" targetRuntime="MyBatis3"> <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表; 一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 --> <property name="autoDelimitKeywords" value="false"/> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- ↓↓↓↓↓↓↓↓↓↓是否启用重写Equals HashCode 的插件↓↓↓↓↓↓↓↓ --> <!--<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> --> <!-- ↓↓↓↓↓↓↓↓↓↓是否启用序列化的插件↓↓↓↓↓↓↓↓ --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <!-- ↓↓↓↓↓↓↓↓↓↓是否启用ToString插件↓↓↓↓↓↓↓↓ --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 --> <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true --> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/datebook" userId="root" password="root"> </jdbcConnection> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 false:默认, scale>0;length>18:使用BigDecimal; scale=0;length[10,18]:使用Long; scale=0;length[5,9]:使用Integer; scale=0;length<5:使用Short; --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- java模型创建器,是必须要的元素 负责:1,key类(见context的defaultModelType);2,java类;3,查询类 targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制; targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录 --> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.generator.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> <!--<property name="useActualColumnNames" value="true" />--> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="com.generator.mapping" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.generator.dao" implementationPackage="com.generator.dao.impl" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素 选择的table会生成一下文件: 1,SQL map文件 2,生成一个主键类; 3,除了BLOB和主键的其他字段的类; 4,包含BLOB的类; 5,一个用户生成动态查询的条件类(selectByExample, deleteByExample),可选; 6,Mapper接口(可选) tableName(必要):要生成对象的表名; 注意:大小写敏感问题。正常情况下,MBG会自动的去识别数据库标识符的大小写敏感度,在一般情况下,MBG会 根据设置的schema,catalog或tablename去查询数据表,按照下面的流程: 1,如果schema,catalog或tablename中有空格,那么设置的是什么格式,就精确的使用指定的大小写格式去查询; 2,否则,如果数据库的标识符使用大写的,那么MBG自动把表名变成大写再查找; 3,否则,如果数据库的标识符使用小写的,那么MBG自动把表名变成小写再查找; 4,否则,使用指定的大小写格式查询; 另外的,如果在创建表的时候,使用的""把数据库对象规定大小写,就算数据库标识符是使用的大写,在这种情况下也会使用给定的大小写来创建表名; 这个时候,请设置delimitIdentifiers="true"即可保留大小写格式; 可选: 1,schema:数据库的schema; 2,catalog:数据库的catalog; 3,alias:为数据表设置的别名,如果设置了alias,那么生成的所有的SELECT SQL语句中,列名会变成:alias_actualColumnName 4,domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面; 5,enableInsert(默认true):指定是否生成insert语句; 6,enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get); 7,enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update); 8,enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete); (会建个example实体类) 9,enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句; 10,enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句; 11,enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询); 12,enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性); 13,modelType:参考context元素的defaultModelType,相当于覆盖; 14,delimitIdentifiers:参考tableName的解释,注意,默认的delimitIdentifiers是双引号,如果类似MYSQL这样的数据库,使用的是`(反引号,那么还需要设置context的beginningDelimiter和endingDelimiter属性) 15,delimitAllColumns:设置是否所有生成的SQL中的列名都使用标识符引起来。默认为false,delimitIdentifiers参考context的属性 注意,table里面很多参数都是对javaModelGenerator,context等元素的默认属性的一个复写; --> <!-- 要生成哪些表 --> <table tableName="block_list" domainObjectName="BlockList" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context></generatorConfiguration>
4.双击666运行(main/java里面的包自己建立如下图)
第三种方法 直接执行命令
其中的数据库名称,用户名,密码以及表表根据自己的具体情况来修改,配置中的targetProject是目标文件夹,不会自动生成,需要自己创建。
配置文件写好以后,打开cmd,将目录切换到lib目录下(快捷方式shift+右键),执行脚本: java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
然后就可以在src目录下找到相应的文件,每个数据库表都会对应三个文件(实体类、接口、配置文件)。
阅读全文
0 0
- 使用Mybatis-Generator自动生成Dao、entity、Mapping教程
- 使用MyBatis Generator插件自动生成Dto、Dao、Mapping
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 切换系统默认的终端
- 【caffe-MATLAB】自己编译faster-rcnn的MATLAB版本
- 二维数组与数组指针关系
- 计算机网络之基础知识(一)
- Windows下Faster R-CNN 配置/Matlab版本编译
- 使用Mybatis-Generator自动生成Dao、entity、Mapping教程
- Nginx 安装配置
- 树
- Vue中this.$router.push参数获取
- 单向链表
- 82岁日本老太太自学编程获苹果库克接见:啥时候开始学都不晚
- Kotlin进阶之反射
- Java博客文章收藏
- 几个网络相关概念