idea 创建java web项目ssm-gradle

环境准备:jdk1.8+tomcat8+idea+gradle

1.创建项目SSM

使用gradle创建项目,按照提示如下


image.png

输入项目名称,组名

image.png
image.png

选择本地安装gradle

image.png

点击下一步创建成功

image.png

如图所示,项目结构不完整,下面完善项目结构
手动创建文件夹java和resources,WEB-INF

image.png

创建web.xml

image.png

创建成功

image.png

配置tomcat

image.png
image.png
image.png

在web.xml中添加默认访问页面

image.png

启动项目,访问localhost:8080如下,项目创建成功

image.png

2.搭建ssm

配置文件build.gradle
更改jdk版本,使用阿里的maven仓库,导入依赖,在此附上spring各个依赖包的作用,方便大家理解。(有时我也很懵逼)http://blog.csdn.net/sunchen2012/article/details/53939253
http://blog.csdn.net/weisong530624687/article/details/50888094

group 'com.lmandy'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
//    mavenCentral()
    maven{
        url "http://maven.aliyun.com/nexus/content/repositories/central/"
    }
}

dependencies {
//junit 单元测试
    compile group: 'junit', name: 'junit', version: '4.12'
//sping
    compile group: 'org.springframework', name: 'spring-core', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-expression', version: '4.3.6.RELEASE'

    compile group: 'org.springframework', name: 'spring-context-support', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.6.RELEASE'

    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-aop', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-tx', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-test', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-aspects', version: '4.3.6.RELEASE'

//  spring依赖日志包
    compile group: 'commons-logging', name: 'commons-logging', version: '1.2'

//  string 增强
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
//  集合增强
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
//上传组件
    compile group: 'commons-io', name: 'commons-io', version: '2.4'
    compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.1'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.10'


//mybatis及依赖包
    compile group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
//spring 整合 mybatis
    compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.0'
//mysql数据库连接驱动
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
//dbcp 连接池配置数据库
    compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
//jsp相关
    compile group: 'jstl', name: 'jstl', version: '1.2'
//JavaEE servlet
    compile group: 'javax', name: 'javaee-api', version: '7.0'

//日志
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.24'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.24'
//gson
    compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
//fastjson
    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.33'
//shiro
    compile group: 'org.apache.shiro', name: 'shiro-core', version: '1.3.2'
    compile group: 'org.apache.shiro', name: 'shiro-web', version: '1.3.2'
    compile group: 'org.apache.shiro', name: 'shiro-spring', version: '1.3.2'
    compile group: 'org.apache.shiro', name: 'shiro-ehcache', version: '1.3.2'

}

spring-mvc.xml

<?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">

    <!--扫描包,自动注入bean-->
    <context:component-scan base-package="com.lmandy"/>
    <!--使用注解开发spring mvc-->
   <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--上传下载-->
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

    <!--登录拦截器  这里注意一下,如果使用这段代码请配置好登录拦截器,不要只是新建一个LogInterceptor类,要把内容写全,要么注释掉下段代码-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/bootstrap/**/*"/>
            <mvc:exclude-mapping path="/css/**/*"/>
            <mvc:exclude-mapping path="/js/**/*"/>
            <mvc:exclude-mapping path="/img/**/*"/>
            <bean class="com.lmandy.interceptor.LogInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

    <!--过滤掉静态资源文件-->
    <mvc:resources mapping="/bootstrap/**/*" location="/resources/bootstrap/"/>
    <mvc:resources mapping="/css/**/*" location="/resources/css/"/>
    <mvc:resources mapping="/js/**/*" location="/resources/js/"/>
    <mvc:resources mapping="/img/**/*" location="/resources/img/"/>
</beans>

applicationContext.xml

<?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: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.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.lmandy"/>
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>
    <!--sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/lmandy/dao/**/*Mapper.xml"/>

    </bean>
    <!--mapper 扫描器,配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lmandy.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--声明式事物管理器-->
    <!-- 配置spring的PlatformTransactionManager,名字为默认值 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置基于注解的声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <!-- 使用监听器加载applicationContext文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 解决post乱码问题 -->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

<!--前段控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--默认访问页面-->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

jdbc.properties(数据库配置文件)

#jdbc.driver=com.mysql.jdbc.Driver
jdbc.driver=com.mysql.cj.jdbc.Driver
#jdbc.url=jdbc:mysql:///account?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.url=jdbc:mysql:///account?serverTimezone=UTC
jdbc.username=root
jdbc.password=123

log4j.properties(spring整合log4j的日志文件,依赖已导入)

log4j.rootLogger=DEBUG , stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

最终项目结构如下,相关包大家自己修改,创建。要和配置文件里对应

image.png

至此整个项目搭建完成,下面进行简单的测试。在WEB-INF/views下新建testIndex.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试</title>
</head>
<body>
    这是一个测试页面
</body>
</html>

创建TestController

package com.lmandy;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/test")
@Controller
public class TestController {

    @RequestMapping("index")
    public String testIndex(){

        return "testIndex"; 
        //对应刚刚创建的jsp文件名,这里用的是逻辑视图
    }
}

逻辑视图解析器已在spring-mvc.xml配置

 <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

下面启动项目开始访问 http://localhost:8080/test/index如下访问成功

image.png

至此可以进行开发了

在此附上demo的地址 https://gitee.com/lmandy/text_gradle.git

创建数据库:account
创建:test_user表 此处注意要和jdbc中对应

CREATE TABLE `test_user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(64) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

推荐阅读更多精彩内容