1、jar包整理
2、web.xml配置
配置spring mvc入口:即添加一个拦截器,spring mvc的入口类为org.springframework.web.servlet.DispatcherServlet,如果要对入口类做一定的封装,只要自己编写一个类继承DispartServlet,重写相应方法即可,比如被spring mvc拦截器,没有任何匹配的控制器,则可以重写noHandlerFound即可。
spring mvc在web.xml配置如下
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--表示入口程序为--> <init-param> <param-name>contextConfigLocation</param-name><!--加载配置文件的参数--> <param-value>classpath:spring-servlet-action.xml</param-value><!--spring mvc初始化加载的配置文件名称,classpath表示class根路径--> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name><!--匹配servlet-name--> <url-pattern>*.do</url-pattern><!--拦截所有以.do结尾的url请求--> </servlet-mapping>
spring 在 web.xml中的配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener<!--spring容器初始化监听器,我们可以自己编写一个类继承他,然后重写2、contextInitialized就可以监听容器初始化完成的事件,contextDestroyed表示容器销毁的事件 --></listener-class></listener><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value><!--spring容器启动时,回去contextConfigLocation下的配置文件名称,如果不配则是
从classpath下面名为applicationContent.xml文件--> </context-param>
3、spring mvc配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 注解探测器 --> <context:component-scan base-package="com.travel.controller"/> <!-- spring mvc控制器扫描包--> <!-- 视图解析器,根据视图的名称new ModelAndView(name),在配置文件查找对应的bean配置 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="1"/> </bean> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean> -->
<!-- spring mvc 当为jsp视图,则访问/WEB-INF/下面的jsp页面-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp" /></beans>
4、spring配置
2、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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.travel" /><!-- 依赖注入扫描的包的根路径--> <!--spring 中读取配置文件方法,比如存在多个版本配置(生产,开发,测试)三个环境,则可以通过profile授权控制-->5、profile 授权配置<beans profile="dev"> <context:property-placeholder location="classpath:sys_jdbc_dev.properties" /> </beans> <beans profile="produce"> <context:property-placeholder location="classpath:sys_jdbc_produce.properties" /> </beans> <beans profile="test"> <context:property-placeholder location="classpath:sys_jdbc_test.properties" /> </beans></beans>
如要实现上述多个环境动态配置,则可以在web.xml中增加如下配置
<context-param> <param-name>spring.profiles.active</param-name><!-- profile参数名,固定不变--> <param-value>test</param-value><!-- profile参数值对应<beans profile="dev"中的值 --> --> </context-param>
6、spring整合mybatis
由于mybati是和spring整合所以所有的配置文件皆在spring容器的配置文件中,通常applicationContent.xml配置文件
6.1数据源配置
2、web.xml配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!-- destroy-method要写,防止连接池在容器关闭时没有销毁--> <property name="driverClassName" value="${jdbc.user.driver}" /><!--数据库链接类--> <property name="url" value="${jdbc.user.url}" /><!--数据库地址路径--> <property name="username" value="${jdbc.user.name}" /><!--账号--> <property name="password" value="${jdbc.user.password}" /><!--密码--> <!--maxActive: 最大连接数量--> <property name="maxActive" value="${jdbc.user.maxActive}"/> <!--minIdle: 最小空闲连接--> <property name="minIdle" value="${jdbc.user.minIdle}"/> <!--maxIdle: 最大空闲连接--> <property name="maxIdle" value="${jdbc.user.maxIdle}"/> <!--initialSize: 初始化连接--> <property name="initialSize" value="${jdbc.user.initialSize}"/> <!-- 连接被泄露时是否打印 --> <property name="logAbandoned" value="${jdbc.user.logAbandoned}"/> <!--removeAbandoned: 是否自动回收超时连接--> <property name="removeAbandoned" value="${jdbc.user.removeAbandoned}"/> <!--removeAbandonedTimeout: 超时时间(以秒数为单位)--> <property name="removeAbandonedTimeout" value="${jdbc.user.removeAbandonedTimeout}"/> <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒--> <property name="maxWait" value="${jdbc.user.maxWait}"/> <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. --> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.user.timeBetweenEvictionRunsMillis}"/> <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 --> <property name="numTestsPerEvictionRun" value="${jdbc.user.numTestsPerEvictionRun}"/> <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程--> <property name="minEvictableIdleTimeMillis" value="${jdbc.user.minEvictableIdleTimeMillis}"/> <property name="validationQuery" value="${jdbc.user.validationQuery}"/> </bean>附上sys_jdbc.properties文件配置
jdbc.user.driver=com.mysql.jdbc.Driver jdbc.user.url=jdbc:mysql://127.0.0.1:3306/creeper?characterEncoding=UTF-8 jdbc.user.name=root jdbc.user.validationQuery=SELECT NOW() FROM DUAL jdbc.user.minEvictableIdleTimeMillis=1000 jdbc.user.numTestsPerEvictionRun=10 jdbc.user.timeBetweenEvictionRunsMillis=10000 jdbc.user.maxWait=1000 jdbc.user.removeAbandonedTimeout=10 jdbc.user.removeAbandoned=true jdbc.user.logAbandoned=true jdbc.user.initialSize=30 jdbc.user.maxIdle=20 jdbc.user.minIdle=5 jdbc.user.maxActive=150 jdbc.user.password=
6.2 mybatis session工厂配置
<bean id="sqlSessionFactoryConfig" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /><!--6.1配置的数据库数据源--> <property name="mapperLocations" value="classpath:mybatis_mapper/config/*Mapper.xml" /><!--匹配的路径,表示classpath路径下,config文件
下以Mapper.xml配置文件结尾的类--> <property name="typeAliasesPackage" value="com.travel.beans" /><!--对应实体类的所在包--> <property name="plugins"> <ref bean="paginationInterceptor" /><!--插件类--> </property> </bean>
插件类配置如下,本类主要用来实现mybatis分页,mybatis原生分页为逻辑分页,后面附上代码
<bean id="paginationInterceptor" class="com.travel.global.PaginationInterceptor"></bean>
6.3 mybatis代理类配置
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.travel.dao" /> <!--mybatis对应的dao所在目录,由于mybatis的dao层为接口,常理接口不能执行和实例化,所以MapperScannerConfigurer会生成对应的代理类 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><!----> </bean>
7、综上ssm整合基本完毕,接下来,首先利用mybatis生成工具,自动生成基本的xml和dao,beam
附上mybatisgenerator的配置文件,<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <!-- 引入配置文件 --> <properties resource="sys_jdbc.properties" /> <!-- 指定数据连接驱动jar地址 --> <classPathEntry location="${classpath}"/><!--mysql而言为,mysql-connector-{version}.jar--> <context id="context1" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"/> <!-- 注释 --> <commentGenerator > <property name="suppressAllComments" value="false"/><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--> </commentGenerator> <jdbcConnection driverClass="${jdbc.config.driver}" connectionURL="${jdbc.config.url}" userId="${jdbc.config.name}" password="${jdbc.config.password}" /> <javaModelGenerator targetPackage="com.travel.beans"<!--实体类对应名称--> targetProject="${project}" /> <sqlMapGenerator targetPackage="mybatis_mapper" targetProject="${project}" /><!--targetPackage,xml生成的路径,targetProject:生成的项目--> <javaClientGenerator targetPackage="com.travel.dao.config"<!--dao生成的文件--> targetProject="${project}" type="XMLMAPPER" /> <table schema="${jdbc.config.db}" tableName="usr_ppt_order_relation"<!--数据库对应的表名--> domainObjectName="UsrPptOrderRelation" enableCountByExample="true"<!--生成的实体类的名称--> enableDeleteByExample="true" enableSelectByExample="true" enableUpdateByExample="true" ></table> </context> </generatorConfiguration>
附上jdbc配置
classpath=D:/developTools/java_workspace_sdk/TravelCreeper/lib/mysql-connector-java-5.0.8-bin.jar project=../src
8、编写测试代码
8.1控制器代码
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by 520cloud on 2016/9/25. */ @Controller @RequestMapping(value="/") public class PageController {
<!--动态注入mybatis dao层-->
@Resourcepublic TestMapper testMapper;@RequestMapping(value="Page/{pageName}.do") public String page(@PathVariable("pageName")String pageName){ return "jsp/"+pageName; } }如果项目名为Test,则上述控制器访问的地址:localhost:8080/Test/Page/test.do则为访问的是/WEB-INF下面的test.jsp这里可以通过PathVariable来动态获取路径中的变量9、整个流程中需要用到的jar包和mybatis分页代码,点击下面地址
http://download.csdn.net/detail/cyssxt/9759145
http://download.csdn.net/detail/cyssxt/9759137