1. 程式人生 > >(二)基於SSM+Redis+Nginx+FastDFS的部落格網站

(二)基於SSM+Redis+Nginx+FastDFS的部落格網站

上一篇簡單介紹了該網站的所用的框架、技術之類,這篇就開始講搭建過程。本篇為配置檔案。 其中redis和fastdfs的後續用到時再寫。


一、搭建基礎框架SSM之配置檔案

  1. maven工程的pom.xml,基本的SSM框架所需的Jar包,Jedis客戶端包,上傳元件,FastDFS包這個在maven庫裡是沒有的,需要自行下載然後新增到本地倉庫在進行maven更新。若maven更新後,jar包和配置檔案都有,但是啟動報錯,這時就該考慮是否是jar包衝突檔案。jar包衝突問題樓主也不怎麼會解決,如果是本地測試又比較趕時間的話,最簡單粗暴的方法是直接建立一個新的maven倉庫,也就是新建一個目錄,然後在ide的maven指向一下,再重新maven更新即可。
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.tdrip</groupId>
    	<artifactId>T-Drip-portal</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>war</packaging>
    
    	<url>http://maven.apache.org</url>
    
    	<!--Spring start -->
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>4.2.4.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-web</artifactId>
    			<version>4.2.4.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>4.2.4.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>4.2.4.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>4.2.4.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>4.2.4.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>4.2.4.RELEASE</version>
    		</dependency>
    		<!--Spring end -->
    		<!-- test tools. -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.11</version>
    			<scope>test</scope>
    		</dependency>
    		<!--Mybatis -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    			<version>3.2.7</version>
    		</dependency>
    		<dependency>
    			<groupId>tk.mybatis</groupId>
    			<artifactId>mapper</artifactId>
    			<version>3.3.9</version>
    		</dependency>
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    			<version>1.2.2</version>
    		</dependency>
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-ehcache</artifactId>
    			<version>1.0.0</version>
    		</dependency>
    		<!-- 分頁外掛 -->
    		<dependency>
    			<groupId>com.github.pagehelper</groupId>
    			<artifactId>pagehelper</artifactId>
    			<version>4.1.6</version>
    		</dependency>
    		<dependency>
    			<groupId>com.github.jsqlparser</groupId>
    			<artifactId>jsqlparser</artifactId>
    			<version>0.9.5</version>
    		</dependency>
    		<!--aspectj -->
    		<dependency>
    			<groupId>org.aspectj</groupId>
    			<artifactId>aspectjrt</artifactId>
    			<version>1.8.6</version>
    		</dependency>
    		<dependency>
    			<groupId>org.aspectj</groupId>
    			<artifactId>aspectjweaver</artifactId>
    			<version>1.8.7</version>
    		</dependency>
    		<!--MySql -->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.32</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.persistence</groupId>
    			<artifactId>persistence-api</artifactId>
    			<version>1.0.2</version>
    		</dependency>
    		<!--阿里巴巴資料來源 -->
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>druid</artifactId>
    			<version>1.0.8</version>
    		</dependency>
    		<!-- Jedis客戶端 -->
    		<dependency>
    			<groupId>redis.clients</groupId>
    			<artifactId>jedis</artifactId>
    			<version>2.7.2</version>
    		</dependency>
    		<!--Servlet -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.1.0</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>jstl</artifactId>
    			<version>1.2</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.servlet.jsp</groupId>
    			<artifactId>jsp-api</artifactId>
    			<version>2.2</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.taglibs</groupId>
    			<artifactId>taglibs-standard-impl</artifactId>
    			<version>1.2.1</version>
    		</dependency>
    		<!--日誌Log4j -->
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-api</artifactId>
    			<version>1.7.7</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    			<version>1.7.7</version>
    		</dependency>
    
    		<!--JSON -->
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>fastjson</artifactId>
    			<version>1.1.41</version>
    		</dependency>
    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-annotations</artifactId>
    			<version>2.4.5</version>
    		</dependency>
    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-databind</artifactId>
    			<version>2.4.5</version>
    		</dependency>
    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-core</artifactId>
    			<version>2.4.5</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>org.codehaus.jackson</groupId>
    			<artifactId>jackson-mapper-asl</artifactId>
    			<version>1.9.12</version>
    		</dependency>
    		<dependency>
    			<groupId>org.codehaus.jackson</groupId>
    			<artifactId>jackson-core-asl</artifactId>
    			<version>1.9.12</version>
    		</dependency>
    		
    		<!--Commons -->
    		<dependency>
    			<groupId>commons-lang</groupId>
    			<artifactId>commons-lang</artifactId>
    			<version>2.6</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-io</groupId>
    			<artifactId>commons-io</artifactId>
    			<version>2.4</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-net</groupId>
    			<artifactId>commons-net</artifactId>
    			<version>3.5</version>
    		</dependency>
    		
    		<!-- 檔案上傳元件 -->
    		<dependency>
    			<groupId>commons-fileupload</groupId>
    			<artifactId>commons-fileupload</artifactId>
    			<version>1.3.1</version>
    		</dependency>
    
    		<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
    		<dependency>
    			<groupId>dom4j</groupId>
    			<artifactId>dom4j</artifactId>
    			<version>1.6.1</version>
    		</dependency>
    		
    	
    		<!-- Jetty is needed if you're are not using the CXFServlet -->
    		<dependency>
    			<groupId>org.apache.cxf</groupId>
    			<artifactId>cxf-rt-transports-http-jetty</artifactId>
    			<version>3.1.7</version>
    		</dependency>
    		
    		<!-- FastDFS -->
    		<dependency>
    			<groupId>fastdfs_client</groupId>
    			<artifactId>fastdfs_client</artifactId>
    			<version>1.25</version>
    		</dependency>
    	</dependencies>
    
    	<!-- 如果不新增此節點mybatis的mapper.xml檔案都會被漏掉。 -->
    	<build>
    		<resources>
    			<resource>
    				<directory>src/main/resources</directory>
    				<includes>
    					<include>**/*.properties</include>
    					<include>**/*.xml</include>
    				</includes>
    				<filtering>false</filtering>
    			</resource>
    		</resources>
    	</build>
    </project>
     配置applicationContext-*.xml、springmvc.xml和web.xml檔案。在配置spring相關的配置檔案時,要切記spring分為父容器和子容器之分。子容器可以訪問父容器,而父容器不可以訪問子容器。在該系統中,application*.xml檔案都是父容器,而springmvc是子容器,所以controller層可以通過註解得到service層等的bean,而service層不能註解controller的bean
  • applicationContent-dao.xml:配置資料庫
  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	
       xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 掃描資料庫配置檔案 --> <context:property-placeholder location="classpath:resource/*.properties" /> <!-- 配置資料來源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> </bean> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 載入mybatis預設配置檔案 --> <property name="configLocation" value="classpath:mybatis/mybatis.xml"/> <!-- 依賴資料來源 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 載入mybatis mapper介面 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.tdrip.mapper"></property> </bean> </beans>

    applicationContext-service.xml:service層配置檔案,主要用於掃描service
  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    	<!-- 掃描service包 -->
    	<context:component-scan base-package="com.tdrip.service" />
    
    </beans>

    applicationContext-trans.xml:用於配置事務管理。
  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    	<!-- 配置事務管理器 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<!-- 注入資料來源 -->
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    
    
    	<!-- 配置事務增強 -->
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    		<tx:attributes>
    			<!-- 事務的傳播行為,即對哪些方法設定事務 -->
    			<tx:method name="save*" propagation="REQUIRED" />
    			<tx:method name="insert*" propagation="REQUIRED" />
    			<tx:method name="add*" propagation="REQUIRED" />
    			<tx:method name="create*" propagation="REQUIRED" />
    			<tx:method name="delete*" propagation="REQUIRED" />
    			<tx:method name="update*" propagation="REQUIRED" />
    			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
    			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
    			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
    		</tx:attributes>
    	</tx:advice>
    	
    
    	<!-- 切面程式設計 -->
    	<aop:config>
    		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tdrip.service.*.*(..))"/>
    	</aop:config>
    	
    
    </beans>

    springmvc.xml:springmvc配置檔案
  • <?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:p="http://www.springframework.org/schema/p"
    	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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!-- 掃描資料庫配置檔案 -->
    	<context:property-placeholder location="classpath:resource/*.properties" />
    
    	<!-- 開啟註解驅動 -->
    	<mvc:annotation-driven />
    	
    	<!-- 掃描controller -->
    	<context:component-scan base-package="com.tdrip.controller" />
    	
    	<!-- 資源對映 -->
    	<mvc:resources location="/WEB-INF/resource" mapping="/**"/>
    	
    	<!-- 檢視解析器 -->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/" />
    		<property name="suffix" value=".jsp" />
    		<!-- 在controller層返回字串index,表示返回WEB-INF/index.jsp頁面 -->
    	</bean>
    	
    	<!-- 配置檔案上傳 -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
            <property name="maxUploadSize">
                <!-- 上傳檔案大小限制為31M,31*1024*1024 -->
                <value>524288000</value>
            </property>
            <property name="maxInMemorySize">
                <value>4096</value>
            </property>
        </bean>
        
         <!--定義異常處理頁面-->
        <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
                    <prop key="java.lang.Exception">error</prop>
                </props>
            </property>
        </bean>
    </beans>
    
    

    web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	id="TDrip" version="2.5">
    
    	<!-- 載入spring父容器 -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:spring/applicationContext-*.xml</param-value>
    	</context-param>
    
    	<!-- 初始化監聽器 -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    	<!-- 編碼過濾 -->
    	<filter>
    		<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    
    	<!-- 載入spring子容器 -->
    	<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/springmvc.xml</param-value>
    		</init-param>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>SpringMVC</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    
    	<!-- 配置登入驗證 -->
    	<filter>
    		<filter-name>LoginFilter</filter-name>
    		<filter-class>com.tdrip.filter.CheckLoginFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>LoginFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    
    	<!-- 歡迎介面 -->
    	<welcome-file-list>
    		<welcome-file>/index</welcome-file>
    	</welcome-file-list>
    
    
    </web-app>
    mybatis.xml:mybatis預設配置檔案,由於已經整合到spring,所以該配置檔案只配置了分頁外掛,該檔案在dao.xml裡被掃描。
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    		PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    		"http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    	<!-- 配置分頁外掛 -->
    	<plugins>
    		<plugin interceptor="com.github.pagehelper.PageHelper">
    			<!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫 -->
    			<property name="dialect" value="mysql" />
    		</plugin>
    	</plugins>
    </configuration>
    jdbc.properties:資料庫連線屬性配置檔案
    jdbc.url=jdbc:mysql://127.0.0.1:3306/t_drip?characterEncoding=utf-8
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.username=root
    jdbc.password=root
    jdbc.maxActive=10
    jdbc.minIdle=5
    二、測試整合情況,http測試工具,controller->service->mybatis->mysql
  • controller層
    package com.tdrip.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    
    import com.tdrip.model.util.DataResult;
    import com.tdrip.service.ContentService;
    
    @Controller
    public class TestController {
    	
    	@Autowired
    	private ContentService contentService;
    	
    	public DataResult test() throws Exception {
    		return contentService.findAll();
    	}
    }

service層
package com.tdrip.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tdrip.mapper.ContentMapper;
import com.tdrip.model.db.ContentModel;
import com.tdrip.model.util.DataResult;
import com.tdrip.service.ContentService;


@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private ContentMapper contentMapper;
	
	@Override
	public DataResult findAll() throws Exception{

		//查詢資料庫
		List<ContentModel> list = contentMapper.selectAll();
		return DataResult.list(list);
	}
	
}
 mapper.xml檔案
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.tdrip.mapper.ContentMapper">

	<select id="selectAll" resultMap="ContentResult">
		<!-- 稽核狀態暫定全部成功1 -->
		SELECT `id`, `title`, `creater`, `publish`, `operator_id`, `utime`, `ctime`, `pic_url`, `description`
		FROM content
		WHERE status=1 AND publish=1
	</select>
</mapper>
 
PS:本篇總結,主要是配置SSM整合所需的檔案,都是很基礎的東西,沒什麼難點,檔案裡的配置也都有相關的註釋。 下一篇講的是登入模組,登入模組的話涉及到過濾器,在第一篇時,初始進入頁面,如果沒登入的話就會有遮罩層, 這個就是通過過濾器和前端的js程式碼實現的。