zoukankan      html  css  js  c++  java
  • Spring + myBatis 配置文件演进过程

    一、配置单个Dao实现:

    mapper/productMapper.xml:

    <?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">

    <!-- namespace:该映射文件的名字,理论上可以任意取 -->
    <mapper namespace="org.mybatis.example.BlogMapper">

    <insert id="ss" parameterType="com.day11.ssm.spring.mybatis.model.Product" >
    INSERT INTO product (product_name, sale_price) VALUES (#{productName},#{salePrice})
    </insert>

    </mapper>


    spring-config.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"
    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">

    <!-- 读取配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置datasources -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/station_demo"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>

    <!-- 整合mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 属性-->
    <!-- 映射文件地址 -->
    <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
    </bean>

    <!-- ProduuctDao -->
    <bean id="productDao" class="com.day11.ssm.spring.mybatis.dao.impl.ProductDao">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

    </beans>
    二、配置单个Dao接口(不需要写具体的实现类):

    mapper/productMapper.xml:

    <?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">

    <!-- namespace:该映射文件的名字,理论上可以任意取 -->
    <mapper namespace="com.day11.ssm.spring.mybatis.dao.IProductDao">

    <insert id="save" parameterType="com.day11.ssm.spring.mybatis.model.Product" >
    INSERT INTO product (product_name, sale_price) VALUES (#{productName},#{salePrice})
    </insert>

    </mapper>

    spring-config.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"
    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">

    <!-- 读取配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置datasources -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/station_demo"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>

    <!-- 整合mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 属性-->
    <!-- 映射文件地址 -->
    <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
    </bean>

    <!-- ProduuctDao -->
    <!-- <bean id="productDao" class="com.day11.ssm.spring.mybatis.dao.impl.ProductDao">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean> -->

    <!-- ProduuctDao -->
    <bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    <property name="mapperInterface" value="com.day11.ssm.spring.mybatis.dao.IProductDao"/>
    </bean>

    </beans>

    三、配置扫描包(多个Dao接口),此为生产上的终极配置方式

    mapper/productMapper.xml:

    <?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">

    <!-- namespace:该映射文件的名字,理论上可以任意取 -->
    <mapper namespace="com.day11.ssm.spring.mybatis.dao.IProductDao">

    <insert id="save" parameterType="com.day11.ssm.spring.mybatis.model.Product" >
    INSERT INTO product (product_name, sale_price) VALUES (#{productName},#{salePrice})
    </insert>

    </mapper>

    spring-config.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"
    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">

    <!-- 读取配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置datasources -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/station_demo"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>

    <!-- 整合mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 属性-->
    <!-- 映射文件地址 -->
    <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
    </bean>

    <!-- ProduuctDao -->
    <!-- <bean id="productDao" class="com.day11.ssm.spring.mybatis.dao.impl.ProductDao">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean> -->

    <!-- ProduuctDao -->
    <!-- <bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    <property name="mapperInterface" value="com.day11.ssm.spring.mybatis.dao.IProductDao"/>
    </bean> -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <property name="basePackage" value="com.day11.ssm.spring.mybatis.dao"/>
    </bean>

    </beans>





    逆天改命!我命由我不由天!
  • 相关阅读:
    puremvc 入门与思考
    PureMVC QA:Where Should I Declare Notification Name Constants
    PureMVC QA:Why can't Proxies hear Notifications?
    EPUB CFI 规范
    Flex中添加大量组件时内存占用问题
    保垒机SSH登录脚本
    用JAVA给JSON进行排版
    基于ProGuard-Maven-Plugin的自定义代码混淆插件
    Protostuff自定义序列化(Delegate)解析
    Web服务图片压缩,nginx+lua生成缩略图
  • 原文地址:https://www.cnblogs.com/huhewei/p/13889583.html
Copyright © 2011-2022 走看看