- 浏览: 118670 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (70)
- mybatis (10)
- eclipse (3)
- 测试 (1)
- myEclipse+Maven (1)
- mysql (6)
- Spring (6)
- java (11)
- pdf (1)
- spring+mvc (4)
- jsp (7)
- sitemesh装饰器 (1)
- form (1)
- input标签 (1)
- UUID (1)
- request (1)
- jquery (2)
- EXCEL (1)
- smartupload (1)
- web.xml (1)
- Spring MVC (1)
- spring mvc注解 (1)
- SVN (1)
- log4j (1)
- Maven相关文档 (1)
- java路径 (1)
- ajax (1)
- js 模态窗口 (1)
- kindeditor (1)
- jstl (1)
最新评论
-
dai2jiang:
少了个逗号,后面
js提交表单kindeditor编辑器textarea为空解决办法 -
afeifqh:
好文!
Maven添加本地jar包 -
握着橄榄枝的人:
我导入<%@ taglib prefix="f ...
JSP中JSTL提供的函数标签EL表达式操作字符串的方法 -
myemptyname:
markmarkmarkmark
Spring3+mybatis+mysql整合详解(五) -
JUnique:
解释的还行吧。
表单form里的method属性post、get
下面来看Spring的配置:
1、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: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-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- DAO配置 -->
- <import resource="daoContext.xml" />
- <!-- IoC配置 -->
- <import resource="iocContext.xml" />
- <!-- MVC配置 -->
- <import resource="mvcContext.xml" />
- <!-- AOP配置 -->
- <import resource="aopContext.xml" />
- </beans>
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- DAO配置 --> <import resource="daoContext.xml" /> <!-- IoC配置 --> <import resource="iocContext.xml" /> <!-- MVC配置 --> <import resource="mvcContext.xml" /> <!-- AOP配置 --> <import resource="aopContext.xml" /> </beans>
2、daoContext.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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- 配置数据源 -->
- <bean id="DataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/myfristdb" />
- <property name="username" value="root" />
- <property name="password" value="admin" />
- </bean>
- <!-- Spring、MyBatis的整合,需要在 Spring 应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类(UserMapper->iocContext.xml)。 -->
- <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:SqlMapConfig.xml" />
- <property name="dataSource" ref="DataSource" />
- </bean>
- <!-- 配置事务管理器 -->
- <bean id="TransactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="DataSource" />
- </bean>
- </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置数据源 --> <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/myfristdb" /> <property name="username" value="root" /> <property name="password" value="admin" /> </bean> <!-- Spring、MyBatis的整合,需要在 Spring 应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类(UserMapper->iocContext.xml)。 --> <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <property name="dataSource" ref="DataSource" /> </bean> <!-- 配置事务管理器 --> <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="DataSource" /> </bean> </beans>
该部分为Mybatis和Spring3的整合配置,包括数据源、SqlSessionFactory及事务管理器的配置。
3、iocContext.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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- 数据映射器类 mapper bean -->
- <bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="sqlSessionFactory" ref="SqlSessionFactory" />
- <!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类 -->
- <property name="mapperInterface" value="com.hl.usersmanager.dao.IUserMapper" />
- </bean>
- <bean id="LoginMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="sqlSessionFactory" ref="SqlSessionFactory" />
- <property name="mapperInterface" value="com.hl.usersmanager.dao.ILoginMapper" />
- </bean>
- </beans>
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 数据映射器类 mapper bean --> <bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="SqlSessionFactory" /> <!-- 注意指定的映射器类必须是一个接口,而不是具体的实现类 --> <property name="mapperInterface" value="com.hl.usersmanager.dao.IUserMapper" /> </bean> <bean id="LoginMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="SqlSessionFactory" /> <property name="mapperInterface" value="com.hl.usersmanager.dao.ILoginMapper" /> </bean> </beans>
这部分是Dao层bean的配置。注意,这里bean的class并非直接指向dao层的类,而是指向org.mybatis.spring.mapper.MapperFactoryBean,并将dao层接口IUserMapper作为MapperFactoryBean的mapperInterface属性。
mybatis会自动帮我们创建一个代理类,执行IUserMapper里面的方法。也就是说,只要我们写好了mybatis的SqlMapConfig.xml,dao层只需要一个接口Mapper就行了,甚至连实现类都不要,因为myBatis自动帮我们完成。强大吧!
当然必须为MapperFactoryBean指定一个sqlSessionFactory,那就是前面我们在daoContext.xml中配置的SqlSessionFactory。
4、mvcContext.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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- 参考资料:http://code.google.com/p/bounding/wiki/SpringMVC3 -->
- <!-- 国际化配置 参考:http://hi.baidu.com/sonmeika/blog/item/8069b2dd7db1c9395882dd29.html
- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
- </bean> -->
- <!--配置视图解析器-->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/page/"/>
- <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
- <property name="viewClass">
- <value>org.springframework.web.servlet.view.InternalResourceView</value>
- </property>
- </bean>
- <!-- 注解支持 -->
- <mvc:annotation-driven/>
- <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
- <context:component-scan base-package="com.hl.usersmanager">
- <!-- 允许定义过滤器将基包下的某些类纳入或排除
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
- </context:component-scan>
- <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射
- <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->
- </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 参考资料:http://code.google.com/p/bounding/wiki/SpringMVC3 --> <!-- 国际化配置 参考:http://hi.baidu.com/sonmeika/blog/item/8069b2dd7db1c9395882dd29.html <bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"> </bean> --> <!--配置视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> </bean> <!-- 注解支持 --> <mvc:annotation-driven/> <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.hl.usersmanager"> <!-- 允许定义过滤器将基包下的某些类纳入或排除 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> </context:component-scan> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> --> </beans>
这部分是Spring mvc的配置。
viewResolver视图解析器是必须的。其属性prefix表示某个视图指向到那个路径,suffix属性表示视图的后缀(如果配置为jsp则表示该视图使用jsp页面进行渲染)。例如某个Controller返回一个login视图,根据当前的配置,表示该视图使用/WEB-INF/page/login.jsp进行渲染。这一点跟struts2有点像.
由于我们使用了注解的方式声明Controller,所以我们不再需要在Spring中配置Controller的bean。首先,开启注解支持,然后知道需要扫描注解的路径。
5、aopContext.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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- aop注解支持 -->
- <aop:aspectj-autoproxy/>
- <!--
- <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />-->
- <!-- 拦截器 -->
- <bean id="UserInterceptor" class="com.hl.usersmanager.controller.aop.UserInterceptor"></bean>
- </beans>
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- aop注解支持 --> <aop:aspectj-autoproxy/> <!-- <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />--> <!-- 拦截器 --> <bean id="UserInterceptor" class="com.hl.usersmanager.controller.aop.UserInterceptor"></bean> </beans>这里是Spring aop的配置。首先需要开启注解支持。.AnnotationAwareAspectJAutoProxyCreator可以自动完成aop 注解类的加载,但测试时发现跟spring mvc似乎有些冲突,没有测试成功。好吧,只能老老实实的把每个拦截器bean配置起来。
发表评论
-
Spring3 MVC 学习笔记(三)文件上传
2012-07-05 10:01 1451spring支持在网络应用程序处理文件上传,提供拔插的or ... -
Spring3+mybatis+mysql整合详解(五)
2012-06-19 11:21 2141下面来看java代码: 1、Control层代码: ... -
Spring3+mybatis+mysql整合详解(二)
2012-06-17 15:53 1408Spring 配置 下面来看web.xml的配置: ... -
Spring3+mybatis+mysql整合详解(一)
2012-06-17 15:52 1620项目所需要的jar包: Spring3: or ... -
Spring3+mybatis+mysql整合详解(四)
2012-06-17 15:47 1833使用Mybatis,你需要配置一个SqlMapConfig ...
相关推荐
NULL 博文链接:https://mgxy123.iteye.com/blog/1562364
NULL 博文链接:https://mgxy123.iteye.com/blog/1562361
Spring+SpringMVC+Mybatis+Maven+MySql项目框架 加分页,详解适合新手 初步学习搭建框架 maven 以及分页技术的应用
Spring+SpringMVC+Mybatis+Maven+MySql项目框架 加分页详解适合新手学习.zip
主要给大家介绍了关于利用Spring MVC+Mybatis实现Mysql分页数据查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
它由 MySQL 的原始开发者制作,并保证保持开源。它是大多数云产品的一部分,也是大多数Linux发行版的默认配置。MariaDB 被设计为 MySQL 的直接替代产品,具有更多功能,新的存储引擎,更少的错误和更好的性能。它...
本项目实用Spring + Spring MVC + Mybatis。数据库实用Mysql数据库 项目主要涉及,SSM框架的配置搭建,涉及Mybatis一对多的插入和查询,同时也涉及到一些简单的文件上传和下载.
主要介绍了Spring整合MyBatis(Maven+MySQL)图文教程详解的相关资料,需要的朋友可以参考下
基于现如今流行的SSM(spring springMVC mybatis mysql)框架的hrm人事管理系统后台实例 sql代码与详解:http://blog.csdn.net/csdn___lyy/article/details/72887390
主要介绍了Spring mvc整合mybatis(crud+分页插件)操作mysql的步骤详解,需要的朋友可以参考下
# Spring Boot 集成 MyBatis, 分页插件 PageHelper, 通用 Mapper ## 项目依赖 ```xml <!--mybatis--> <groupId>org.mybatis.spring.boot <artifactId>mybatis-spring-boot-starter <version>1.1.1 <!...
springboot配置文件模板:包括多环境开发布置,端口号配置,全局jackson配置,mysql数据库配置,oracle数据库配置,连接池配置,redis配置,redis链接池的配置,设置上传文件大小限制配置,全局格式化时间配置,https证书配置...
spring4.2.4+mybatis3.2.8+c3p0-0.9.1.2+Mysql5.6.24 二、c3p0的配置详解及spring+c3p0配置 1.配置详解 官方文档 : http://www.mchange.com/projects/c3p0/index.html <!--当连接池中的连接耗尽的时候c3p0一次...
springboot入门demo合集,springboot+mybatis+mysql实例,springboot文件上传下载,springboot拦截器等等关于springboot的基础demo都有,同步学习网站:https://www.moyundong.com/java/springboot/1介绍.html
3.使用 DriverManager.getConnection(url, userName, passWord) 创建数据库对象 4.创建执行Sql对象 connection.createStatement() 5.通过statement对象执行具体的Sql statement.executeQuery(sql) 6.最后关闭...
3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 ...
本资源包含Java面试题及详解,包含Java基础、JVM、多线程&并发、Spring、MyBatis、SpringBoot、MySQL、Spring Cloud、Dubbo、Nginx、MQ、Linux、ZooKeeper、Redis、分布式、网络、设计模式、ES等相关技术的常见面试...
Spring Boot 功能特性和组件系列的整合, 详解与使用。下面所有Demo都是基于 Spring Boot 2.0.x, 2.1.x, 2.2.x 版本。 : Blog: Github: Gitee: 数据库 本示例及所有子项目使用的数据库系统是MySQL, 连接的数据库...
通过实际测试对比了Spring Boot中6种MySQL批量更新方式的效率,并详细记录了每种方法在处理500,1000,5000,10000,50000,100000条数据的平均时间、最小时间和最大时间。包括MyBatis-Plus提供的批量更新,JdbcTemplate...
Java导出数据为excel表格,超级详细的,内容包含所需要的jar及所有相关文件,结合Mybatis+Spring+Mysql技术等技术内容丰富,步骤也比较详细,可以直接用到日常开发中使用,有不懂的地方可以留言