mybatis与spring整合(方式二)

本文介绍了一种使用SqlSessionTemplate实现MyBatis与Spring框架整合的方法,详细展示了配置文件applicationContext.xml的具体设置,包括数据源配置、SqlSessionFactory及SqlSessionTemplate的创建过程,并通过BaseDao类和UserDao类的具体实现,讲解了如何在项目中应用这种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis与spring整合(方式一)
下面这种是使用了SqlSessionTemplate来实现

项目整体上只是在方式一上做了改动(实现方式变了),其余只有较小的调整

1、applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" 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/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.cwy"/>

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}" />
        <property name="jdbcUrl" value="${jdbcUrl}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${password}" />
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <property name="autoCommitOnClose" value="false"/>
        <property name="checkoutTimeout" value="10000"/>
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.cwy.entity"/>
        <!--  只是为了演示,因为没有更改默认配置,所以加不加无所谓
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        -->
        <property name="mapperLocations" value="classpath:com/cwy/dao/mapper/*.xml"/>
    </bean>

    <!-- 与方式一的不同点-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory" />
    </bean>
</beans>

jdbc.properties

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8
jdbc.username=cwy
password=151818

2、增加BseDao基类

package com.cwy;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by asus on 2017/7/13.
 */
public class BaseDao {
    @Autowired private SqlSessionTemplate template;

    public BaseDao() {
        System.out.println(template);
    }

    public <T> T findOne(String sqlId, Object params){
        return template.selectOne(sqlId,params);
    }
}

3、将UserDao接口改为实现类

package com.cwy.dao;

import com.cwy.BaseDao;
import com.cwy.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao extends BaseDao{
    /**
     * mybatis的mapper映射文件命名空间
     */
   private static final String MAPPER_NAMESPACE="com.cwy.dao.UserDao";

   public User getById(Long id){
       return findOne(MAPPER_NAMESPACE+".queryById",id);//注意这儿的“.”!!
   }
}

UserDao.xml(不变)

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cwy.dao.UserDao">

    <resultMap id="BaseResultMap" type="User">
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="name" property="name" jdbcType="VARCHAR" />
    </resultMap>

    <select id="queryById" resultType="User" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT *
        FROM user
        WHERE id=#{id}
    </select>

</mapper>

项目整体结构
这里写图片描述

4、遇到的bug

  • 注意加点和命名空间
    template.selectOne(sqlId,params);的sqld的写法为
    findOne(MAPPER_NAMESPACE+".queryById",id);
    注意两点:
    1、MAPPER_NAMESPACE就是mapper的xml文件中的<mapper namespace="com.cwy.dao.UserDao">
    2、是.queryById而不是queryById,否则会报IllegalArgumentException

  • jdbc.properties的书写顺序
    写成这样的时候一直报错

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8
password=151818
jdbc.username=cwy

错误信息

java.sql.SQLException: Connections could not be acquired from the underlying database!

后来把用户名提前至密码前即可!

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8
jdbc.username=cwy
password=151818
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值