環(huán)境準(zhǔn)備:jdk1.8+tomcat8+idea+gradle
1.創(chuàng)建項目SSM
使用gradle創(chuàng)建項目桐愉,按照提示如下
輸入項目名稱窒升,組名
選擇本地安裝gradle
點擊下一步創(chuàng)建成功
如圖所示,項目結(jié)構(gòu)不完整亚亲,下面完善項目結(jié)構(gòu)
手動創(chuàng)建文件夾java和resources,WEB-INF
創(chuàng)建web.xml
創(chuàng)建成功
配置tomcat
在web.xml中添加默認(rèn)訪問頁面
啟動項目,訪問localhost:8080如下响谓,項目創(chuàng)建成功
2.搭建ssm
配置文件build.gradle
更改jdk版本损合,使用阿里的maven倉庫,導(dǎo)入依賴娘纷,在此附上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數(shù)據(jù)庫連接驅(qū)動
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
//dbcp 連接池配置數(shù)據(jù)庫
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
//jsp相關(guān)
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"/>
<!--使用注解開發(fā)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類,要把內(nèi)容寫全遏插,要么注釋掉下段代碼-->
<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>
<!--過濾掉靜態(tài)資源文件-->
<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"/>
<!--數(shù)據(jù)源-->
<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接口包,動態(tài)實現(xiàn)Dao接口胳嘲,注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lmandy.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--聲明式事物管理器-->
<!-- 配置spring的PlatformTransactionManager厂僧,名字為默認(rèn)值 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置基于注解的聲明式事務(wù) -->
<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>
<!-- 使用監(jiān)聽器加載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>
<!--默認(rèn)訪問頁面-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
jdbc.properties(數(shù)據(jù)庫配置文件)
#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的日志文件,依賴已導(dǎo)入)
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
最終項目結(jié)構(gòu)如下胎围,相關(guān)包大家自己修改吁系,創(chuàng)建。要和配置文件里對應(yīng)
至此整個項目搭建完成白魂,下面進行簡單的測試汽纤。在WEB-INF/views下新建testIndex.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>測試</title>
</head>
<body>
這是一個測試頁面
</body>
</html>
創(chuàng)建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";
//對應(yīng)剛剛創(chuàng)建的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如下訪問成功
至此可以進行開發(fā)了
在此附上demo的地址 https://gitee.com/lmandy/text_gradle.git
創(chuàng)建數(shù)據(jù)庫:account
創(chuàng)建:test_user表 此處注意要和jdbc中對應(yīng)
CREATE TABLE `test_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;