SpringMVC和Mybatis的整合(配置部分)
来源:互联网 发布:男女打架差距 知乎 编辑:程序博客网 时间:2024/06/05 09:11
项目目录视图:
1.整合Dao层
Mybatis所需的配置文件: sqlMapConfig.xml。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 批量扫描别名 --> <typeAliases> <package name="ccom.video.po"/> </typeAliases></configuration>
2、配置数据源、事务管理,配置SqlSessionFactory、mapper扫描器。
applicationContext-Dao.xml的配置:
<!-- 加载配置文件 --><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 "> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.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="30"/> <property name="maxIdle" value="5"/> --> </bean> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> </bean> <!-- mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.video.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean></beans>
3、配置Service
<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 "><!-- 商品管理的service --><bean id="hello" class="com.video.Controller.hello"/> </beans>
4、事物控制的配置(该部分暂时省略)
5.整合springmvc.xml
<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 "> <mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package="com.video.Controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="" /> <property name="suffix" value=".jsp" /> </bean></beans>
6.配置web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc</display-name> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 解决post乱码 --> <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> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping></web-app>
这里我们用一个小例子测试运行配置是否出错。
package com.video.Controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class hello { @RequestMapping("/hello.action") public void test(){ System.out.println("hello"); }}
启动项目并运行,结果如下:
在控制台看到有输出:
0 0
- SpringMVC和Mybatis的整合(配置部分)
- spring和mybatis的整合配置(包括springmvc)
- MyBatis和SpringMVC的整合
- springmvc和mybatis的整合
- SpringMVC和Mybatis的整合
- mybatis和springMVC的整合
- spring mybatis springmvc框架整合配置(配置的总结和含义)
- SpringMVC框架学习(2)--SpringMVC和Mybatis的整合
- springmvc和mybatis整合关键配置
- springMVC+Mybatis整合配置
- springmvc(五)springmvc和mybatis整合
- 我的SpringMVC+mybatis整合配置
- springmvc+spring+mybatis整合的配置
- springmvc+mybatis整合下的日志配置
- springmvc和mybatis整合
- springmvc和mybatis整合
- mybatis和springmvc整合
- springMVC和mybatis整合
- 344. Reverse String
- RCNN学习笔记(7):Faster R-CNN 英文论文翻译笔记
- C与C++中关于声明与定义概念理解
- 算法导论 第二十三章 最小生成树
- 方法的递归算法
- SpringMVC和Mybatis的整合(配置部分)
- [整理]Android屏幕适配(不同的屏幕分辨率和尺寸)
- Windows下MYSQL的安装与配置
- 分割 (大数的分割) 字符串的灵活使用
- HDU-1003 Max Sum (dp)
- Android进阶之路 - Handler的详细使用(一)
- javaScript实现焦点轮播图界面效果(三)
- Android Studio Mac版与Win版快捷键对比
- RMQ