1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)Spring配置文件:
- <!-- 引入jdbc配置文件 -->
- <context:property-placeholder location="jdbc.properties"/>
- <!--创建jdbc数据源 -->
- <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>
- <!-- 创建SqlSessionFactory,同时指定数据源-->
- <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的配置文件
<?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: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">
<mvc:annotation-driven />
<!-- 配置加载资源文件包括js,css,image -->
<mvc:default-servlet-handler />
<context:component-scan
base-package="circulation.controller">
</context:component-scan>
<!-- 配置spring的mvc的ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix">
<value>.jsp</value>
</property>
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
</bean>
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:config.properties"/>
<bean class="circulation.model.ReceiptDetailModel" />
<bean class="circulation.service.PurchaseService" />
<bean class="circulation.service.UserService" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close" autowire="no">
<!-- <property name="fair
Queue" value="false" />
<property name="minIdle" value="1" />
<property name="maxIdle" value="5" />
<property name="maxActive" value="5" />
<property name="initialSize" value="1" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="select 1" />
<property name="validationInterval" value="500000" />5min
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="30" /> -->
<property name="driverClassName" value="${database_driver}" />
<property name="url" value="${database_url}" />
<property name="username" value="${user}" />
<property name="password" value="${pass}" />
</bean>
<!-- 配制mybatis的SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--mapperLocations对应的是mapper的配置文件所在的路径-->
<property name="mapperLocations" value="classpath:circulation/mapper/*.xml" />
</bean>
<!-- 配置mybatis的MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage对应的是接口所在的目录-->
<property name="basePackage" value="circulation.data" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
2.接口的定义
package circulation.data;
import org.springframework.stereotype.Repository;
import java.util.Map;
/**
*
* */
@Repository
public interface PurchaseMapper {
public int insertPurchaseOrder(Map<String,Object> map);
}
3.mapper的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="circulation.data.PurchaseMapper">
<insert id="insertPurchaseOrder">
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
<foreach collection="list" item="em" separator=",">
(#{head.purchaseId},#{em.productId},
#{em.productCount},#{em.receiptNote})
</foreach>
delete from purchase_detail_tab
where purchase_product = ''
and purchase_id = #{head.purchaseId};
</insert>
</mapper>