This article mainly introduces three common integration methods between Spring and Mybatis. The required integration framework package is mybatis-spring.jar, which can be accessed through the link
Download from http://code.google.com/p/mybatis/.
1. Using the data mapper (MapperFactoryBean) method, there is no need to write mybatis mapping files, and the corresponding sql statements and input parameters are provided using annotations.
(1) Spring configuration file:
<!--Introduce jdbc configuration file--> <context:property-placeholder location="jdbc.properties"/> <!--Create jdbc data source--> <bean id="dataSource" destroy-method="close "> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <property name="initialSize" value="${initialSize}"/> <property name="maxActive " value="${maxActive}"/> <property name="maxIdle" value="${maxIdle}"/> <property name="minIdle" value="${minIdle}"/> </bean> <!--Create SqlSessionFactory and specify the data source--> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> </bean> <!--Create data Mapper, the data mapper must be an interface --> <bean id="userMapper"> <property name="mapperInterface" value="com.xxt.ibatis.dbcp.dao.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="userDaoImpl2"> <property name="userMapper" ref="userMapper"/> </bean>(2) Data mapper UserMapper, the code is as follows:
public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{userId}") User getUser(@Param("userId") long id); }(3) dao interface class UserDao, the code is as follows:
public interface UserDao { public User getUserById(User user); }(4) dao implementation class UserDaoImpl2, the code is as follows:
public class UserDaoImpl2 implements UserDao { private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public User getUserById(User user) { return userMapper.getUser(user.getId()); } }2. Use the implementation class org.mybatis.spring.SqlSessionTemplate of the interface org.apache.ibatis.session.SqlSession.
In mybatis, sessionFactory can be created by SqlSessionFactoryBuilder.
In MyBatis-Spring, SqlSessionFactoryBean is used instead.
SqlSessionFactoryBean has a required attribute dataSource, and it also has a common attribute configLocation (used to specify the xml configuration file path of mybatis).
(1) Spring configuration file:
<!-- Create SqlSessionFactory and specify the data source -->
<bean id="sqlSessionFactory">
<property name="dataSource" ref="dataSource" />
<!-- Specify the sqlMapConfig overall configuration file, and the customized environment will no longer take effect in the spring container -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
<!--Specify the entity class mapping file. You can specify a certain package and all configuration files under the sub-package at the same time. Only one mapperLocations and configLocation are required. When you need to specify an alias for the entity class, you can specify the configLocation attribute, and then Use mapper to introduce entity class mapping files in the mybatis general configuration file -->
<!- - <property name="mapperLocations" value="classpath*:com/xxt/ibatis/dbcp/**/*.xml"/> -->
<bean>
(2)mybatis total configuration file sqlMapConfig.xml:
<configuration> <typeAliases> <typeAlias type="com.xxt.ibatis.dbcp.domain.User" alias="User" /> </typeAliases> <mappers> <mapper resource="com/xxt/ibatis/dbcp/ domain/user.map.xml" /> </mappers> </configuration>
(3) Entity class mapping file user.map.xml:
<mapper namespace="com.xxt.ibatis.dbcp.domain.User"> <resultMap type="User" id="userMap"> <id property="id" column="id" /> <result property=" name" column="name" /> <result property="password" column="password" /> <result property="createTime" column="createtime" /> </resultMap> <select id="getUser" parameterType="User" resultMap="userMap"> select * from user where id = #{id} </select> <mapper/>(4) dao layer interface implementation class UserDaoImpl:
Java code
public class UserDaoImpl implements UserDao { public SqlSessionTemplate sqlSession; public User getUserById(User user) { return (User)sqlSession.selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user); } public void setSqlSession( SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } }3. Use the abstract class org.mybatis.spring.support.SqlSessionDaoSupport to provide SqlSession.
(1) spring configuration file:
Java code
<bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <!-- <property name="mapperLocations " value="classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"/ > --> </bean> <bean id="sqlSession" > <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <bean id="userDaoImpl3"> <!--Inject SqlSessionTemplate instance--> <property name= "sqlSessionTemplate" ref="sqlSession" /> <!--You can also directly inject the SqlSessionFactory instance. When both are specified, SqlSessionFactory becomes invalid--> <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> --> </bean>
(2) dao layer interface implementation class UserDaoImpl3:
Java code
public class UserDaoImpl3 extends SqlSessionDaoSupport implements UserDao { public User getUserById(User user) { return (User) getSqlSession().selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user); } }