步骤1导入相关jar
1.首先是Spring必须基础jar
commons-logging-1.1.3.jar 这个包不属于Spring框架。
spring-beans-4.3.10.RELEASE.jar
spring-context-4.3.10.RELEASE.jar
spring-core-4.3.10.RELEASE.jar
spring-expression-4.3.10.RELEASE.jar
2. aop的jar包
aopalliance-1.0.0.jar
aspectjweaver-1.6.8.jar
spring-aop-4.3.10.RELEASE.jar
spring-aspects-4.3.10.RELEASE.jar
3.dbcp数据库连接池jar
commons-dbcp-1.4.jar
commons-logging-1.1.1.jar
commons-pool-1.5.3.jar
4.spring 的事物管理及操作jar
spring-jdbc-4.3.10.RELEASE.jar
spring-tx-4.3.10.RELEASE.jar
5.SpringMVC需要jar(注意:与Spring基础包重复)
commons-logging-1.1.1.jar
spring-aop-4.3.10.RELEASE.jar
spring-context-4.3.10.RELEASE.jar
spring-context-support-4.3.10.RELEASE.jar
spring-core-4.3.10.RELEASE.jar
spring-expression-4.3.10.RELEASE.jar
spring-web-4.3.10.RELEASE.jar
spring-webmvc-4.3.10.RELEASE.jar
spring-webmvc-portlet-4.3.10.RELEASE.jar
6.mybatis整合到Spring需要jar
mybatis-3.2.8.jar
mybatis-spring-1.2.0.jar
7.jstl必须的两个jar
jstl-1.2.jar
standard-1.1.2.jar
参考图
步骤2添加配置文件
1.添加Spring配置文件xml,文件名建议用applicationcontext.xml
2.添加SpringMVC的配置文件,建议命名为xxxx-servlet.xml。(整合之后一般只放别名的配置,也可以不放。因为在Spring配置文件中可做)
3.添加Mybaties的配置文件
一般叫mybatis-config.xml
4.连接数据库的properties文件
举个列子
applicationcontext.xml文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- default-lazy-init="true "-->
<!-- 启动spring注解配置 -->
<context:annotation-config/>
<!-- 启动对AOP的支持 -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> -->
<!-- 扫描包,对包中的对象创建bean对象,前提类型要加特定注解 -->
<context:component-scan base-package="com.icss.service"/>
<!-- 配置加载属性文件bean -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/jdbc.properties"></property>
</bean>
<!-- 连接数据库的连接池 -->
<bean id="dataSource" 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}"/>
<!-- 连接池配置 初始化连接数,最大连接数,最大空闲数,最小空闲数,最大空闲时间-->
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxWait" value="${maxWait}"/>
</bean>
<!-- Mybatis 的配置,主配置,别名,映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.icss.entity"/>
<property name="mapperLocations" value="classpath:com/icss/mapping/*.xml"/>
</bean>
<!-- 扫描所有的接口,生成代理对象 通过sqlSessionFactory 工厂对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.icss.dao"></property>
</bean>
</beans>
properties文件
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/message
username=root
password=root
initialSize=10
maxActive=100
maxIdle=30
minIdle=10
maxWait=20000
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>
<!-- 根节点必须要留 -->
</configuration>
SpringMVC-servlet.xml文件
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
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">
<!-- 启用MVC的注解配置 -->
<mvc:annotation-driven />
<!-- 扫描项目中的Bean对象到SpringMVC的IOC子容器中 -->
<context:component-scan base-package="com.icss.controller" />
<!-- 静态资源访问路径的配置 -->
<!-- <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources
location="/css/" mapping="/css/**"/> <mvc:resources location="/js/" mapping="/js/**"/> -->
<!-- 也可以解决静态资源的问题 -->
<mvc:default-servlet-handler />
<!-- 视图解析器,作用是根据控制返回的视图名称解析出具体的视图文件 假如控制器返回的视图名称是 : graeList,根据这个名称解析出完整的视图文件是:
/graeList.jsp -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
最后是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" id="WebApp_ID" version="3.0">
<display-name>SSM0723</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 启动Spring框架,配置一个监听器 ContextLoaderListener,此监听器加载时需要Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationcontext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SpringMVC 的前端控制器,这是一个Servlet -->
<servlet>
<!-- 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:config/SpringMVC-servlet.xml</param-value>
</init-param>
<!-- 1:表示设置Servlet在应用程序启动时启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!-- 设置请求的URL, /表示拦截所有请求 错误的写法是:/* 。 (*.do *.html)这种配置不推荐,不符合目前的URL风格(REST) -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- SpringMVC 解决中文乱码的问题 -->
<filter>
<filter-name>charFilter</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>
</filter>
<filter-mapping>
<filter-name>charFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
步骤3创建项目包
下面简单的代码测试
首先entity层
public class Teacher {
private int id;
private String tname;
private String tsex;
private int tage;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getTsex() {
return tsex;
}
public void setTsex(String tsex) {
this.tsex = tsex;
}
public int getTage() {
return tage;
}
public void setTage(int tage) {
this.tage = tage;
}
}
然后mapping里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.icss.dao.TeacherMapper">
<select id="find" resultType="Teacher">
SELECT * from teacher
</select>
</mapper>
接着dao层接口
package com.icss.dao;
import java.util.List;
import com.icss.entity.Teacher;
public interface TeacherMapper {
public List<Teacher> find();
}
继续service层
@Service
public class TeacherService {
/*@Autowired
SqlSessionFactory factory;
List<Teacher> list=null;
public List<Teacher> find(){
SqlSession sqlSession= factory.openSession();
//取数据访问接口的代理对象
TeacherMapper dao = sqlSession.getMapper(TeacherMapper.class);
//调用接口方法
list = dao.find();
//关流
sqlSession.close();
return list;
}*/
@Autowired
TeacherMapper dao;
public List<Teacher> find(){
return dao.find();
}
}
最后controller层
@Controller
public class TeacherController {
@Autowired
TeacherService service;
@RequestMapping("list")
public String list(Model model){
List<Teacher> list=service.find();
model.addAttribute("teachers", list);
return "teahcerlist";
}
}
jsp 页面就需要teahcerlist.jsp
通过index.jsp首页
<a href="list">老师列表</a>
根据控制器类里@RequestMapping(“list”)找到对应的方法。跳转到指定的teahcerlist.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'teahcerlist.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table border="1" width="400">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<c:forEach items="${teachers }" var="t">
<tr>
<td>${t.id }</td>
<td>${t.tname }</td>
<td>${t.tsex }</td>
<td>${t.tage }</td>
</tr>
</c:forEach>
</table>
</body>
</html>