zoukankan      html  css  js  c++  java
  • Spring整合Mybatis

    1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
      (1)Spring配置文件:

    1. <!-- 引入jdbc配置文件 -->  
    2.      <context:property-placeholder location="jdbc.properties"/>  
    3.       <!--创建jdbc数据源 -->  
    4.       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
    5.         <property name="driverClassName" value="${driver}"/>  
    6.         <property name="url" value="${url}"/>  
    7.         <property name="username" value="${username}"/>  
    8.         <property name="password" value="${password}"/>  
    9.         <property name="initialSize" value="${initialSize}"/>  
    10.         <property name="maxActive" value="${maxActive}"/>  
    11.         <property name="maxIdle" value="${maxIdle}"/>  
    12.         <property name="minIdle" value="${minIdle}"/>  
    13.       </bean>  
    14.       <!-- 创建SqlSessionFactory,同时指定数据源-->  
    15.       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    16.       <property name="dataSource" ref="dataSource" />   
    17.       </bean>  
    18.       <!--创建数据映射器,数据映射器必须为接口-->  
    19.       <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
    20.       <property name="mapperInterface" value="com.xxt.ibatis.dbcp.dao.UserMapper" />  
    21.       <property name="sqlSessionFactory" ref="sqlSessionFactory" />   
    22.       </bean>  
    23.       <bean id="userDaoImpl2" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2">  
    24.       <property name="userMapper" ref="userMapper"/>  
    25.  </bean>  
    26. (2)数据映射器UserMapper,代码如下:
      1. public interface UserMapper {  
      2.       @Select("SELECT * FROM user WHERE id = #{userId}")   
      3.       User getUser(@Param("userId") long id);   
      4. }  
      5.  (3) dao接口类UserDao,代码如下:
        1. public interface UserDao {  
        2.     public User getUserById(User user);  
        3. }  
        4. (4)dao实现类UserDaoImpl2,,代码如下:
          1. public class UserDaoImpl2 implements UserDao {  
          2.        private UserMapper userMapper;  
          3.        public void setUserMapper(UserMapper userMapper) {   
          4.            this.userMapper = userMapper;   
          5.        }   
          6.        public User getUserById(User user) {  
          7.           return userMapper.getUser(user.getId());   
          8.        }  
          9.    }  

    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>

     

     

  • 相关阅读:
    svn和git的优缺点
    idea 的MAVEN Lifecycle 基本用法
    递归SQL---树形结构
    基本:linux命令
    2017年9月22日01:42:08
    简述数据库的设计过程
    HelloH5+搭建
    【Java报错】Message: 3 字节的 UTF-8 序列的字节 2 无效
    css class嵌套
    【java报错】Could not instantiate listener
  • 原文地址:https://www.cnblogs.com/CY001/p/7704787.html
Copyright © 2011-2022 走看看