昨天跟一個(gè)小粉絲在聊天的時(shí)候岸浑,他跟我哭訴說最近好難搏存,我以為他是說找工作好難啊,然后就在我準(zhǔn)備各種開導(dǎo)他的時(shí)候矢洲,他跟我說不是璧眠,是我的畢設(shè)啊,我的畢設(shè)和我的實(shí)習(xí)期沖突了兵钮,沒那么多時(shí)間準(zhǔn)備蛆橡,并且他說他們學(xué)校查得也不嚴(yán)格舌界,但是就是要有這么個(gè)破玩意掘譬?
哈哈哈哈,想起我當(dāng)年的畢設(shè)呻拌,一杯咖啡葱轩,一臺(tái)電腦,一份畢設(shè)寫一天藐握,改論文的時(shí)候坐在宿舍走廊改到凌晨5點(diǎn)的時(shí)候靴拱,聽完他的需求,我翻江倒海啊猾普,找到一份當(dāng)年畢設(shè)那會(huì)袜炕,整理的一份純凈的ssm框架
所以可能今天的內(nèi)容對(duì)于剛畢業(yè)或者剛接觸編程的程序員,一套純凈的ssm框架分享給大家初家,每一個(gè)部分都會(huì)有詳細(xì)的注釋解釋偎窘,希望對(duì)大家的學(xué)習(xí)能夠有所幫助
關(guān)注我,大家一起成長和進(jìn)步溜在,好了陌知,看正事
開始配置
首先建立以下目錄 目錄結(jié)構(gòu) 可以自行修改,但一定要同時(shí)更改所有配置文件中的路徑
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>xss</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file> <!-- 初始頁面 -->
</welcome-file-list>
<!-- 加載srping容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!-- ContextLoaderListener監(jiān)聽器的作用就是啟動(dòng)Web容器時(shí)掖肋,自動(dòng)裝配ApplicationContext 的配置信息仆葡。
因?yàn)樗鼘?shí)現(xiàn)了ServletContextListener這個(gè)接口,在web.xml配置這個(gè)監(jiān)聽器志笼,啟動(dòng)容器時(shí)沿盅, 就會(huì)默認(rèn)執(zhí)行它實(shí)現(xiàn)的方法 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Spring字符編碼過濾器 -->
<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>
<!-- 設(shè)置熱部署 -->
<init-param>
? <param-name>development</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>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springmvc的核心文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 映射 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
? ? <servlet-name >default </servlet-name >? ? ? ?
<url-pattern >*.js</url-pattern>? ? ?
</servlet-mapping >
<servlet-mapping >
? ? <servlet-name >default </servlet-name >? ? ? ? ? ?
<url-pattern >*.css</url-pattern>? ? ? ?
</servlet-mapping >
</web-app>
applicationContext.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 配置 @Service注解掃描 -->
<context:component-scan base-package="com.*.service"></context:component-scan>
<!-- 加載其他配置文件 -->
<import resource="classpath:applicationContext-mybatis.xml" />
<!-- 導(dǎo)入屬性配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 允許JVM參數(shù)覆蓋 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<!-- 忽略沒有找到的資源文件 -->
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:db.properties</value>
</list>
</property>
</bean>
<!-- ==================druid連接池 配置================== -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本屬性 url把篓、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小腰涧、最小纸俭、最大 -->
<property name="initialSize" value="${ds.initialSize}" />
<property name="minIdle" value="${ds.minIdle}" />
<property name="maxActive" value="${ds.maxActive}" />
<!-- 配置獲取連接等待超時(shí)的時(shí)間 -->
<property name="maxWait" value="${ds.maxWait}" />
<!-- 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接南窗,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}" />
<!-- 配置一個(gè)連接在池中最小生存的時(shí)間揍很,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}" />
<!-- 打開PSCache,并且指定每個(gè)連接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
<!-- JDBC Proxy Driver -->
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
</list>
</property>
</bean>
<!-- SQL合并配置, 慢SQL記錄 如秘鑰方式配置 -->
<!-- <property name="connectionProperties" value="druid.stat.mergeSql=true,druid.stat.slowSqlMillis=1000;config.decrypt=true;config.decrypt.key=${publicKey}"
/> -->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<property name="slowSqlMillis" value="1000" />
<property name="logSlowSql" value="true" />
<property name="mergeSql" value="true" />
</bean>
<!-- 事務(wù)聲明 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven proxy-target-class="false"
transaction-manager="txManager" />
</beans>
applicationContext-mybatis.xml
<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">
<!-- mapper配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定數(shù)據(jù)庫連接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加載mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="typeAliasesPackage" value="com.xss.pojo"></property><!-- 數(shù)據(jù)庫實(shí)體類 -->
<property name="mapperLocations">
<list>
<value>classpath:mybatis/mapper/*.xml</value>
</list>
</property>
<!-- 分頁插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
offsetAsPageNum=true
<!-- 防止出現(xiàn)小于第一頁万伤,大于最后一頁的異常情況出現(xiàn)窒悔。 -->
reasonable=true
</value>
</property>
</bean>
<bean class="com.github.abel533.mapperhelper.MapperInterceptor">
<property name="properties">
<value>
<!-- 主鍵自增回寫方法,默認(rèn)值MYSQL -->
IDENTITY=MYSQL
mappers=com.github.abel533.mapper.Mapper
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 配置mapper的bean,使用包掃描的方式 批量導(dǎo)入Mapper敌买。這樣mybatis所有的配置都交給spring來管理 掃描后 引用時(shí)可以直接使用類名简珠,注意首字母小寫 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定掃描包的全路徑。如果有多個(gè)虹钮,用英文的逗號(hào)分開 -->
<property name="basePackage" value="com.*.mapper"></property>
</bean>
</beans>
mybatis-config.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>
<!-- 配置mybatis的緩存聋庵,延遲加載等等一系列屬性 -->
<settings>
<!-- 全局映射器啟用緩存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 查詢時(shí),關(guān)閉關(guān)聯(lián)對(duì)象即時(shí)加載以提高性能 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 對(duì)于未知的SQL查詢芙粱,允許返回不同的結(jié)果集以達(dá)到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 允許使用列標(biāo)簽代替列名 -->
<setting name="useColumnLabel" value="true"/>
<!-- 不允許使用自定義的主鍵值(比如由程序生成的UUID 32位編碼作為鍵值)祭玉,數(shù)據(jù)表的PK生成策略將被覆蓋 -->
<setting name="useGeneratedKeys" value="false"/>
<!-- 給予被嵌套的resultMap以字段-屬性的映射支持 FULL,PARTIAL -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- Allows using RowBounds on nested statements -->
<setting name="safeRowBoundsEnabled" value="false"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT
? ? ? ? ? ? local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
<setting name="localCacheScope" value="SESSION"/>
<!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values
? ? ? ? ? ? like NULL, VARCHAR or OTHER. -->
<setting name="jdbcTypeForNull" value="OTHER"/>
<!-- Specifies which Object's methods trigger a lazy load -->
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
<!-- 設(shè)置關(guān)聯(lián)對(duì)象加載的形態(tài),此處為按需加載字段(加載字段由SQL指定)春畔,不會(huì)加載關(guān)聯(lián)表的所有字段脱货,以提高性能 -->
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!-- 注:model表對(duì)象配置,請(qǐng)按照model 包中的順序進(jìn)行錄入,并做好注釋. -->
<!--設(shè)置別名-->
<package name="cn.xss.pojo"/>?
</typeAliases>
<plugins>
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!--主鍵自增回寫方法,默認(rèn)值MYSQL,詳細(xì)說明請(qǐng)看文檔 -->
<property name="IDENTITY" value="MYSQL" />
<!--通用Mapper接口律姨,多個(gè)通用接口用逗號(hào)隔開 -->
<property name="mappers" value="com.github.abel533.mapper.Mapper" />
</plugin>
</plugins>
</configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
#\u672C\u5730\u6570\u636E\u5E93
jdbc.url=jdbc:mysql://127.0.0.1:3306/xss?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
##DataSource Global Setting
#\u914D\u7F6E\u521D\u59CB\u5316\u5927\u5C0F\u3001\u6700\u5C0F\u3001\u6700\u5927
ds.initialSize=10
ds.minIdle=5
ds.maxActive=12
#\u914D\u7F6E\u83B7\u53D6\u8FDE\u63A5\u7B49\u5F85\u8D85\u65F6\u7684\u65F6\u95F4
ds.maxWait=60000
#\u914D\u7F6E\u95F4\u9694\u591A\u4E45\u624D\u8FDB\u884C\u4E00\u6B21\u68C0\u6D4B\uFF0C\u68C0\u6D4B\u9700\u8981\u5173\u95ED\u7684\u7A7A\u95F2\u8FDE\u63A5\uFF0C\u5355\u4F4D\u662F\u6BEB\u79D2
ds.timeBetweenEvictionRunsMillis=60000
# \u914D\u7F6E\u4E00\u4E2A\u8FDE\u63A5\u5728\u6C60\u4E2D\u6700\u5C0F\u751F\u5B58\u7684\u65F6\u95F4\uFF0C\u5355\u4F4D\u662F\u6BEB\u79D2
ds.minEvictableIdleTimeMillis=300000
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
? ? ? ? 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">
<!-- 配置@Controller注解 掃描加載 -->
<context:component-scan base-package="com.*.controller"></context:component-scan>
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Spring框架來處理靜態(tài)文件解決后臺(tái)No mapping found for HTTP request with URI 錯(cuò)誤 -->
<mvc:default-servlet-handler />
<!-- 支持返回json(避免IE在ajax請(qǐng)求時(shí)振峻,返回json出現(xiàn)下載 ) -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- FreeMarker視圖解析器? ? 默認(rèn)視圖 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="requestContextAttribute" value="rc"/>
<property name="cache" value="false"/>
<property name="viewNames" value="*.html" />
<property name="suffix" value=""/>
<property name="order" value="0"/>
</bean>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/page/"/>
<property name="defaultEncoding" value ="UTF-8"></property>
</bean>
<!-- 靜態(tài)資源配置 -->
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
</beans>
寫在最后
其實(shí)個(gè)人感覺程序員這一行除了努力認(rèn)真地學(xué)習(xí)一些知識(shí)點(diǎn)之外,整理記錄也是相當(dāng)重要择份,前期可能沒什么效果扣孟,越到后面重要性體現(xiàn)的越明顯,甚至有時(shí)候會(huì)影響你的工作升遷
關(guān)注我荣赶,大家一起努力成長