SSM(spring + springMVC + mybatis)配置
来源:互联网 发布:freecodecamp python 编辑:程序博客网 时间:2024/05/16 02:31
1、引入相关jar包(这里还包括很多扩展内容)
2、web.xml配置(包括了restful风格配置、自定义过滤器和谷歌Kaptcha插件)
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 配置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> <!-- 前端控制器 --> <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> <!-- restful的前端控制器配置 --> <!-- <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> restful配置位/拦截所有 <url-pattern>/</url-pattern> </servlet-mapping> --> <!-- 过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>com.yy.utils.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 验证码插件 --> <servlet> <servlet-name>Kaptcha</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/kaptcha.jpg</url-pattern> </servlet-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
3、分模块配置spring
以下4个文件在spring目录下
mybatis下只有SqlMapConfig.xml
properties文件:
CustomValidationMessage.properties是验证时用到的错误信息提示文件
jdbc.properties提供了是连接数据库的驱动,用户名,密码以及url
log4j.properties是日志文件
(1)springMVC.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 注解的handler --> <!-- <bean class="com.yy.springMVC.ItemsController3"/> --> <!-- spring的注解扫描 --> <context:component-scan base-package="com.yy.controller"/> <!-- 可以替代映射器和适配器 --> <mvc:annotation-driven conversion-service="conversionService"/> <!-- 需要配置静态资源解析 (配置restful以后静态资源访问会出现问题)--> <!-- <mvc:resources location="/" mapping="/**/*.js"/> <mvc:resources location="/" mapping="/**/*.css"/> <mvc:resources location="/" mapping="/**/*.jpg"/> <mvc:resources location="/" mapping="/**/*.png"/> <mvc:resources location="/" mapping="/**/*.gif"/> <mvc:resources location="/assets/" mapping="/assets/**/*"/> <mvc:resources location="/images/" mapping="/images/*" cache-period="360000"/> <mvc:resources location="/images1/" mapping="/images1/*" cache-period="360000"/> --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!-- 转换器 --> <property name="converters"> <list> <bean class="com.yy.controller.converter.CustomDateConverter"/> <bean class="com.yy.controller.converter.StringTrimConverter"/> </list> </property> </bean> <!-- 校验器 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <!-- 注入Hibernate校验器 --> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <!-- 指定校验所使用的资源文件,如果不指定默认使用classes下面的 --> <property name="validationMessageSource" ref="messageSource"/> </bean> <!-- 校验错误信息配置文件 --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <!-- 资源文件名 --> <property name="basenames"> <list> <value> classpath:CustomValidationMessages </value> </list> </property> <!-- 资源文件的编码格式 --> <property name="fileEncodings" value="utf-8"/> <!-- 对资源文件缓冲时间 --> <property name="cacheSeconds" value="120"/> </bean> <!-- 拦截器 --> <!-- <mvc:interceptors> 可配置多个 <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/**/fonts/*"/> <mvc:exclude-mapping path="/**/*.css"/> <mvc:exclude-mapping path="/**/*.js"/> <mvc:exclude-mapping path="/**/*.png"/> <mvc:exclude-mapping path="/**/*.gif"/> <mvc:exclude-mapping path="/**/*.jpg"/> <mvc:exclude-mapping path="/**/*.jpeg"/> <mvc:exclude-mapping path="/**/*login*"/> <mvc:exclude-mapping path="/**/*Login*"/> <bean class="com.yy.controller.interceptor.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors> --> <!-- 配置处理器映射器 根据bean的name进行查找Handler 将action的url配置在bean的name中 --> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> --> <!-- 注解映射器 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> --> <!-- 注解适配器 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 自定义属性编辑器 <property name="webBindingInitializer" ref="customBinder"></property> </bean> --> <!-- 文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为5MB --> <property name="maxUploadSize"> <value>5242800</value> </property> </bean> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 注册属性编辑器 --> <!-- <bean id="customPropertyEditor" class="com.yy.controller.propertyeditor.CustomPropertyEditor"></bean> --> <!-- 自定义的属性编辑器 --> <!-- <bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="propertyEditorRegistrars"> <list> <ref bean="customPropertyEditor"/> </list> </property> </bean> --></beans>
(2)、applicationContext-dao.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:instance="http://www.springframework.org/schema/instance" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 数据源 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="10"/> <property name="maxIdle" value="5"/> </bean> <!-- SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置扫描包的路径 如果要扫描多个包,中间使用多个半角逗号分隔 要求mapper.xml和mapper.java同名且在同一目录下 --> <property name="basePackage" value="com.yy.mapper"/> <!-- 如果使用SqlSessionFactory会和 加载配置文件时冲突--> <!-- 所以使用SqlSessionFactoryBeanName --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean></beans>
(3)、applicationContext-service.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:instance="http://www.springframework.org/schema/instance" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 注入service(单个注入) --> <!-- <bean id="userService" class="com.yy.service.impl.UserServiceImpl"/> --> <!-- spring的注解扫描 --> <context:component-scan base-package="com.yy.service"/></beans>
(4)、applicationContext-transaction.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:instance="http://www.springframework.org/schema/instance" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 使用声明式事务配置,可以有效地规范代码 --> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="load*" propagation="SUPPORTS" read-only="true" /> <tx:method name="list*" propagation="SUPPORTS" read-only="true" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> <!-- aop --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution( * com.yy.service..*.*(..))"/> </aop:config></beans>
(5)SqlMapConfig.xml
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <!-- 属性定义 加载一个properties文件 在 properties标签中配置属性值 --> <!-- <properties resource="jdbc.properties"> <property name="" value=""/> </properties> --> <settings> <!-- 延迟加载总开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 设置按需加载 --> <setting name="aggressiveLazyLoading" value="false"/> </settings> <!-- 定义 别名 --> <typeAliases> <!-- 单个别名的定义 alias:别名,type:别名映射的类型 --> <!-- <typeAlias type="com.yy.mybatis.po.User" alias="user"/> --> <!-- 批量别名定义 指定包路径,自动扫描包下边的pojo,定义别名,别名默认为类名(首字母小写或大写) --> <package name="com.yy.po"/> </typeAliases> <!-- 和spring整合后 environments配置将废除--> <!-- <environments default="development"> <environment id="development"> 使用jdbc事务管理 <transactionManager type="JDBC" /> 数据库连接池 <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> --> <!--加载mapper映射 如果将和spring整合后,可以使用整合包中提供的mapper扫描器,此处的mappers不用配置了。 --> <!-- <mappers> 通过resource引用mapper的映射文件 <mapper resource="sqlmap/User.xml" /> <mapper resource="mapper/Usermapper.xml" /> 通过class引用mapper接口 class:配置mapper接口全限定名 要求:需要mapper.xml和mapper.java同名并且在一个目录 中 <mapper class="com.yy.mybatis.mapper.UserMapper"/> 批量mapper配置 通过package进行自动扫描包下边的mapper接口, 要求:需要mapper.xml和mapper.java同名并且在一个目录 中 <package name="com.yy.mapper"/> </mappers> --></configuration>
0 0
- SSM(spring + springMVC + mybatis)配置
- SSM配置 springmvc spring mybatis
- SSM(Spring+SpringMVC+Mybatis)框架配置例子
- SSM(spring+springMVC+Mybatis)配置步骤
- ssm相对标准配置(Spring+springmvc+mybatis)
- SSM(spring+springMVC+mybatis)整合配置
- Eclipse+maven配置SSM(Spring+SpringMVC+mybatis)
- ssm---(springMvc+spring+mybatis)
- SSM(SpringMVC+Spring+Mybatis)整合
- SSM,Spring,SpringMVC,Mybatis---28
- springmvc+mybatis+spring 整合SSM
- SSM(springMVC+spring+MyBatis)
- 【SSM】Spring+SpringMVC+Mybatis整合
- SSM (Spring+SpringMVC + MyBatis)
- SSM框架:SpringMVC + Spring + MyBatis
- SSM整合(Spring+SpringMVC+Mybatis)
- SSM Spring +SpringMVC+Mybatis 配置 及pom.xml
- java web,从零开始,一步一步配置ssm(Spring+SpringMVC+MyBatis)框架
- shell 笔记2
- ModelSim对.txt文件的读写——十进制格式
- (五)容器-集合
- 蓝桥杯 算法提高 矩阵乘法 (区间dp)
- ACM递推递归练习J 数学黑洞
- SSM(spring + springMVC + mybatis)配置
- java设计模式之模板方法模式
- maven
- 让Fetch()也可以Timeout
- HDOJ 3460 Ancient Printer
- randomAccessFile
- 师--链表的结点插入
- 神经网络在化学中的应用
- 字符串和存储区