SpringMvc + Mybatis + Spring - 工程总结 ,基础知识结语篇
来源:互联网 发布:淘宝iphone官换机 骗局 编辑:程序博客网 时间:2024/05/07 10:51
结语:
到此为止,大部分的基础知识已经学习完毕了,但并没有学习全面,连cookie操作,图片的上传优化,分页请求实现等都还没有实现。故,在后面需要进行完整的项目实现,在此将Spring+ SpringMvc + Mybatis的工程进行了总结,和配置信息清单的总述;
SpringMvc+Spring+Mybatis 总配置 :
下载地址 :http://download.csdn.net/detail/lablenet/9396439
清单如下 :
1.Jar包
Spring 3.2.0SpringMvc 3.2.0
Mybatis 3.2.7
Mybatis-Spring 1.2.2
Oracle-jdbc 5
mysql-jdbc 5.1.7
jstl 1.2
jackson-mapper 1.9.11
jackson-core 1.9.11
dbcp-cglib 2.2.2
hibernate-validation 4.3.0
2.Spring配置
2.1 applicationContext-dao.xml1)加载数据库配置信息
<context:property-placeholder location="classpath:db.properties"/>
2)加载数据源 :
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.name}"></property> <property name="password" value="${jdbc.pass}"></property> <property name="maxActive" value="10"></property> <property name="maxIdle" value="5"></property> </bean>
3)配置sqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/sqlmapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean>
4)采用mapper扫描方式 ,加载mapper
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.labelnet.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
2.2 applicationContext-service.xml
配置service的bean , 提供给HandlerMapping使用
2.3 applicationContext-transcation.xml
1)配置事务管理器,使用Spring下的jdbc事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource"></property> </bean>
2)通知配置
<tx:advice id="txManager" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice>
3)aop配置
<aop:config> <aop:pointcut expression="execution(* cn.labelnet.ssm.service.impl.*.*(..))" id="perfrom"/> <aop:advisor advice-ref="txManager" pointcut-ref="perfrom"/> </aop:config>
2.4 Springmvc.xml
1)配置HandlerMapping(Controller)扫描方式 ,指定包名
<context:component-scan base-package="cn.labelnet.ssm.controller"></context:component-scan>
2)RESTful静态文件加载配置,指定自己路径
<mvc:resources location="/" mapping="/**" />
3)使用注解方式配置:不建议
4)使用注解驱动方式配置 :
<mvc:annotation-driven conversion-service="conversionService" validator="validator"></mvc:annotation-driven>
5)配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
6)可配置自定义类型转换器
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <!-- 在这里可以定义多个转换 --> <bean class="cn.labelnet.ssm.controller.converter.FormatDateConverter"></bean> </list> </property><span style="font-family: 'Comic Sans MS';font-size:18px;"> </bean></span>
7)配置校验器
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <!-- 配置hibernate校验器 --> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property> <!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如过不指定默认使用classpath下的ValidationMessages.properties --> <property name="validationMessageSource" ref="messageSource"></property> </bean>
8)配置校验器信息配置文件
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <!-- 资源文件名:配置自己的验证信息 --> <property name="basenames"> <list> <value>classpath:CustomValidationMessage</value> </list> </property> <!-- 资源文件编码格式 --> <property name="fileEncodings" value="utf-8"></property> <!-- 对资源文件内容缓存时间,单位秒 --> <property name="cacheSeconds" value="120"></property> </bean>
9)配置全局异常类
实现 HandlerExceptionResolver接口 :
<bean class="cn.labelnet.ssm.controller.exception.CustomExceptionResover"></bean>
10)配置文件上传解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 最大的上传尺寸 --> <property name="maxUploadSize" value="5242880"></property> </bean>
11)配置拦截器
<mvc:interceptors> <!-- 多个拦截器,顺序执行 --> <mvc:interceptor> <!-- /** 表示所有的url,包括子url路径 --> <mvc:mapping path="/**"/> <bean class="cn.labelnet.ssm.filter.HanderInterceptor1"></bean> </mvc:interceptor> <!-- 配置登陆拦截器 --> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="cn.labelnet.ssm.filter.LoginHandlerIntercepter"></bean> </mvc:interceptor> <!-- ..... --> </mvc:interceptors>
2.4 Spring和SpringMVC
配置信息在这里实现,尤其是SpringMvc的配置尤其重要,涉及到很多,比如 校验器,拦截器,全局异常信息类,文件上传解析器等,
而其实都是Spring的配置文件,Spring配置文件加载,在web.xml中进行,采用通配符的方式加载Spring的配置文件,所以Spring的
配置文件在命名的时候,也很重要!
<!-- 加载spring容器:采用通配符的方式实现 -->
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
而SpringMVc的配置文件加载,在SpringMVc的前端控制器中进行加载 ,比如 :
<!-- 配置Springmvc 处理器 --> <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:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
3.Mybatis配置
3.1 db.properties数据库信息
3.2 log4j.properties
固定的配置信息,一般在开发的时候,将其设置为 DEBUG模式 ,开发完成时,设置为 INFO 模式 ;
# Global logging configuration # 在这里修改logger模式 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
3.3 sqlMapConfig.xml
对Mybatis的配置信息的配置,如 setiing, 别名 等 ,而 mapper的加载 就不需要在这里实现了,
在SpringMvc.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>
3.4 Mapper.xml
在这里我们需要注意几点,首先mapper.xml和mapper.java在同一个包下,且名字一样;其次是,mapper.xml
中的命名空间是mapper.java的全限定名,操作的id是mapper.java的方法名;
<?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="全限定名"> </mapper>
4.web配置-web.xml
4.1 过滤器实现
固定不变,可直接复制
<filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4.2 加载Spring容器
需要配置<param-value>的spring配置文件的位置,可以在部署在tomcat上后在配置位置;
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
4.3 配置SpringMvc前端控制器
需要配置springmvc.xml 的位置,和 url-pattern 的配置,url的后缀;
<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:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
4.4 可以配置RESTful的URL支持
url-pattern的值为 / ;,可以同时配置,并不冲突;
<servlet> <servlet-name>springmvc_rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc_rest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
5.工程目录固定化
5.1 srccn.labelnet.ssm.controller
--HandlerMapping.java : controller实现
cn.labelnet.ssm.controller.converter
--自定义参数类型,实现Converter接口
cn.labelnet.ssm.controller.exception
--自定义异常类,继承Exception
--全局异常处理器,实现HandlerExceptionResolver接口
cn.labelnet.ssm.controller.validated
--校验分组接口,定义即可,不需要方法
cn.labelnet.ssm.filter
--拦截器实现,实现HandlerInterceptor接口
--实现preHandler,postHandler,afterHandler三个方法
cn.labelent.ssm.mapper
-- mapper.java 和 mapper.xml
cn.labelnet.ssm.po
-- pojo类
cn.labelnet.ssm.po.custom
-- pojo拓展类
cn.labelent.ssm.po.vo
--包装类
cn.labelnet.ssm.service
-- 业务接口
cn.labelnet.ssm.service.impl
-- 业务接口实现类
5.2 config
--db.properties
--log4j.properties
--CustomValidationMessage.properties
mybatis
--sqlmapconfig.xml
spring
--applicationContext-dao.xml
--applicationContext-service.xml
--applicationContext-transcation.xml
--springmvc.xml
5.3 test
cn.labelnet.ssm.test
--各种测试类
5.5 webRoot
个人需求
总配置下载地址 :http://download.csdn.net/detail/lablenet/9396439
0 0
- SpringMvc + Mybatis + Spring - 工程总结 ,基础知识结语篇
- spring+springMVC+Mybatis在maven工程的搭建总结
- 搭建Spring+SpringMVC+MyBatis工程
- 框架基础知识spring springmvc mybatis struts hibernate
- Spring SpringMVC Mybatis项目总结
- Mybatis+SpringMVC+Spring框架实践总结
- Spring+SpringMVC+mybatis框架环境搭建总结
- spring+springmvc+mybatis框架搭建总结
- Eclipse创建WEB工程Maven+Spring+SpringMVC+MyBatis示例
- maven工程建立和SSM(springMVC+spring+mybatis)整合
- spring+springMVC+mybatis +dubbo架构的工程创建(一)
- 如何搭建SSM(SpringMvc+mybatis+Spring)java web工程
- Spring+SpringMVC+MyBatis框架一个简单工程的搭建实例
- spring-springmvc-mybatis-maven整合的简单工程
- SSM(SpringMvc+mybatis+Spring)java web搭建工程
- 手把手教你Spring+SpringMVC+MyBatis工程创建
- spring+springmvc+mybatis(3)--逆向工程自动生成代码
- 自己搭建spring-springmvc-mybatis工程
- #if defined 与 #ifdef
- PHP写一个函数,算出两个文件的相对路径
- 1010. 一元多项式求导 (25)
- 鸡尾酒要左手拿着喝
- 【计算机基础】字符集和字符编码(Charset & Encoding)
- SpringMvc + Mybatis + Spring - 工程总结 ,基础知识结语篇
- css3
- Linux:如何获取Linux发行版的内核头文件
- java日期格式大全 format SimpleDateFormat
- 注入式开发(二):.NET 匿名函数
- Python字符串操作
- HandlerThread
- ibatis.SqlMapClientTemplate的使用集锦
- oracle跟踪事件(dump)总结