mybatis自动生成mapping文件

来源:互联网 发布:比特币软件 编辑:程序博客网 时间:2024/05/21 19:28

老的经典框架像ssh ssm ssmm,数据传输层用hibernate和mabatis的属于比较流行的,hibernate的映射文件有几种生成方式,这里主要结束mabatis映射文件的生成方式:

1、环境

     数据库:MariaDB(就是mysql)

     开发工具:springsts-3.9.0(版本启动需要jdk1.8,如果环境变量不是1.8,自己设置一下init文件的虚拟机环境,设置成1.8)

 2、只结束两种简单的方式

(1)、下载mybatis-generator-core-1.3.2.jar和mysql-connector-java-5.1.35.jar放到一个文件中,新建一个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="E:\software\SSMM\CmdGenerator\mysql-connector-java-5.1.35.jar" /><!--<classPathEntry location="xxxxx\ojdbc14.jar" /> --><context id="DB2Tables" targetRuntime="MyBatis3"><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator><!-- 数据库链接URL、用户名、密码 --><jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://*。*。*。*:*/*" userId="*" password="*"> <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="xe" password="xe">--></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- 生成模型的包名和位置 --><javaModelGenerator targetPackage="*.model" targetProject="E:\**\src"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 生成的映射文件包名和位置 --><sqlMapGenerator targetPackage="*.mapping" targetProject="E:\**\src"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 生成DAO的包名和位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="*.dao" targetProject="E:\**\src"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 要生成那些表(更改tableName和domainObjectName就可以) --><table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /><table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /><table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /><table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /><table tableName="permission" domainObjectName="Permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /></context></generatorConfiguration>
很简单,从上往下就是根据数据库驱动包连接你要连接的数据库,生成名字是model、mapping、dao的文件夹,存在在targetProject指向的绝对路径下,并且根据数据库表名实体类的映射;tableName->domainObjectName


第二部进入dos命令,执行java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

目录结构如下:


然后再从src下分别复制出类文件放到项目中


(2)第二种更简单,直接在spring-sts ->help ->eclipse marketplace 搜索mybatis



点击install,创建一个maven项目,右键新建->others->



创建一个generatorConfig.xml文件,里面内容和上面那个文件一样,复制过来把绝对路径改成项目的绝对路径就行,然后右键general mybatis/ibatis artifacts,完事了。


每次新建一个数据库表,增删查改可以通过这两种方式生成,剩下的多表关联还是要自己写语句,mybatis简单,就是写sql语句。

阅读全文
0 0