首先,两个可以整合,测试是成功的。在这里要问的是整合中的问题
在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&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&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&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中是怎样体现事务的?
在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&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&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&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中是怎样体现事务的?