今天终于决定要写博客了,好久以前都纠结博客这个事,终于下定决心开始行动。仔细想了想,没什么可写的,正好现在正在学习SSM(sping+springMVC+mybatis)这个框架,于是决定把这三个框架的整合教程整理成一篇文章进行发布出来,希望对大家能够有所帮助。说了好多废话,现在正式进入主题:
IDE: Eclipse Mars Release (4.5.0)
DB: MySQL 5.1.73
服务器: Tomcat 7.0
1,首先在Eclipse中新建一个Dynamic Web Project,暂且起名为SSM01;
2,新建包名:
com.juyuan238.ssm.mapper
com.juyuan238.ssm.service
com.juyuan238.ssm.pojo
com.juyuan238.ssm.controller
3, 导入jar包:
http://download.csdn.net/detail/samile6899/9100335(jar包资源)
此时的的项目结构:
![](https://img-blog.csdn.net/20150912105840814)
4, 新建一个Source Folder,命名为config;(将资源文件都放在config文件夹中)
在config文件夹中新建db.properties文件,定义数据库连接参数,
db.read.database=数据库名
db.read.host=数据库连接地址
db.read.port=端口号
db.read.userName=数据库连接的账号
db.read.password=数据库连接的密码
5, 将log4j.properties文件复制粘贴到config文件夹中;
6, 新建sqlMapConfig.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="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="autoMappingBehavior" value="FULL" />
<setting name="defaultExecutorType" value="BATCH" />
<setting name="defaultStatementTimeout" value="25000" />
</settings>
<typeAliases>
//......
</typeAliases>
<mappers>
//......
</mappers>
</configuration>
7, 新建spring.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.juyuan238.ssm"/>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl">
<value><![CDATA[jdbc:mysql://${db.read.host}:${db.read.port}/${db.read.database}?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true]]></value>
</property>
<property name="user" value="${db.read.userName}" />
<property name="password" value="${db.read.password}" />
<property name="maxPoolSize" value="12" />
<property name="minPoolSize" value="0" />
<property name="maxStatements" value="100" />
<property name="initialPoolSize" value="3" />
<property name="maxIdleTime" value="10"/>
<property name="idleConnectionTestPeriod" value="10" />
<property name="testConnectionOnCheckin" value="true" />
<property name="testConnectionOnCheckout" value="false" />
<property name="preferredTestQuery" value="SELECT 1 FROM DUAL" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
8, 在WEB-INF下面新建springMVC-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<context:component-scan base-package="com.juyuan238.ssm.controller"/>
<mvc:annotation-driven />
<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>
9, 编辑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>Web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/assets/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<distributable/>
</web-app>
10, 新建一个Controller类,命名为: ShowController
@Controller
public class ShowController {
@RequestMapping("/")
public String index(){
return "login";
}
}
11,到WEB-INF/jsp下面新建“ login.jsp ”页面
<body>
<h1>欢迎来到我的页面!</h1>
</body>
12,新建jsp页面可能会报错,
![](https://img-blog.csdn.net/20150912113817309)
解决办法:
选中项目--->右键"Build Path"--->"Configure Build Path"--->Add Library--->Server Runtime--->Next--->选中一个创建的tomcat( 如果没有的话,就是没有创建tomcat服务器,自己去创建一个tomcat服务器 )--->Finish--->OK
13, 点击tomcat添加并运行该项目
14,OK,成功运行该项目,进入到login.jsp中,框架搭建成功!接下来,会继续发布增删改查的帖子,希望发布的帖子能够对大家能够有所帮助,有所启迪!