springmvc和mybatis的整合
来源:互联网 发布:java web接口测试工具 编辑:程序博客网 时间:2024/05/16 00:02
~~~~~~~~~~~~~~~~~~~~~~~增加内容~~~~~~~~~~~~~~~~~~~~~~~~
经过再次学习研究,发现可以不用先分别搭好在整合,可以直接搭好springmvc的框架,然后直接在springmvc的配置文件springmvc-config.xml上集成mybatis。就是下边的第二步和第三步,只是第三步中代码不用指定全局配置文件。此外在项目中对mybatis的使用和以前所写的mybatis的配置教程里面的步骤一样。
~~~~~~~~~~~~~~~~~~~~~~增加完毕~~~~~~~~~~~~~~~~~~~~~~~~~~
学过springmvc和m它们mybatis的配置之后,整合它们就很简单了。
springMVC的配置
mybatis的配置
首先,我们springmvc的配置文件是springmvc-config.xml,mybatis的配置文件是mybatis.xml.
我们要知道整合这两个框架所能达到的效果是什么?
我们使用mybatis的时候对数据库的操作是通过**mapper.xml文件来实现的,几乎对每一张表得操作都需要建一个mapper.xml文件,创建好的mapper.xml文件要配置到mybatis.xml里面去才能够起作用。
<!-- 加载映射文件 --> <mappers> <mapper resource="com/guozi/Mapper/UserMapper.xml"/> </mappers>
如果把mybatis整合到Springmvc中去,就是会实现自动配置mapper.xml到mybatis。也就是说省去了我们收到把mapper.xml配置到mybatis.xml的过程。
整合步骤:
1.先按上边的教程把springmvc和mybatis配置好并测试配置成功
2.在WEB-INF/lib包里加上jar包;
3.在springmvc的配置文件springmvc-config.xml中添加以下代码:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="1023894102" /> </bean>
<!-- mybatis文件,这里配置好了就不用在dao层开关sql工厂了 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置数据源--><property name="dataSource" ref="dataSource" /><!-- 指定全局配置文件 --> <property name="configLocation" value="classpath:mybatis.xml" /><!-- 扫描到mapper 映射文件 --><property name="mapperLocations" value="classpath:com/guozi/Mapper/*.xml" /></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"><constructor-arg ref="sqlSessionFactory" /></bean>
注:其中两个classpath的地方要注意一个是mybatis.xml的路径,一个是放置mapper.xml的路径,要写规范4.把mybatis.xml里面的上面所说的加载映射文件的配置和数据库连接信息删除或者注释掉,也就是这段代码:
<environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理,事务控制由mybatis管理--> <transactionManager type="JDBC" /> <!-- 数据库连接池,由mybatis管理--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="1023894102" /> </dataSource> </environment> </environments>
<!-- 加载映射文件 --> <mappers> <mapper resource="com/guozi/Mapper/UserMapper.xml"/> </mappers>
5.测试,继续运行我们配置mybatis时的那个UserService.class文件mybatis配置教程
运行成功,说明已经整合成功了
0 0
- MyBatis和SpringMVC的整合
- springmvc和mybatis的整合
- SpringMVC和Mybatis的整合
- mybatis和springMVC的整合
- springmvc和mybatis整合
- springmvc和mybatis整合
- mybatis和springmvc整合
- springMVC和mybatis整合
- Springmvc和mybatis整合
- Springmvc和mybatis整合
- Springmvc和mybatis整合
- SpringMVC和mybatis框架的简单整合
- springmvc和mybatis整合出现的问题
- Springmvc、Spring和mybatis的整合
- 【SpringMVC整合MyBatis】springmvc和struts2的区别
- 【SSM-SpringMVC框架】SpringMVC和MyBatis的整合
- 【SpringMVC学习04】Spring、MyBatis和SpringMVC的整合
- SpringMVC框架学习(2)--SpringMVC和Mybatis的整合
- JS数组方法汇总 array数组元素的添加和删除
- 电脑启动 状态 0xc00000e9
- PAT A1103. Integer Factorization (30)
- Leetcode-338. Counting Bits
- 一个uv对应多个贴图
- springmvc和mybatis的整合
- hibernate的三种状态
- 生物电(ECG、EMG、EEG)科普研究
- Mac上安装Homebrew 以及 MongoDB(附win安装教程)
- Webpack
- 5-8 单词长度 (15分)
- [bzoj4573][UOJ#195][ZJOI2016]大森林
- 由浅入深分布式(5)dubbo提供者用内网地址注册provider以及 spring boot admin client用主机名注册spring boot admin server
- 架构设计:负载均衡层设计方案(3)——Nginx进阶