网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
06月07日漏签0天
mybatis吧 关注:3,093贴子:5,392
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 2回复贴,共1页
<<返回mybatis吧
>0< 加载中...

spring整合mybatis的一些疑问

  • 只看楼主
  • 收藏

  • 回复
  • J小浩子
  • 一年级
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
首先,两个可以整合,测试是成功的。在这里要问的是整合中的问题
在mybatis与spring整合中的spring配置文件: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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mydb1?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.springmybatis.dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
然后是UserMapper.java文件:
package com.springmybatis.dao;
import com.springmybatis.po.User;
public interface UserMapper {
public User getUser(Integer id);
public void addUser(User user);
public void updateUser(User user);
public void deleteUser(Integer id);
}
问题来了:
1.在ApplicationContext.xml中,有一段:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.springmybatis.dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
可是UserMapper.Java并没有mapperInterface 和 sqlSessionFactory属性,也没有注解,那么这两个属性尤其是mapperInterface,
为什么一改动mapperInterface,就会报错?
2.<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
在这一段中配置sqlSessionFactory,而下面的userMapper直接ref到sqlSessionFactory,如果有很多userMapper,那不是会映射很多
sqlSessionFactory,而sqlSessionFactory又是重量级的,最好是单例模式。
比如hibernate的可以如下配置:
<bean id="loginServiceImpl" class="com.hsp.Impl.loginServiceImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="loginAction" class="com.hsp.web.action.loginAction" scope="prototype">
<property name="loginServiceInter" ref="loginServiceImpl"/>
</bean>
这里通过继承可以保持sessionFactory为单例,所以请问mybayis该如何配置?
3.<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mydb1?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
这段配置连接池的配置文件没有指出最大连接数什么的,下面看看hibernate的连接池:
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sjjg?useUnicode=true&amp;characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值,但经过一个高峰时间后,连接池会将不用的连接释放一部分 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值,空闲时连接池会申请连接,以免高峰来临时造成连接时间长 -->
<property name="minIdle" value="1"/>
</bean>
这里明确指出连接的细节,mybatis是如何配置的?
4.先看看spring的ApplicationContext.xml文件对hibernate的配置:
事务:
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
但是在mybatis中是怎样体现事务的?


  • qhitly
  • 六年级
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 2回复贴,共1页
<<返回mybatis吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示