1.配置SpringMVC
1.1编写Spring配置文件
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 注解扫描 -->
<context:component-scan base-package="com.jyh.controller" ></context:component-scan>
<!-- 注解映射器和注解适配器可以使用<mvc:annotation-driven/>代替,默认注册了注解映射器和注解适配器等bean。 -->
<mvc:annotation-driven />
<!-- ViewResolver视图解析器 -->
<!-- InternalResourceViewResolver:支持JSP视图解析 viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,
所以classpath中必须包含jstl的相关jar 包; prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为:
前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,
比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp” -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
1.2编写web.xml配置文件
<?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" version="3.1">
<display-name>BlogManage</display-name>
<!-- 加载spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 乱码过滤 -->
<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>
<!-- 前端控制器 -->
<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:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.3编写Controller、jsp & 运行测试
UserController:
/**
* Created by OverrideRe on 2017/7/6.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/getUser")
public String getUser(Model model) throws Exception {
User user = new User();
user.setUserName("OverrideRe");
user.setCity("上海");
model.addAttribute("user", user);
return "userInfo";
}
}
WEB-INF/jsp/userInfo.jsp
<%--
Created by IntelliJ IDEA.
User: OverrideRe
Date: 2017/7/6
Time: 17:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${user.userName}你好
</body>
</html>
运行结果
2.整合SSM
2.1将MyBatis整合进Spring
在applicationContext.xml文件中整合MyBatis配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/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-4.0.xsd">
<!-- 注解扫描 -->
<context:component-scan base-package="com.jyh.controller,com.jyh.service.impl" ></context:component-scan>
<!-- ******************************** 页面、controller相关 ******************************** -->
<!-- 注解映射器和注解适配器可以使用<mvc:annotation-driven/>代替,默认注册了注解映射器和注解适配器等bean。 -->
<mvc:annotation-driven />
<!-- ViewResolver视图解析器 -->
<!-- InternalResourceViewResolver:支持JSP视图解析 viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,
所以classpath中必须包含jstl的相关jar 包; prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为:
前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,
比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp” -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- **************************** Service、Dao相关 **************************** -->
<!-- 被扫描的包要有mapper接口和对应的xml配置文件,扫描出来的mapper自动让spring注册,id为第一个字母小写的类名 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 会话工厂 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 扫描包,多个包用逗号隔开 -->
<property name="basePackage" value="com.jyh.dao" />
</bean>
<!-- ******************************** 数据库相关 ******************************** -->
<!--
引入prperties配置文件
-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- 创建sessionFactory引入dataSource -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource" />
<!-- 配置mybatis 可以不用了
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
-->
<!-- 扫描mapper.xml配置文件 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
</bean>
<!-- 创建事务管理,并注入sessionFactory -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 声明事务 -->
<tx:advice transaction-manager="transactionManager" id="tx">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<!--
除了save开头的方法以外,其他的方法用只读事务
-->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:pointcut
expression="execution(* com.jyh.dao.*.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
</beans>
2.2编写service、controller
service
/**
* Created by OverrideRe on 2017/7/6.
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource(name = "userMapper")
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public UserMapper getUserMapper() {
return userMapper;
}
public User findUserById(String userId){
return userMapper.selectByPrimaryKey(userId);
}
}
controller
/**
* Created by OverrideRe on 2017/7/6.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Resource(name = "userService")
private UserService userService;
@RequestMapping(value = "/getUser")
public String getUser(Model model) throws Exception {
User user = userService.findUserById("111");
model.addAttribute("user", user);
return "userInfo";
}
}
2.3运行结果 & 完整模块
运行结果
完整模块