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

    一、Dao层接口

    1、在Dao文件夹下面建立SeckillDao文件,代码如下:

    public interface SeckillDao {
        //秒杀减库存
        int updateSeckillById(@Param("Id") Integer Id,@Param("killtime") Date killtime);
    
        //查询单条信息
        Seckill qurryById(Integer Id);
    
        //查询所有秒杀列表
        List<Seckill> qurryAll(@Param("offet") Integer offet,@Param("Limit") Integer Limit);
      
    //按照条件 联合Salary表查询   List<Map<String, Object>> queryAuthorAndSalary(@Param("where1") Integer where1, @Param("where2") Integer where2); }

    二、基于myBatis实现Dao层接口的编程

    1、在resourse - mapper - 创建SeckillDao.xml

    2、id属性的值对应 Dao接口里面的方法

    3、“<=” 号 会引发错误  需要转换  "<![CDATA[ <= ]]>"

    4、#{值}是Dao接口里面方法的参数

    <?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="com.example.seckill.Dao.SeckillDao">
        <update id="updateSeckillById">
            update
              seckill
            set
              number=number-1
            where Id=#{Id}
            and start_time <![CDATA[ <= ]]> #{killtime}
            and end_time>=#{killtime}
            and number>0;
        </update>
    
        <select id="qurryById" resultType="Seckill" parameterType="Integer">
              select Id,name,number,start_time,end_time,create_time
              from seckill
              where Id=#{Id};
        </select>
    
        <select id="qurryAll" resultType="Seckill">
              select Id,name,number,start_time,end_time,create_time
              from seckill
              order by create_time desc
              limit #{offet},#{Limit};
        </select>
    
        <select id="queryAuthorAndSalary" resultType="java.util.Map">
            select * from
            t_author as a INNER join t_salary s on a.o_id=s.id
            <if test="where1 ==1">
                AND a.o_id =3
            </if>
            <if test="where1 ==2">
                AND s.welfare like'五险%'
            </if>
        </select>
    
    </mapper>

    三、myBatis整合Spring编程

    1、在 resourses - spring - 创建 spring-Dao.xml 文件

    2、具体代码注释如下

    <?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">
    <!-- 整合MyBatis过程-->
    
        <!-- 1:配置数据库相关连接参数-->
        <context:property-placeholder location="classpath:jdbc.properties" />
        <!-- 2:数据库连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 配置连接池属性-->
            <property name="driverClass" value="${driver}" />
            <property name="jdbcUrl" value="${url}" />
            <property name="user" value="${user}" />
            <property name="password" value="${password}" />
    
            <!-- c3p0连接池私有属性-->
            <property name="maxPoolSize" value="30" />
            <property name="minPoolSize" value="10"/>
            <!-- 关闭连接后不自动conmit-->
            <property name="autoCommitOnClose" value="false"/>
            <!-- 获取连接超时时间-->
            <property name="checkoutTimeout" value="1000"/>
            <!-- 当获取连接失败重试次数-->
            <property name="acquireRetryAttempts" value="2" />
        </bean>
    
        <!-- 3:配置sqlSessionFactory对象-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 注入数据库连接池  上面的bean-->
            <property name="dataSource" ref="dataSource"/>
            <!-- 配置MyBatis全局配置文件mybatis-config.xml-->
            <property name="configLocation" value="classpath:mybatis-config"/>
            <!-- 扫描entity包  使用别名-->
            <property name="typeAliasesPackage" value="com.example.seckill.Entity"/>
            <!-- 扫描sql配置文件:mapper需要的xml文件-->
            <property name="mapperLocations" value="classpath:mapper/*"/>
        </bean>
    
        <!-- 4:配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 注入sqlSessionFactory-->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
            <!-- 给出需要扫描Dao接口包-->
            <property name="basePackage" value="com.example.seckill.Dao"/>
        </bean>
    
    </beans>

    四、测试及结果

    1、直接调用Dao接口方法

        @Test
        public void qurryById() {
            Integer Id=3;
            Seckill seckill = seckillDao.qurryById(Id);
            System.out.println(seckill.getName());
        }

    Spring boot整合MyBatis

    在配置文件application.yml添加

    mybatis:
      type-aliases-package: com.panshi.ProjectMyBatis.Entity
      mapper-locations: classpath:mapper/*.xml
  • 相关阅读:
    对 String 的几个错误认识 X
    用C# 自定义Window7的JumpList(跳转列表) X
    IPv6无状态自动配置功能配合DHCPv6无状态配置功能 实现IPv6自动分配
    H3C S7500E IPV6白皮书
    静默方式执行chkdsk命令
    IPv6基本知识(转载)
    解决win7官方主题themepack无法安装的问题
    英保通等PXE网刻软件的使用
    通过命令提示符修改windows默认打印机
    OFFICE2010出现两个激活信息的处理办法。
  • 原文地址:https://www.cnblogs.com/qiujianfeng/p/10552432.html
Copyright © 2011-2022 走看看