javaweb—SpringMVC+Spring+MyBatis整合
来源:互联网 发布:刻录软件怎么用 编辑:程序博客网 时间:2024/04/27 22:30
SpringMVC+Spring+MyBatis整合:
#1.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">
<display-name>ShareZone</display-name>
<!-- Servlet编码过滤器 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置Spring的用于初始化容器对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- SpringMVC文件配置 -->
<!--核心SpringMVC核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvc-servlet.xml</param-value>
</init-param>
<!--启动顺序,让这个Servlet随Servlet容器一起启动,必须放在配置的最后 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--拦截所以的请求-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
#2.applicationContext.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:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--开启注解扫描与装配bean -->
<context:component-scan base-package="com.sharezone.web.dao"></context:component-scan>
<context:component-scan base-package="com.sharezone.web.entity"></context:component-scan>
<context:component-scan base-package="com.sharezone.web.service"></context:component-scan>
<!--加载参数配置文件 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置C3P0连接池,目的:管理数据库连接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--数据库连接配置 -->
<property name="driverClass" value="${sqlserverDriver}"/>
<property name="jdbcUrl" value="${sqlserverUrl}"/>
<property name="user" value="${sqlserverUsername}"/>
<property name="password" value="${sqlserverPassword}"/>
<!--其他配置 -->
<property name="minPoolSize" value="1"></property>
<property name="maxPoolSize" value="30"></property>
<property name="maxIdleTime" value="1800"></property>
<property name="acquireIncrement" value="5"></property>
<property name="initialPoolSize" value="5"></property>
<property name="idleConnectionTestPeriod" value="1800"></property>
<property name="breakAfterAcquireFailure" value="true"></property>
<property name="testConnectionOnCheckout" value="false"></property>
</bean>
<!--配置SqlSessionFactoryBean目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- sqlSessionTemplate单例模式,线程安全,使用DefailtSqlSession的时候都从SqlSessionFactory
当中获取一个不能确保线程安全-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--配置事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注解方式实现事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
#3.SpringMvc-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd " >
<!--默认的注解映射的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--如果当前请求为“/”时,则转发到“/XXX/index” -->
<mvc:view-controller path="/" view-name="forward:/home/index"/>
<!--静态资源映射:防止被拦截 -->
<mvc:resources mapping="/Js/**" location="/Js/" />
<mvc:resources mapping="/css/**" location="/Css/" />
<mvc:resources mapping="/fonts/**" location="/Fonts/" />
<mvc:resources mapping="/plugins/**" location="/Plugins/" />
<mvc:resources mapping="/images/**" location="/Images/" />
<!--当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 ,
Spring会先执行上面的,如果没有匹配的,最后在执行这个默认的servlet处理器-->
<mvc:default-servlet-handler/>
<!--开启controller注解支持 -->
<!-- use-default-filters="false"只扫描指定的注解,防止事务不起作用 -->
<context:component-scan base-package="com.sharezone.web.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/View/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--解析返回JSON -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list >
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!--文件上传配置: SpringMVC上传文件时,需要配置MultipartResolver处理器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--指定所上传文件的总大小不能超过5M-->
<property name="maxUploadSize" value="5242880"/>
</bean>
</beans>
#4.mybatis.cfg.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>
<!--设置类型别名:更换所有的type类型 -->
<typeAliases>
//……
</typeAliases>
<!--加载映射文件 -->
<mappers>
//……
</mappers>
</configuration>
附件db.properties
sqlserverDriver=com.microsoft.sqlserver.jdbc.SQLServerDriver
sqlserverUrl=jdbc\:sqlserver\://127.0.0.1\:1433;DatabaseName=ShareZone
sqlserverUsername=sa
sqlserverPassword=123456
mysqlDriver=com.mysql.jdbc.Driver
mysqlUrl=jdbc:mysql://localhost:3306/ShareZone
mysqlUsername=root
mysqlPassword=root
这样基本整合就结束了!
- javaweb—SpringMVC+Spring+MyBatis整合
- JavaWeb——springMVC、mybatis与spring的整合
- Spring-SpringMVC-Mybatis整合
- springmvc+spring+mybatis整合
- springmvc+spring+mybatis整合
- Spring+SpringMVC+Mybatis整合
- springmvc+mybatis+spring整合
- springmvc+spring+mybatis 整合
- mybatis+springmvc+spring 整合
- SpringMVC+Spring+Mybatis整合
- springmvc+spring+mybatis 整合
- springmvc+spring+mybatis整合
- Spring+Springmvc+MyBatis整合
- Spring+Springmvc+MyBatis整合
- Spring+Springmvc+MyBatis整合
- springmvc+mybatis+spring 整合
- springmvc+mybatis+spring 整合
- spring+springMVC+Mybatis整合
- 利用 BASE64Encoder 对字符串进行加密 BASE64Decoder进行解密
- Coin Change
- Java 面试题汇总 《个人从各种看过的面试宝典里面集合过来的》
- Android RecyclerView 完全解析
- MySQL 基于存储过程 实现数据统计按日、周、月份统计模板
- javaweb—SpringMVC+Spring+MyBatis整合
- 查看Linux版本系统信息方法汇总
- Java合并数组的两个子有序段
- 最长公共子序列(易理解)
- 面试的笔试题!
- RocketMQ消费者,设置setConsumeFromWhere无效的问题
- 网络信息安全学习平台---脚本关第15题
- Android 日期转为时间戳
- 了解ARM处理器