SSM个人思路总结
1、整体技术点
后端框架SSM:SpringMVC+Spring+MyBatis
前端框架:BootStrap
分页-PageHelper
2、环境搭建
2.1创建maven工程
通过idea工具,使用骨架的webapp方式创建或者跳过骨架进行创建;其中可能会缺少web.xml文件;下面是xml中一部分:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
</web-app>
2.2、引入依赖的jar包
可以自行去maven官网上去下载以下相关的jar包
Spring相关
SpringMVC相关
MyBatis相关
数据库连接池
数据库驱动
JSTL
servlet-api
Junit 单元测试
Log4j 日志记录
Hibernate Validate 数据校验
Jackson @ResponseBody返回JSON数据
文件上传等
2.3、引用前端内容
css 文件、fonts 文件、js 文件、jQuery 文件
(可以去BootStrap官网上去找,自己练习就随便写个页面)
2.4、添加web文件
思路:项目最先加载的配置文件,所以里面先要加载Spring容器、再添加字符编码过滤器、Rest风格的URI、配置前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 加载Spring容器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 字符编码过滤器,放在所有过滤器最前面 -->
<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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</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>
<!-- 使用Rest风格的URI,将页面普通的POST请求转为指定的delete和put请求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置前端控制器 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.5、Spring配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!--1、配置扫包-->
<context:component-scan base-package="com.yeahsir">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.method.ControllerAdviceBean"/>
</context:component-scan>
<!--2、加载数据库配置文件-->
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
<!--3、加载数据源 mchange.v2.c3p0.ComboPooledDataSource-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name = "filters" value = "${filters}" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="${maxActive}" />
<!-- 初始化连接数量 -->
<property name="initialSize" value="${initialSize}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${maxWait}" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="${minIdle}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="${removeAbandoned}" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="${logAbandoned}" />
</bean>
<!--4、(配置和MyBatis整合)创建sqlSessionFactory,加载mybatis的配置文件,注入数据源-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:../mappers/*.xml"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--5、扫描dao接口所在的包,生成代理实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="xxxx"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!--6、平台事物管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--7、通知-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" isolation="REPEATABLE_READ" rollback-for="Exception"/>
<tx:method name="update*" isolation="REPEATABLE_READ" rollback-for="Exception"/>
<tx:method name="get*" isolation="REPEATABLE_READ" read-only="true"/>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="find*" isolation="REPEATABLE_READ" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--8、aop管理事物-->
<aop:config>
<aop:pointcut expression="execution(* xxx..*.*(..))" id="serviceMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
</beans>
2.6、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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">
<!-- 配置包扫描 -->
<context:component-scan base-package="xx" use-default-filters="false">
<!-- 只扫描Controller -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.method.ControllerAdviceBean"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
<!-- 通过viewName(视图名)拼接起来后如下,来访问页面 -->
<!-- /WEB-INF/pages/<viewName>.jsp -->
</bean>
<!-- 两个标准配置 -->
<!-- 将SpringMVC不能处理的请求交给tomcat -->
<mvc:default-servlet-handler/>
<!-- 只能SpringMVC更高级的功能,比如不需要配置HandlerMapping、HandlerAdapter,支持JSR303数据校验等等 -->
<mvc:annotation-driven />
</beans>
2.7、日志
#分级别记录 debug<info<warn<error<fatal
log4j.rootLogger=debug,stdout,debug,info,warn,error,fatal
#日志输出样式
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-ddHH\:mm\:ss,SSS}[%p]\=\=\=\=%t\:%c.%M(%L)t%m%n
2.8、逆向工程配置
网上有,或者idea里面下载插件
2.9、数据库配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xxx?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
3、注意事项
如果逆向工程生成的xml在java包下面,项目则不会主动加载xml文件,则需要添加配置文件,在pom.xml中标签中添加
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
4、编写Service
简单写个测试例子
注意:由于逆向工程的帮助,所以,mapping、dao、entity这三层都主动写好了,只需要调用;
@Service("testService")
public class testService {
@Autowired
private TestMapper testMapper;
public Test selectByPrimaryKey(Integer deptId) {
return testMapper.selectByPrimaryKey(Id);
}
}
5、控制层Controller
@Controller
@RequestMapping("/dept")
public class DepartmentController {
@Autowired
private TestService tetsService;
@RequestMapping("/getInfoById/{deptId}")
@ResponseBody
public Department getInfoById(@PathVariable("deptId") Integer deptId) {
return tetsService.selectByPrimaryKey(deptId);
}
}
6、写JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>首页</h1>
<a href="dept/getInfoById/1">通过ID获取数据</a>
</body>
</html>
7、Spring集成Junit进行单元测试
下面是测试依赖:Test相关
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
7.1、测试
还有Example的用法
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TestMapper {
@Autowired
private TestMapper testMapper;
/**
* 普通的测试
*/
@Test
public void test01() {
Test test = testMapper.selectByPrimaryKey(1);
System.out.println(test.getDeptName());
}
8、小结

版权声明:本文为qq_41429551原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。