最近在视频上学了企业级权限管理课程,里面的generator这个插件不错,省去了很多编写mapper的时间,特此标记一下步骤,以备不时之需。
首先需要两个jar包,一个是generator工具的jar,一个是连数据库的jar,这里以mysql为例:
然后编写xml,配置好数据库连接信息和生成的dao的位置:
<?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:/ProjectSelf/study/permission/generator/mysql-connector-java-5.1.34.jar" /> <!-- 1 --> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/permission?characterEncoding=utf8" userId="root" password="mysql"> <!-- 2 --> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <!-- 3 --> <javaModelGenerator targetPackage="com.permission.model" targetProject="E:/ProjectSelf/study/permission/generator/src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的映射文件包名和位置 --> <!-- 4 --> <sqlMapGenerator targetPackage="com.permission.mapper" targetProject="E:/ProjectSelf/study/permission/generator/src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <!-- 5 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.permission.dao" targetProject="E:/ProjectSelf/study/permission/generator/src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --><!-- 6 --> <table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="sys_dept" domainObjectName="SysDept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="sys_acl" domainObjectName="SysAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="sys_acl_module" domainObjectName="SysAclModule" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="sys_role" domainObjectName="SysRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="sys_role_acl" domainObjectName="SysRoleAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="sys_role_user" domainObjectName="SysRoleUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="sys_log" domainObjectName="SysLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
最后,cd进入generator的jar包的文件位置,输入命令即可:
D:\StudyInfos\permission>cd generator
D:\StudyInfos\permission\generator>java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
PS:如果运行多次命令会覆盖掉生成的类,但是生成的mapper文件里面的内容不会覆盖,即会生成重复的resultMap之类的标签,解决方法:删掉重搞呗。