Bootstrap

Spring4.2.6+SpringMVC+Mybatis+mysql+Gradle使用IDEA搭建

前段时间刚换成IDEA+Gradle开发  自己稍微的记录一下咯

先创建项目

选择自己本地安装的gradle   和maven差不多,算是个本地库吧。然后next输入你的项目名进行创建 最后会生成一个空的项目出来

╮( ̄▽ ̄")╭ 虽然IDEA能够自己生成好多完整的配置 but 本人比较菜 不怎么会弄 还是自己手动写比较方便点。。。

下一步 先不管gradle配置文件  先去创建完整的项目结构,这些文件夹我选择自己手动创建,这时候就只剩下webapp的配置文件了 在Project structrue配置里面

然后把这块的配置改成之前创建的webapp目录 点击完成 一个正常的项目就创建完毕 ╮( ̄▽ ̄")╭虽然看着麻烦 但是能够有效的避免掉其他错误

在IDEA里面 包是不会自动的下载 需要我们手动的build下载 如果找不见build在哪里 请看软件最右侧 应该是有Gradle的选项 点开后有刷新的按钮 正常人都能看到╮( ̄▽ ̄")╭


下面先是build.gradle的配置文件

apply plugin: 'java'
//IntelliJ IDEA 插件
apply plugin: 'idea'
//war包插件
apply plugin: 'war'
//jdk版本
sourceCompatibility = 1.8
version = '1.0'
//下载地址  同样是在maven库里 选择gradle的标签进行复制
repositories {
    maven {
        url "http://www.mvnrepository.com/"
    }
    mavenCentral()
}
//下面的文件可能有些是不需要的  是之前自己项目的配置 但是只多不会少 就直接粘了过来
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile group: 'org.springframework', name: 'spring-core', version: '4.2.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-context-support
    compile group: 'org.springframework', name: 'spring-context-support', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-tx
    compile group: 'org.springframework', name: 'spring-tx', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-aop
    compile group: 'org.springframework', name: 'spring-aop', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-beans
    compile group: 'org.springframework', name: 'spring-beans', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-jdbc
    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-web
    compile group: 'org.springframework', name: 'spring-web', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-webmvc
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-websocket
    compile group: 'org.springframework', name: 'spring-websocket', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-messaging
    compile group: 'org.springframework', name: 'spring-messaging', version: '4.2.6.RELEASE'
    // https://mvnrepository.com/artifact/org.mybatis/mybatis
    compile group: 'org.mybatis', name: 'mybatis', version: '3.2.8'
    // https://mvnrepository.com/artifact/org.mybatis/mybatis-spring
    compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.2.2'
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'
    // https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp
    compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
    // https://mvnrepository.com/artifact/commons-pool/commons-pool
    compile group: 'commons-pool', name: 'commons-pool', version: '1.6'
    // https://mvnrepository.com/artifact/log4j/log4j
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
    // https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
    // https://mvnrepository.com/artifact/org.freemarker/freemarker
    compile group: 'org.freemarker', name: 'freemarker', version: '2.3.20'
    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.1'
    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.1'
    // https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws
    compile group: 'org.apache.cxf', name: 'cxf-rt-frontend-jaxws', version: '3.1.4'
    // https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore
    compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.2'
    // https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore-nio
    compile group: 'org.apache.httpcomponents', name: 'httpcore-nio', version: '4.3.2'
    // https://mvnrepository.com/artifact/wsdl4j/wsdl4j
    compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'
    // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.12'
    // https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-bindings-soap
    compile group: 'org.apache.cxf', name: 'cxf-rt-bindings-soap', version: '3.1.4'
    // https://mvnrepository.com/artifact/org.apache.cxf/cxf-core
    compile group: 'org.apache.cxf', name: 'cxf-core', version: '3.1.4'
    // https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http
    compile group: 'org.apache.cxf', name: 'cxf-rt-transports-http', version: '3.1.4'
    // https://mvnrepository.com/artifact/org.json/json
    compile group: 'org.json', name: 'json', version: '20160810'
    // https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
    compile group: 'org.elasticsearch', name: 'elasticsearch', version: '2.4.0'
    // https://mvnrepository.com/artifact/redis.clients/jedis
    compile group: 'redis.clients', name: 'jedis', version: '2.9.0'/*
// https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '3.1.2'*/
// https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl
    compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.12'
// https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl
    compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.12'
// https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.8'
    // https://mvnrepository.com/artifact/org.springframework/spring-oxm
    compile group: 'org.springframework', name: 'spring-oxm', version: '4.2.6.RELEASE'
// https://mvnrepository.com/artifact/aopalliance/aopalliance
    compile group: 'aopalliance', name: 'aopalliance', version: '1.0'

}
然后再resources文件夹下创建spring的xml applicationContext.xml、spring-mvc.xml,貌似IDEA的项目配置文件必须创建在这里 也就不作死的改地方了

这里是web.xml的配置文件

<?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">
    <!-- 过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

    <!-- SpringMVC配置 -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
    <filter>
        <filter-name>httpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <servlet-name>SpringMVC</servlet-name>
    </filter-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--<error-page>
        <error-code>500</error-code>
        <location>/error/500.do</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/error/404.do</location>
    </error-page>-->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.html</url-pattern>
        <!--<url-pattern>*.jsp</url-pattern>-->
        <url-pattern>*.gif</url-pattern>
        <url-pattern>*.jpg</url-pattern>
        <url-pattern>*.png</url-pattern>
        <url-pattern>*.eot</url-pattern>
        <url-pattern>*.svg</url-pattern>
        <url-pattern>*.ttf</url-pattern>
        <url-pattern>*.woff</url-pattern>
        <url-pattern>*.less</url-pattern>
        <url-pattern>*.scss</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.html</welcome-file>
        <!--<welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>-->
    </welcome-file-list>
</web-app>

这里是applicationContext.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:websocket="http://www.springframework.org/schema/websocket"
       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-3.0.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
           http://www.springframework.org/schema/websocket
           http://www.springframework.org/schema/websocket/spring-websocket.xsd
           ">

    <!-- 引入注解项 -->
    <context:component-scan base-package="com.hanmi"></context:component-scan>

    <!-- 数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="自己的地址"></property>
        <property name="username" value="帐号"></property>
        <property name="password" value="密码"></property>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="1000"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="2000"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="300"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="100"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000"></property>
    </bean>
    <!-- 配置线程池 -->
    <bean id ="taskExecutor"  class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
        <!-- 线程池维护线程的最少数量 -->
        <property name ="corePoolSize" value ="5" />
        <!-- 线程池维护线程所允许的空闲时间 -->
        <property name ="keepAliveSeconds" value ="30000" />
        <!-- 线程池维护线程的最大数量 -->
        <property name ="maxPoolSize" value ="1000" />
        <!-- 线程池所使用的缓冲队列 -->
        <property name ="queueCapacity" value ="200" />
    </bean>
    <!-- 引入数据源 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:com.hanmi.mapper/*.xml"></property>
    </bean>

    <!-- 扫描指定包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.demo.dao"></property>
    </bean>


    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
        <!-- enable transaction annotation support -->
    <tx:annotation-driven transaction-manager="txManager"/>

    <!-- 异常处理见Spring例子 -->
    <!-- Mapping exception to the handler view -->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->
        <property name="defaultErrorView" value="error"></property>
        <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
        <property name="exceptionAttribute" value="ex"></property>
        <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 -->
        <property name="exceptionMappings">
            <props>
                <prop key="IOException">error/ioexp</prop>
                <prop key="java.sql.SQLException">error/sqlexp</prop>
            </props>
        </property>
    </bean>
</beans>
接下来是spring-mvc.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	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-3.0.xsd
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           ">

<context:component-scan base-package="com.hanmi" />


    <mvc:annotation-driven />

    <bean id="viewResolverCommon" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value=""/>
        <property name="suffix" value=""/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.InternalResourceView</value>
        </property>
        <property name="order" value="1"/>
    </bean>


    <!-- 配置内容协商视图解析 -->
    <bean id="contentNegotiatingViewResolver"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
       <!--mj tl gb em xy xx ts  <!– 设置内容协商管理器 –>
        <property name="contentNegotiationManager" ref="contentNegotiationManager"></property>
        <!– 设置默认视图 –>
        <property name="defaultViews">
            <list>
                <ref bean="mappingJacksonJsonView" />
                <ref bean="marshallingView" />
            </list>
        </property>
        <!– 设置视图解析器 –>
        <property name="viewResolvers">
            <list>
                <ref bean="defalutViewResolver" />
            </list>
        </property>-->
        <property name="suffix" value=".ftl" />
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="viewClass" value="com.freemarker.util.FreeMarkerViewUtil" />
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="order" value="0"/>
    </bean>

    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/views/" />
        <property name="freemarkerVariables">
            <map>
                <entry key="x,l_escape" value-ref="fmXmlEscape" />
            </map>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">1</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="number_format">0.##########</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="classic_compatible">true</prop>
                <prop key="template_exception_handler">ignore</prop>
            </props>
        </property>
    </bean>
    <!--<bean id="defalutViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=""></property>
    </bean>-->

    <!-- JSON视图 -->
    <bean id="mappingJacksonJsonView"
              class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
        <!--去除key 只返回value-->
        <property name="extractValueFromSingleKeyModel" value="true" />
    </bean>

    <!-- XML视图 -->
    <bean id="marshallingView"
          class="org.springframework.web.servlet.view.xml.MarshallingView">
        <property name="marshaller">
            <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
            </bean>
        </property>
    </bean>

    <bean id="contentNegotiationManager"
          class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="mediaTypes">
            <props>
                <prop key="json">application/json;charset=utf-8</prop>
                <prop key="xml">application/xml;charset=utf-8</prop>
                <prop key="do">application/html;charset=utf-8</prop>
            </props>
        </property>
        <property name="ignoreAcceptHeader" value="true"></property>
        <!-- 设置默认的 ContentType为application/json-->
        <property name="defaultContentType" value="application/json"></property>
    </bean>
</beans>
一共三个格式的后缀名 .do返回视图 .json返回json格式的数据 .xml返回xml格式的数据  视图用到了freemarker 不需要的可以去掉 用下面注释的标签

在Controller返回视图的时候 需要用ModelAndView进行返回 格式类似下面

    @RequestMapping(value = "test")
    public ModelAndView xxx(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        Map<Object, Object> map = new HashMap<Object,Object>();
        List list = new ArrayList<Object>();
        map.put("id" ,request.getParameter("id"));
        map.put("name","暖心大人");
        list.add("暖心大人");
        mav.addObject(list);

        mav.setViewName("/WEB-INF/view/user1.html");
        System.out.print(list);
        return mav;
    }
这时候 三种后缀名都可以返回对应的视图和数据

Mybatis的mapper也没什么特殊的地方了 自行解决掉。。。。。如果没有什么问题出现 项目的大体框架就已经配置完成了

;