博客转载至:http://blog.csdn.net/u011781521/article/details/53725788
一、所用版本与步骤
三个框架所使用的版本:
mybatis ---- 3.2.5
spring ---- 4.3.3.RELEASE
spring mvc ---- 4.3.3.RELEASE
其它:
junit ---- 4.8.1
MySQL ---- 5.1.38
log4j ---- 1.2.17
c3p0 ---- 0.9.2.1
mybatis-spring ---- 1.3.0
jstl ---- 1.2
SSM框架整合的一般步骤:
1.新建一个Maven WEB项目
2.导入MySQL与Mybatis 依赖
3.写Mybatis配置文件及实体类和映射文件
4.测试(Mybatis)
5.导入Spring 依赖
6.写Spring 配置文件applicationContext(引入db,c3p0DataSource,SessionFactory,事物)
7.测试(Spring与Mybatis)
8.整合Spring MVC
9.写Controller包与Controller类
10.写Spring MVC配置文件(springmvc.xml)
11.web.xml中加载配置文件(Spring,SpringMVC)
二、整合框架
项目结构:
pom.xml文件内容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.fendo.ssmgroupId>
<artifactId>fendo-SSMartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>fendo-SSM Maven Webappname>
<url>http://maven.apache.orgurl>
<properties>
<spring.version>4.3.3.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.3version>
<scope>providedscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.2.5version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-oxmartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.2.1version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
dependencies>
<build>
<finalName>fendo-SSMfinalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<url>http://localhost:8080/manager/texturl>
<username>adminusername>
<password>adminpassword>
configuration>
plugin>
plugins>
build>
project>
1.新建一个Maven WEB项目
在eclipse菜单栏中选择“File”—“News”—“Other”菜单项,就会打开如下对话窗口,在窗口中选择“Maven”节点下“Maven Project”条目,如下图所示:
点击Next
点击Next
点击Next
点击Finish
注:
index.jsp页面报错: The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
原因是没加入ServletAPI所导致的,在pom.xml中加入依赖就行了。
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.3version>
<scope>providedscope>
dependency>
2.导入MySQL与Mybatis 依赖
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.2.5version>
dependency>
3.写Mybatis配置文件及实体类和映射文件
新建两个包:
com.fendo.entity —-实体包
com.fendo.dao —-方法包
并在其中加入一个Person类和Person.xml的映射文件,和一个操作Person的接口类,以及一个测试类:
mySql数据库新建一个表: person
插入数据
实体类: person.java
package com.fendo.entity;
public class Person {
private int pid;
private String pname;
private int page;
private int pageIndex;
private int pageSize;
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int pid, String pname, int page) {
super();
this.pid = pid;
this.pname = pname;
this.page = page;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
@Override
public String toString() {
return "Person [pid=" + pid + ", pname=" + pname + ", page=" + page + "]";
}
public Person(String pname, int page) {
super();
this.pname = pname;
this.page = page;
}
}
SQL操作配置文件: Person.xml
<mapper namespace="com.fendo.dao.PersonDao">
<select id="getPersons" resultType="Person">
select * from person
select>
<insert id="addPerson" parameterType="person">
insert into person(pname,page) values(#{pname},#{page})
insert>
mapper>
方法接口类: PersonDao.java
package com.fendo.dao;
import java.util.List;
import com.fendo.entity.Person;
public interface PersonDao {
public List getPersons();
public void addPerson(Person person);
}
MyBatis配置文件-myBatisConfig.xml
<configuration>
<typeAliases>
<package name="com.fendo.entity"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="com/fendo/entity/Person.xml"/>
mappers>
configuration>
Log4j配置文件-log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
测试类: TestMybatis.java
package com.fendo.test;
import java.io.IOException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.fendo.dao.PersonDao;
public class TestMybatis {
private SqlSession sqlSession;
@Before
public void before(){
try {
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("myBatisConfig.xml"));
sqlSession = sqlSessionFactory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void after(){
sqlSession.commit();
sqlSession.close();
}
@Test
public void test(){
PersonDao personDao=sqlSession.getMapper(PersonDao.class);
System.out.println(personDao.getPersons().size());
}
}
4.测试(Mybatis)
点击test右键Run AS —-> Junit Test
Mybatis测试成功,下载地址: http://download.csdn.net/detail/u011781521/9714668
5.导入Spring 依赖
<properties>
<spring.version>4.3.3.RELEASEspring.version>
properties>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-oxmartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.2.1version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
6.写Spring 配置文件applicationContext(引入db,c3p0DataSource,SessionFactory,事务)
在src/main/resources下新建applicationContext.xml,以及db.properties配置文件
applicationContext.xml内容如下:
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
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-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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${uname}">property>
<property name="password" value="${upass}">property>
<property name="jdbcUrl" value="${url}">property>
<property name="driverClass" value="${driver_class}">property>
<property name="initialPoolSize" value="${initPoolSize}">property>
<property name="maxPoolSize" value="${maxPoolSize}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocation" value="classpath:myBatisConfig.xml">property>
<property name="mapperLocations" value="classpath:com/fendo/entity/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fendo.dao">property>
bean>
beans>
配置Dao接口的两种方法说明:
db.properties内容如下:
uname=root
upass=123
driver_class=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
initPoolSize=5
maxPoolSize=10
把myBatisConfig.xml文件修改成如下:
<configuration>
<typeAliases>
<package name="com.fendo.entity"/>
typeAliases>
configuration>
连接数据库属性哪些配置移到了applicationContext.xml文件中去了。
7.测试(Spring与Mybatis)
新建TestSpringMybatis测试类:
public class TestSpringMybatis {
public static void main(String[] args) {
//01.加载spring配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDao personDao=(PersonDao) ac.getBean("personDao");
personDao.addPerson(new Person("小小A", 10));
System.out.println(personDao.getPersons().size());
}
}
运行输出结果如下:
spring+mybatis整合成功!!!!
8.整合Spring MVC
先写一个简单的注册页面与一个注册成功页面
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册页面title>
head>
<body>
<form action="addPerson.action" method="post">
pname:<input type="text" name="pname"/><br/>
page:<input type="text" name="page"/><br/>
<input type="submit" value="添加"/><br/>
form>
body>
html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<table border="1">
<tr>
<th>编号th>
<th>姓名th>
<th>年龄th>
tr>
<c:forEach items="${persons}" var="p">
<tr>
<td>${p.pid}td>
<td>${p.pname}td>
<td>${p.page}td>
tr>
c:forEach>
table>
body>
html>
这里导入了JSTL所以要在pom.xml中引入依赖
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
9.写Controller包与Controller类
新建一个Controler包和一个PersonController类
@Controller
public class PersonController {
@Autowired
private PersonDao personDao;
@RequestMapping("/addPerson")
public ModelAndView addPerson(Person person){
System.out.println("页面数据:"+person);
//加入数据
personDao.addPerson(person);
//查数据
List persons=personDao.getPersons();
//存起来
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("success");
modelAndView.addObject("persons", persons);
return modelAndView;
}
}
10.写Spring MVC配置文件(springmvc.xml)
<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/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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.fendo">context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp">property>
<property name="prefix" value="/">property>
bean>
beans>
11.web.xml中加载配置文件(Spring,SpringMVC)
<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springDispatcherServletservlet-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>springDispatcherServletservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
web-app>
最后在依赖中加入tomcat插件
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<url>http://localhost:8080/manager/texturl>
<username>adminusername>
<password>adminpassword>
configuration>
plugin>
plugins>
然后右键项目–>Run AS –> Run Configurations ,在maven build的goals中输入命令: tomcat7:run
点击Run
然后访问: http://localhost:8080/fendo-SSM/index.jsp
点击添加
添加成功
完整的项目地址: http://download.csdn.net/detail/u011781521/9714655