1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)Spring配置文件:
- <context:property-placeholder location="jdbc.properties"/>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- bean>
- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="com.xxt.ibatis.dbcp.dao.UserMapper" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- bean>
- <bean id="userDaoImpl2" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2">
- <property name="userMapper" ref="userMapper"/>
- bean>
- (2)数据映射器UserMapper,代码如下:
-
- public interface UserMapper {
- @Select("SELECT * FROM user WHERE id = #{userId}")
- User getUser(@Param("userId") long id);
- }
- (3) dao接口类UserDao,代码如下:
-
- public interface UserDao {
- public User getUserById(User user);
- }
- (4)dao实现类UserDaoImpl2,,代码如下:
-
- 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());
- }
- }
1.环境的配置
在spring和mybatis的整合的过程中是不需要mybatis的配置文件,只需要spring和maper的配置文件
在spring的配置文件里面需要配置spring的基本配置和mybatis需要的数据源和org.mybatis.spring.SqlSessionFactoryBean以及org.mybatis.spring.mapper.MapperScannerConfigurer的配置
2.使用mapper来使用mybatis
在spring和mybatis的整合之后,可以使用mapper的方式来使用mybatis
使用mapper的方式也即只写接口没有接口的实现
在使用mapper的方式来使用的时候需要在mapper的配置文件里面配置namespace属性,namespace属性和接口的全称是一致的,在mapper配置文件里面的操作是和接口中的方法是一致的
3.定义接口和配置mapper
使用mapper方式来使用mybatis的时候需要定义接口,所有的操作是通过mapper的配置来实现的
4.使用时需要注意到事项
在使用的时候对于的pojo需要实现java.io.Serializable接口
备注
1.spring的配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> base-package="circulation.controller"> destroy-method="close" autowire="no">
2.接口的定义
package circulation.data;
import org.springframework.stereotype.Repository;
import java.util.Map;
/**
*
* */
@Repository
public interface PurchaseMapper {
public int insertPurchaseOrder(Map
}
3.mapper的配置
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into purchase_head_tab(
purchase_id,purchase_supplier,
purchase_inventory,purchase_money,purchase_data,
purchase_note,purchase_type)
values(#{head.purchaseId},#{head.supplierId},#{head.inventoryId},
#{head.moneys},#{head.purchaseDate},#{head.purchaseNote},
#{head.purchaseType});
insert into purchase_detail_tab(purchase_id,
purchase_product,purchase_count,purchase_note)
values
(#{head.purchaseId},#{em.productId},
#{em.productCount},#{em.receiptNote})
delete from purchase_detail_tab
where purchase_product = ''
and purchase_id = #{head.purchaseId};