Spring--ssm整合(分)
来源:互联网 发布:mac pro可以玩gta5吗 编辑:程序博客网 时间:2024/06/06 01:41
1 导入jar
导入spring(包含springmvc),mybatis,mybatis-spring整合。数据库驱动,jstl,c3p0管理数据源,log4j.
2 配置web.xml入门文件
2.1 图解
2.2 加载Spring容器
<!-- 1.Spring容器 applicationContext.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
2.3 加载springmvc.xml
<!-- 2.Springmvc 前端控制器 --> <!-- 2.1 加载 springmvc.xml --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 2.2 设置请求url --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 2.3 restful风格,过滤.do --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
2.4 编码过滤—springMVC自带
<!-- 3.编码过滤 --> <filter> <filter-name>characterEncoding</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>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.5 Restfull处理各种类型请求转换
<!-- 4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>HttpPutFormContentFilter</filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpPutFormContentFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3 springmvc.xml
3.1 图解
3.2 全局扫描—只扫描@Controller
<!-- 1. 扫描 --> <context:component-scan base-package="com.lan" use-default-filters="false"> <!--只扫描控制器。@Controller --> </context:component-scan>
3.3 映射器适配器,处理json
<!--2. 两个标准配置 --> <!-- 将springmvc不能处理的请求交给tomcat --> <mvc:default-servlet-handler /> <!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 --> <mvc:annotation-driven />
3.4 视图解析器
<!--3. 配置视图解析器,方便页面返回 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean>
4 applicationContext-dao.xml
4.1 图解
4.2 全局扫描--无
4.3 数据源
4.3.1 db.properties
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/e3mall-32?characterEncoding=utf-8jdbc.driverClass=com.mysql.jdbc.Driverjdbc.user=rootjdbc.password=root
4.3.2 引入、加载数据源 alibaba
<!-- 2.数据源 --><!-- 2.1 引入--><context:property-placeholder location="classpath:db.properties" /><!-- 2.2 加载 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${jdbc.jdbcUrl}"></property> <property name="driverClassName" value="${jdbc.driverClass}"></property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /></bean>
4.4 sqlSessionFactory
4.4.1 第一种
<!-- 3.sqlSessionFactory 配置和MyBatis的整合 --> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <!--3.1注入数据源 --> <propertyname="dataSource" ref="dataSource"></property> <!--3.2 指定mybatis 全局配置文件 --> <propertyname="configLocation" value="classpath:sqlMapConfig.xml"></property> <!--3.3指定mybatis,mapper文件的位置 --> <propertyname="mapperLocations"value="classpath:mapper/*.xml"></property> </bean>
4.4.2 第二种 在同包下
<!--3.sqlSessionFactory 配置和MyBatis的整合 --><bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 3.1注入数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 3.2 指定mybatis 全局配置文件 --> <property name="configLocation"value="classpath:mybatis/SqlMapConfig.xml"/> <!-- 3.3指定mybatis,mapper文件的位置--> <!--<propertyname="mapperLocations"value="classpath:mapper/*.xml"></property>--></bean>
多一步pom加载xml
<build> <finalName>${project.artifactId}</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources></build>
4.5 Mybatis
<!-- 4. 配置mybatis扫描器, --><!-- 4.1 将mybatis接口的实现加入到ioc容器中 dao接口 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.e3mall.mapper"></property> <!-- 使用sqlSessionFactoryBeanName --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean><!-- 4.2 (可选)配置一个可以执行批量的sqlSession --><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg></bean>
5 applicationContext-service.xml
<!-- 配置包扫描器 --><context:component-scan base-package="cn.e3mall.service"/>
6 applicationContext-trans.xml-事务
6.1 xml
<!-- 事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /></bean><!-- 通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes></tx:advice><!-- 切面 --><aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.e3mall.service..*.*(..))" /></aop:config>
6.2 注解
<!-- 5.事务控制 --><!-- 5.1 声明事务 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入数据源 --><property name="dataSource" ref="dataSource"></property></bean><!-- 5.2 开启事务注解 --><tx:annotation-driven transaction-manager="transactionManager" />
7 SqlMapConfig.xml—全局配置文件
<!-- 1.驮峰式命名规则 --> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <!-- 2.ognl实例类的别名 --> <typeAliases> <package name="com.lan.pojo" /> </typeAliases> <!-- 3.(可选扩展)注册分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
8 工程结构
源代码文件:http://download.csdn.net/detail/qq_26553781/9852543
http://download.csdn.net/download/qq_26553781/10034223
阅读全文
0 0
- Spring--ssm整合(分)
- spring--ssm整合
- SSM项目整合-整合Spring
- SSM(SpringMVC+Spring+Mybatis)整合
- Spring+WebSocket整合(SSM)
- springmvc+mybatis+spring 整合SSM
- 【spring】ssm整合小知识
- 【SSM】Spring+SpringMVC+Mybatis整合
- SSM整合(spingmvc,spring,mybatis)
- SSM整合(Spring+SpringMVC+Mybatis)
- SSM整合(2): spring 与 mybatis 整合
- SSM整合(2): spring 与 mybatis 整合
- Spring+Spring MVC+MyBatis(SSM)整合
- SSM(Spring mvc+Spring+Mybatis)框架整合
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
- SSM(Spring+SpringMVC+MyBatis)整合
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- Spring+Mybatis整合(1)- SSM(四)
- Linux学习(二)
- 用python改变目录里所有图片的尺寸
- [Paper 资源帖]ICML 2017中有关GAN的Paper
- 结构体定义的三钟方式
- 数组的排序
- Spring--ssm整合(分)
- 开发微信公众号授权失败
- javascript 学习笔记
- 目标跟踪之相关滤波:CF及后续改进篇
- 坚持#第180天~优秀
- 基于STM32wifi小车制作(四)
- nuc972平台qt+dfb+2d简单性能对比测试
- mysql 查询当天、最近一天、最近一周,本月,上一个月的数据
- 小动画:线条流水般变大缩小