如何搭建一个SSM框架

如何搭建一个空的SSM框架

    • 开始配置
      • web.xml
      • applicationContext.xml
      • applicationContext-mybatis.xml
      • mybatis-config.xml
      • db.properties
      • springmvc.xml
      • 写在最后

这里说的SSM是指 Spring SpringMVC MyBatis
这里写这篇文章的目的是 马上又要开始 一年的毕设季

开始配置

首先建立以下目录 目录结构 可以自行修改,但一定要同时更改所有配置文件中的路径
如何搭建一个SSM框架_第1张图片

web.xml


<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>xssdisplay-name>
	<welcome-file-list>
		<welcome-file>index.htmlwelcome-file>	
	welcome-file-list>

	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:applicationContext.xmlparam-value>
	context-param>
	<listener>
		
		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>

	
	<filter>
		<filter-name>encodingFilterfilter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
		<init-param>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
		<init-param>
			<param-name>forceEncodingparam-name>
			<param-value>trueparam-value>
		init-param>
		
		<init-param>
		  	<param-name>developmentparam-name>
		  	<param-value>trueparam-value>
		init-param>
	filter>
	<filter-mapping>
		<filter-name>encodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>

	
	<servlet>
		<servlet-name>springmvcservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc.xmlparam-value>
		init-param>
		<load-on-startup>1load-on-startup>
	servlet>
	
	<servlet-mapping>
		<servlet-name>springmvcservlet-name>
		<url-pattern>/*url-pattern>
	servlet-mapping>

	<servlet-mapping>
     	<servlet-name >default servlet-name >         
		<url-pattern >*.jsurl-pattern>      
	servlet-mapping >
	<servlet-mapping >
	     <servlet-name >default servlet-name >             
		 <url-pattern >*.cssurl-pattern>        
	servlet-mapping >
web-app>

applicationContext.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.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">

	
	<context:component-scan base-package="com.*.service">context:component-scan>

	
	<import resource="classpath:applicationContext-mybatis.xml" />

	
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath*:db.propertiesvalue>
			list>
		property>
	bean>

	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		
		<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}" />
		
		<property name="maxWait" value="${ds.maxWait}" />
		
		<property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}" />
		
		<property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}" />
		
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />

		
		<property name="proxyFilters">
			<list>
				<ref bean="stat-filter" />
			list>
		property>


	bean>
	
	
	<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>

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

	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource">property>
		
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml">property>
		<property name="typeAliasesPackage" value="com.xss.pojo">property>
		<property name="mapperLocations">
			<list>
				<value>classpath:mybatis/mapper/*.xmlvalue>
			list>
		property>
		
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">
						<value>
							helperDialect=mysql
							offsetAsPageNum=true
							
							reasonable=true
						value>
					property>
				bean>
				<bean class="com.github.abel533.mapperhelper.MapperInterceptor">
					<property name="properties">
						<value>
							
							IDENTITY=MYSQL
							mappers=com.github.abel533.mapper.Mapper
						value>
					property>
				bean>
			array>
		property>
	bean>
	
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="basePackage" value="com.*.mapper">property>
	bean>
beans>

mybatis-config.xml

   

<configuration>

	
	<settings>
		
		<setting name="cacheEnabled" value="true"/>

		
		<setting name="lazyLoadingEnabled" value="true"/>

		
		<setting name="multipleResultSetsEnabled" value="true"/>

		
		<setting name="useColumnLabel" value="true"/>

		
		<setting name="useGeneratedKeys" value="false"/>

		
		<setting name="autoMappingBehavior" value="PARTIAL"/>

		
		<setting name="safeRowBoundsEnabled" value="false"/>

		
		<setting name="mapUnderscoreToCamelCase" value="true"/>

		
		<setting name="localCacheScope" value="SESSION"/>

		
		<setting name="jdbcTypeForNull" value="OTHER"/>

		
		<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>

		
		<setting name="aggressiveLazyLoading" value="false"/>
		<setting name="logImpl" value="STDOUT_LOGGING"/>
	settings>

	<typeAliases>
		
		
		<package name="cn.xss.pojo"/>  
	typeAliases>
	<plugins>
		<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
			
			<property name="IDENTITY" value="MYSQL" />
			
			<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


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

	
	<context:component-scan base-package="com.*.controller">context:component-scan>

	
	<mvc:annotation-driven />

	
	<mvc:default-servlet-handler />

	
	<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-8value>
				<value>application/json;charset=UTF-8value>
			list>
		property>
	bean>

	
	<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>
	bean>
	
	
	<mvc:resources location="/static/" mapping="/static/**">mvc:resources>

beans>

写在最后

想需要源码的可以来找我喔
关注公众号
再喝最后一杯珍珠奶茶
如何搭建一个SSM框架_第2张图片

你可能感兴趣的:(xxxzy,Java,常用,ssm,搭建框架,毕设)