Gradle是一個基於Apache Ant和Apache Maven概念的項目自動化建構工具。 它使用一種基於Groovy的特定領域語言(DSL)來聲明項目設置,拋棄了基於XML的各類繁瑣配置。css
Spring MVC屬於SpringFrameWork的後續產品,已經融合 在Spring Web Flow裏面。Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。html
MyBatis本是apache的一個開源項目iBatis, 2010年這個項目 由apache software foundation 遷移到了google code,而且更名爲MyBatis 。 2013年11月遷移到Github。java
1.安裝mysql,並完成相應數據庫的創建mysql
2.安裝IDEA完整版web
3.下載tomcat或其餘服務器spring
打開IDEA軟件,選擇Create New Project:sql
選擇建立基於gradle的java web項目:數據庫
輸入GroupId和ArtifactId:apache
選擇gradle的來源,能夠用本身下載的gradle,也可使用IDEA內建的gradle版本:瀏覽器
選擇項目的目錄:
點擊完成,會生成一個最基本的java web目錄,增長文件夾和相應文件,將其目錄結構改成如下結構:
其中resources目錄中的config文件夾中的文件和WEB-INF文件夾下的web.xml, 以及build.gradle都是本身進行配置的。
<?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="${jarDirection}"/> <context id="mysql" targetRuntime="MyBatis3"> <jdbcConnection driverClass="${driverClass}" connectionURL="${connectionURL}" userId="${userId}" password="${password}"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_java}"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="${mapperPackage}" targetProject="${src_main_java}"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 每個數據庫表都須要創建一個對應的table字段 --> <table schema="test" tableName="user" domainObjectName="User" > <!-- <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
# 生成的model類所在包 modelPackage=cn.edu.bupt.wen.model # 生成的mapper接口類所在包 mapperPackage=cn.edu.bupt.wen.mapper # 生成的mapper xml文件所在包 sqlMapperPackage=cn.edu.bupt.wen.mybatis_mapper
# 將jdbc.jarDirection後的內容更改成gradle下載的 # mysql-connector-java-5.1.x.jar所在的路徑,gradle自動下載的路徑以下所示 jdbc.jarDirection=/Users/wenzhiquan/.gradle/caches/modules-2/files-2.1/mysql /mysql-connector-java/5.1.38/dbbd7cd309ce167ec8367de4e41c63c2c8593cc5 /mysql-connector-java-5.1.38.jar jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bookbest jdbc.user=root jdbc.pass=root
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 自動掃描控制器 --> <context:component-scan base-package="cn.edu.bupt.wen"/> <!-- 視圖渲染 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 控制器映射器和控制器適配器 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 靜態資源映射器 --> <mvc:resources mapping="/statics/**" location="/WEB-INF/statics/" /> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>index-dispather</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring/spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>index-dispather</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
group 'cn.edu.bupt.wen' version '1.0-SNAPSHOT' apply plugin: 'groovy' apply plugin: 'java' apply plugin: 'war' apply plugin: 'idea' sourceCompatibility = 1.5 repositories { mavenCentral() } configurations { mybatisGenerator } // 讀取config文件夾中對應的配置文件 ext{ def prop = new Properties(); file("src/main/resources/config/mybatis/jdbc-mysql.properties") .withInputStream { prop.load(it) } file("src/main/resources/config/mybatis/gradle.properties") .withInputStream { prop.load(it) } prop.each { project.extensions.add("$it.key",it.value); } } // 配置gradle的依賴項 dependencies { compile 'org.codehaus.groovy:groovy-all:2.3.11' compile 'jstl:jstl:1.2' compile 'org.springframework:spring-aop:4.2.4.RELEASE' compile 'org.springframework:spring-context:4.2.4.RELEASE' compile 'org.springframework:spring-beans:4.2.4.RELEASE' compile 'org.springframework:spring-web:4.2.4.RELEASE' compile 'org.springframework:spring-webmvc:4.2.4.RELEASE' compile 'org.springframework:spring-tx:4.2.4.RELEASE' compile 'org.springframework:spring-jdbc:4.2.4.RELEASE' compile 'org.springframework:spring-test:4.2.4.RELEASE' compile 'mysql:mysql-connector-java:5.1.38' compile 'org.mybatis.generator:mybatis-generator-core:1.3.2' compile 'org.mybatis:mybatis-spring:1.2.3' compile 'org.mybatis:mybatis:3.3.0' compile 'junit:junit:4.12' mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2' mybatisGenerator 'mysql:mysql-connector-java:5.1.38' testCompile group: 'junit', name: 'junit', version: '4.11' } // 利用ant建立一個自動生成MyBatis目錄文件的task task mybatisGenerate << { ant.properties['targetProject'] = projectDir.path ant.properties['jarDirection'] = project['jdbc.jarDirection'] ant.properties['driverClass'] = project['jdbc.driverClassName'] ant.properties['connectionURL'] = project['jdbc.url'] ant.properties['userId'] = project['jdbc.user'] ant.properties['password'] = project['jdbc.pass'] ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path ant.properties['modelPackage'] = project['modelPackage'] ant.properties['mapperPackage'] = project['mapperPackage'] ant.properties['sqlMapperPackage'] = project['sqlMapperPackage'] ant.taskdef( name: 'mbgenerator', classname: 'org.mybatis.generator.ant.GeneratorAntTask', classpath: configurations.mybatisGenerator.asPath ) ant.mbgenerator(overwrite: true, configfile: 'src/main/resources/config/mybatis/generatorConfig.xml', verbose: true) { propertyset { propertyref(name: 'targetProject') propertyref(name: 'jarDirection') propertyref(name: 'userId') propertyref(name: 'driverClass') propertyref(name: 'connectionURL') propertyref(name: 'password') propertyref(name: 'src_main_java') propertyref(name: 'src_main_resources') propertyref(name: 'modelPackage') propertyref(name: 'mapperPackage') propertyref(name: 'sqlMapperPackage') } } }
至此,基本的配置就完成了,接下來就須要運行gradle下載相應的依賴包了, 操做以下:打開IDEA > View > Tool Windows > gradle
:
點擊圖中的按鈕,IDEA就會自動下載依賴包了。
下載完成後,目錄下的External Libraries
文件夾會生成相應的目錄:
下載完依賴包以後,總體的配置基本上就完成了。
打開IDEA > Run > Edit Configurations...
點擊tomcat server的local選項,點擊右上角的configure...
, 在彈出的對話框中選中下載的Tomcat根目錄:
而後將配置好的tomcat添加到運行庫當中:
在controller文件夾中建立IndexController
文件,將代碼修改成:
package cn.edu.bupt.wen.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Author wenzhiquan * Created on 16/4/12 下午12:25 * File name: IndexController.java * Description: null */ @Controller public class IndexController { @RequestMapping("/") public String index(){ return "index"; } }
將index.jsp文件修改成本身想要的樣式:
<%-- Created by IntelliJ IDEA. User: wenzhiquan Date: 16/4/12 Time: 下午12:23 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Index</title> </head> <body> Hello World! </body> </html>
點擊運行按鈕,服務器啓動後會自動啓動瀏覽器進入主界面:
至此,hello world程序已經完成。
進入gradle
的view,點擊建立gradle
任務:
建立一個mybatisGenerate任務:
點擊OK,會在model
,mapper
和mybatis_mapper
文件夾中自動生成相應的文件:
至此,Spring MVC + MyBatis的Java Web開發環境配置完成。