目录
1. Maven 项目搭建
2. Maven 插件生成 MyBatis 代码
3. 待续 ...
开发环境
开发环境请尽量保持一致,不一致的情况可能存在问题。
JDK 1.7
MyEclipse 8.5
Maven 3.0.4
Spring 4.2.5.RELEASE
MyBaties 3.3.1
Maven 项目搭建
现在代码管理一般都是采用Maven管理,所以现在构建项目的不二选择就是用它,百度一下介绍文档很多,环境配置这里不做介绍,下面给出建立web项目相关步骤。
1. 新建一个Maven Project,然后 Next,Catalog 项选择 All Catalogs, Filter 里面填上 web ,选择 maven-archetype-webapp, Next ,
填上相关信息,项目名(不能包含空格)和包名自定义,直接 Finish,这样一个原始的 Maven Web 项目就建立好了。
但是插件生成的项目存在问题,需要手动修改配置和添加目录。
下面是修改步骤:
1. 在项目上右键菜单 properties,上面搜索框输入 facets,修改相关配置。2. 找到 Java Build Path 选项,添加如下目录 /src/main/java /src/test/java /src/test/resources , 修改每个目录的Output folder,总共是4个目录,因为添加的时候是按层级添加,如果添加完成后存在一个单独的 test 目录,直接 Remove 掉即可。这样新的项目结构就OK了。
Maven 插件生成 MyBatis 代码
项目构建完成后,就可以进行 pom.xml 文件的修改来添加 jar 包。下面是整个环境需要的依赖,将自动生成的 dependencies 替换成下面内容。
<properties> <spring.version>4.2.5.RELEASEspring.version> <mybatis.version>3.3.1mybatis.version> <slf4j.version>1.7.7slf4j.version> <log4j.version>1.2.17log4j.version> properties> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-context-supportartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aopartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-oxmartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>1.5.4version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>4.1.4.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>4.1.4.RELEASEversion> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${mybatis.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>1.2.4version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>3.0.1version> dependency> <dependency> <groupId>javaxgroupId> <artifactId>javaee-apiartifactId> <version>7.0version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>1.1.41version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-log4j12artifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>${log4j.version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.38version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.0.17version> dependency> <dependency> <groupId>org.codehaus.jacksongroupId> <artifactId>jackson-mapper-aslartifactId> <version>1.9.13version> dependency> <dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>1.3.1version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.4version> dependency> <dependency> <groupId>commons-codecgroupId> <artifactId>commons-codecartifactId> <version>1.9version> dependency> dependencies>
添加 Maven 插件,Mabatis 的 generator 工具插件来自动生成代码。在 build 节点下添加。
<build> <pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generatorgroupId> <artifactId>mybatis-generator-maven-pluginartifactId> <version>1.3.2version> <configuration> <verbose>trueverbose> <overwrite>falseoverwrite> configuration> plugin> plugins> pluginManagement> <finalName>MyBatisTestfinalName> build>
在 /src/resources 目录下添加MyBatis的自动生成配置文件 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/Krainy/.m2/repository/mysql/mysql-connector-java/5.1.17/mysql-connector-java-5.1.17.jar" /> <context id="Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false"/> <property name="suppressAllComments" value="true" /> commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/shop" userId="root" password="root"> jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> javaTypeResolver> <javaModelGenerator targetPackage="com.lna.model" targetProject="D:/Workspaces/System/MyBatis Maven Webapp/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> javaModelGenerator> <sqlMapGenerator targetPackage="com.lna.mapping" targetProject="D:/Workspaces/System/MyBatis Maven Webapp/src/main/java"> <property name="enableSubPackages" value="true" /> sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lna.dao" targetProject="D:/Workspaces/System/MyBatis Maven Webapp/src/main/java"> <property name="enableSubPackages" value="true" /> javaClientGenerator> <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>
如果发现 generatorConfig.xml 文件报错可不必理会, 修改完配置信息后, 选择 pom.xml 文件,右键选择 Run AS –> Maven Build … –> 在Goals框中输入: mybatis-generator:generate, 点击 Run
就全部自动生成了.是不是很好很强大,再也不用开个CMD再那里敲命令了 ...