SSM三大框架整合配置(Spring+SpringMVC+MyBatis)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 定义Spring MVC的前端控制器 -->
<servlet>
<servlet-name>UserManage</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/UserManage-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- 让Spring MVC的前端控制器拦截所有请求 -->
<servlet-mapping>
<servlet-name>UserManage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- ================Spring配置开始================ -->
<!-- 设置Spring容器加载配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/config/spring-config.xml</param-value>
<!-- <param-value>classpath:/spring-*.xml</param-value> -->
</context-param> <!-- 配置Spring监听器,可以在容器启动时,加载contextConfigLocation的context-param节点的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ================Spring配置结束================ --> <!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- 指定首页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 配置SESSION超时,单位是分钟 -->
<session-config>
<session-timeout>10</session-timeout>
</session-config> <!--如下此段编码设置必须在所有filter的前面,否则过滤器有可能不起作用。
设置编码方式为UTF-8,解决中文乱码问题。-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
UserManage-servlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.usermanage.controller"/> <!-- 配置处理映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 配置处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--指定默认前缀,controller中直接写文件名即可-->
<property name="prefix" value="/WEB-INF/content/jsp/"></property>
</bean> <!--指定单独处理的资源,不经过DispatcherServlet-->
<mvc:annotation-driven/> <!--所有以html结尾的请求全部到指定目录下查询-->
<mvc:resources mapping="/*.html" location="WEB-INF/content/html/"></mvc:resources>
<mvc:resources mapping="/*.js" location="WEB-INF/content/js/"></mvc:resources>
<mvc:resources mapping="/*.css" location="WEB-INF/content/css/"></mvc:resources>
<mvc:resources mapping="/login/*.css" location="WEB-INF/content/css/login/"></mvc:resources> </beans>
spring-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加载配置jdbc属性文件 -->
<!--MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,
PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了
修改如下:-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/config/jdbc.properties</value>
<!--要是有多个配置文件,只需在这里继续添加即可 -->
</list>
</property>
</bean> <!--配置c3p0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入属性-->
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean> <!--SqlSessionFactoryBean配置-->
<bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--Mybatis配置文件,非必须-->
<property name="configLocation" value="WEB-INF/config/mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
<!--扫描mapper.xml文件的位置-->
<property name="mapperLocations" value="classpath:org/usermanage/mapper/*.xml"/>
</bean> <!-- 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory,单个dataSource时可省略 -->
<!--<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"/>-->
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="org.usermanage.mapper"/>
</bean> <!--扫描所有使用注解的类型,使用Annotation自动注册Bean-->
<context:component-scan base-package="org.usermanage"/> <!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 使用annotation注解方式配置事务 -->
<!--<tx:annotation-driven transaction-manager="transactionManager"/>--> </beans>
mybatis-config.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>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/> <!-- 使用列别名替换列名 默认:true -->
<setting name="useColumnLabel" value="true"/> <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings> <!--从外部配置文件导入jdbc信息-->
<!--<properties resource="config/jdbc.properties"></properties>--> <!--<environments default="development">-->
<!--<environment id="development">-->
<!--<transactionManager type="JDBC"/>-->
<!--<dataSource type="POOLED">-->
<!--<property name="driver" value="${driver}"/>-->
<!--<property name="url" value="${url}"/>-->
<!--<property name="username" value="${username}"/>-->
<!--<property name="password" value="${password}"/>-->
<!--</dataSource>-->
<!--</environment>-->
<!--</environments>--> <!--指定映射资源文件-->
<!--<mappers>-->
<!--<mapper resource="classpath:org/usermanage/mapper/UserMapper.xml"/>-->
<!--</mappers>--> </configuration>
jdbc.properties
# jdbc连接信息
driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://192.168.184.130:3306/gxrdb
url=jdbc:mysql://10.15.1.200:3306/gxrdb?useUnicode=true&characterEncoding=utf8
username=root
password=root
SSM三大框架整合配置(Spring+SpringMVC+MyBatis)的更多相关文章
- SSM三大框架整合(Spring+SpringMVC+MyBatis)
一. 导包 18个必须的包 二.配置Spring MVC的web文件 <?xml version="1.0" encoding="UTF-8"?> ...
- 手动配置三大框架整合:Spring+Struts2+mybatis
如今主流的项目框架中,数据库持久层有可能不是hibernate,而是mybatis或者ibatis,事实上它们都是一样的,以下我来把环境搭建一下: [导入相关jar包]新建web项目projectms ...
- SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)
1.开发环境搭建以及创建Maven Web项目 参看之前的博文[确保maven web项目不报错]:http://www.cnblogs.com/cainiaomahua/p/6306476.html ...
- SSM框架的配置Spring+Springmvc +Mybatis
ssm框架是由spring mvc +spring+mybatis组成 快速阅读 通过spring的配置文件spring.xml,在servlet中指定spring mvc的配置文件spring-mv ...
- SSM框架整合(Spring + SpringMVC + MyBatis)
搭建环境 使用Spring(业务层)整合其他的框架SpringMVC(表现层)和MyBatis(持久层) Spring框架 创建数据库表 CREATE DATABASE ssm; USE ssm; C ...
- 04_SSM框架整合(Spring+SpringMVC+MyBatis)
[SSM的系统架构] [整合概述] 第一步: MyBatis和Spring整合,通过Spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在Spring中进行注册. 第二 ...
- SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql
准备工作: 下载整合所需的jar包 点击此处下载 使用MyBatis Generator生成dao接口.映射文件和实体类 如何生成 搭建过程: 先来看一下项目的 目录结构 1.配置dispatcher ...
- SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(Spring+SpringMVC+MyBatis) ]
SSM_BookSystem SSM框架基础 SSM_BookSystem ---> Hello CRUD 说明:本项目目前包含基础的CRUD 日期:2017-05-01 22:25:37 作者 ...
- SSM三大框架整合配置详解
首先,导入框架所需要的全部jar包(此处省略...........) 第一步:先从mybatis框架开始 我们只需要在mybatis的核心配置文件sqlConfigXml里写上这么一段话,代表的是给p ...
随机推荐
- centos7搭建elasticsearch
Elasticsearch:负责日志检索和分析,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等 Logstash:对日志进行收集.过 ...
- pacman详解及常见问题
安装软件包:软件包有很多可选依赖,是为软件提供额外功能, 安装软件时pacman 会输出可选依赖, 但不会在 pacman.log中,浏览安装软件的可选以来可用pacman -Si得到可选依赖的简短描 ...
- 稳压二极管&TVS二极管
稳压二极管 稳压二极管利用的特性是:二极管被反向击穿之后,随着电流的变化,稳压二极管两端电压维持不变的特性: 普通二极管反向击穿后就坏了,但是稳压二极管却可以恢复,而且在被击穿后处在正常的工作状态: ...
- MIUI7 系统应用精简(米5、红米note3)
1.由于安装的部分应用在root后无法使用,所以自己一直不能使用MIUI的开发版本. 2.前段时间米5升级MIUI8,实在是用着不咋地,耗电,王者还掉帧,于是降级miui7 3.被逼走上了刷机路. 1 ...
- python语言程序设计?
1, 别说了,我还是有几分蛋疼的.女朋友..计算机..唉 2, 今天把那几个练习写完吧? 3, 这个注释有啥用最前面的?? 4, 我在学完python后必须学完C和C++并开始离散数学和线代高数等全复 ...
- Java开源博客My-Blog之mysql容器重复初始化的严重bug修复过程
写在前面的话 <Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦> <Java开源博客My-Blog之docker容器组件化修改> ...
- 补充照片:某基同学使用Bing词典
某基同学使用Bing词典的照片
- 读书笔记(chapter4)
进程调度 4.1多任务 1.多任务系统可以划分为:非抢占式多任务和抢占式多任务: (在此模式下,由调度程序来决定什么时候停止一个进程的运行,以便其他进程能够得到执行机会,这个动作叫抢占: 时间片实际上 ...
- git心得与总结
任何文件在Git库中都有四种状态:未跟踪状态untracked.跟踪状态tracked(未修改状态unmodified.已修改状态modified.暂存状态staged),由于文件的上述四种状态,在使 ...
- 第三个Sprint冲刺第九天(燃尽图)