1、创建 web 项目。(利用maven)
2、导入所需 jar 包。
1、spring 相关。(IOC、AOP、jdbc、web等)
2、mybatis 相关。(mybatis-3.5.1.jar)
3、spring-mybatis 整合相关。(mybatis-spring-2.0.1.jar)
4、mysql相关。(c3p0、驱动等)
3、web.xml :
即相关的 servlet 及listener 配置
1、ContextLoaderListener-------------->Spring 容器提供的 web 方面的监听器,用于 web 容器启动时创建 IOC 容器。
2、DispatcherServlet------------------>前端控制器,Spring-mvc 需要的 servlet,拦截所有请求来让 handler 处理。
示例:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!-- 配置 Spring 配置-->
<!-- 配置 spring 配置文件的名称位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
<!--
配置 ServletContextListener:
web 容器初始化时加载 Spring IOC容器
并将其放入 ServletContext 作用域中。
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--***************************************************************************-->
<!-- 配置 Spring MVC 配置-->
<!-- 配置 前端控制器-->
<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:conf/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、applicationContext.xml:
即Spring 相关配置:
1、Spring IOC 容器自动扫描包。------------->不包括控制器(Controller)
2、AOP 注解支持。
3、配置 c3p0 数据源。
4、配置 Spring 的事务管理器,启用事务的注解支持。(@Transactional)
5、Spring 整合 Mybatis:
配置 SqlSessionFactoryBean。
自动扫描 mapper 接口的实现。
示例:
<?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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 配置 bean 自动扫描:不包括控制器-->
<context:component-scan base-package="com.yb">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置 AOP 注解支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--引入外部资源文件-->
<context:property-placeholder location="classpath:conf/db.properties"></context:property-placeholder>
<!--配置 c3p0 数据源-->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="minPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置 Spring 事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--启用事务注解支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--
Spring 整合 mybatis:
目的:1、Spring 管理所有组件,mapper 的实现类
2、Spring 进行事务管理=====>声明式事务
-->
<!--
配置 SqlSessionFactoryBean:
Mybatis所需的工厂。
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation:配置 Mybatis 全局配置文件的位置 -->
<property name="configLocation" value="classpath:conf/mybatis-config.xml"></property>
<!-- mapperLocations:配置 Mybatis sql 映射文件位置 -->
<property name="mapperLocations" value="classpath:conf/*.xml"></property>
</bean>
<!-- 扫描所有 mapper 接口的实现,让这些 mapper 能够自动注入进去 -->
<mybatis-spring:scan base-package="com.yb"></mybatis-spring:scan>
<!--老版本-->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!-- <property name="basePackage" value="com.yb"></property>-->
<!-- </bean>-->
</beans>
5、springmvc-config.xml:
即 Spring-mvc相关配置:
1、Spring IOC 容器自动扫描包。------------->只包括控制器(Controller)
2、配置 Spring-mvc 的视图解析器。
3、配置 Spring-mvc 的注解支持及默认 handler。
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
示例:
<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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
配置自动扫描:只扫瞄 Spring MVC 所需要的控制器
-->
<context:component-scan base-package="com.yb" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</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>
</bean>
<!--两个标准配置-->
<!--将 Spring-MVC 不能处理的请求交给 Tomcat 服务器-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--支持 Spring-MVC 更高级的功能-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
6、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>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启全局二级缓存配置。-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--数据库厂商支持-->
<databaseIdProvider type="DB_VENDOR">
<property name="MySQL" value="mysql"/>
<property name="SQL Server" value="sqlserver"/>
</databaseIdProvider>
</configuration>
7、xxxMapper.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.yb.mapper.CountryMapper">
<select id="getAllCountrys" resultType="com.yb.beans.Country">
select * from country
</select>
</mapper>
8、相应的 handler(Controller)、service(Service)、mapper(Dao)、beans(pojo)省略。
index.jsp:
<html>
<body>
<h2>Hello World!</h2>
<a href="Info">test SSM</a>
</body>
</html>
view.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>id</td>
<td>countryname</td>
<td>countrycode</td>
</tr>
<c:forEach items="${countrys}" var="country">
<tr>
<td>${country.id}</td>
<td>${country.countryname}</td>
<td>${country.countrycode}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Handler1.java:
@Controller
public class Handler1 {
@Autowired
private CountryService countryService;
@RequestMapping("/Info")
public String getCountryInfo(Map<String,Object> map){
List<Country> allCountrys = countryService.getAllCountrys();
map.put("countrys",allCountrys);
return "Message";
}
}
CountryService.java:
@Service("countryService")
public class CountryService {
@Autowired
private CountryMapper countryMapper;
//获取所有集合信息
public List<Country> getAllCountrys(){
List<Country> allCountrys = countryMapper.getAllCountrys();
return allCountrys;
}
}
CountryMapper.java:
@Repository("countryMapper")
public interface CountryMapper {
public List<Country> getAllCountrys();
}
Country.java:
@Component("country")
public class Country implements Serializable {
private int id;
private String countryname;
private String countrycode;
......
}
相应的 get、set 方法省略。