先说一下,这个搭建之前用的项目名叫mybatis,但是在做项目(blogs)的时候发现有些地方需要修改,也就是配置的不合理,同时也解决了一些实际上的报错问题,所以你可能看到的配置文件的包名有的是 cy.** 有的是com.andyc.** 。我都上传了实际项目中的配置,你只需要根据你自己的项目名称来修改成一致即可,配置问文件!
一、准备工作:
1、JDK、Eclipse(MyEclipse)、Tomcat、MySql或其他数据库、Navicat等数据库管理软件
2、如果不清楚需要哪些jar包、或者不是用Maven,可下载jar包,下载地址
3、查找jar包版本信息网址推荐:
1)、导jar包推荐网址1:https://mvnrepository.com/artifact/org.springframework
2)、导jar包推荐网址2:https://search.maven.org/
4、下载好mybatis-generator的jar包(版本可自行选择),本人使用的是1.3.7版本:mybatis-generator-core-1.3.7.jar
下载地址:https://github.com/mybatis/generator/releases
5、准备好生成语句,jar包版本替换为自己下载的版本:
java -jar mybatis-generator-core-1.3.7.jar -configfile generator.xml -overwrite
6、新建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>
<!-- 数据库驱动包位置 -->
<!-- <classPathEntry location="D:\lib\mysql-connector-java-5.1.39.jar" /> -->
<classPathEntry location="D:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.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/database" userId="root" password="root"> -->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="learn20190528" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="cy.model" targetProject="D:\JavaWorkSpace\generators\currency\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 -->
<sqlMapGenerator targetPackage="cy.mapping" targetProject="D:\JavaWorkSpace\generators\currency\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cy.dao" targetProject="D:\JavaWorkSpace\generators\currency\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName-数据表的表明 和 domainObjectName-要生成的类文件名) -->
<table tableName="users" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="menu" domainObjectName="Menu" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="roles" domainObjectName="Roles" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
7、生成generator.xml中配置中所需要的文件
生成的文件如下:
8、Maven加载oracle驱动:ojdbc6.jar
注:使用Oracle数据库,稍微麻烦点,要用到此步骤,MySQL就不需要此步骤!