mybatis与spring整合步骤以及自己遇到的错误

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: mybatis与spring整合步骤以及自己遇到的错误

首先讲一下作者自己的配置路线:

1.首先是找好那些我们需要用到的jar包


20190427151233610.png

20190427151303377.png

20190427151323372.png

20190427151353762.png

20190427151421368.png

以及第三方的数据源jar包

20190427151510661.png


这是作者已经整理好的链接所需jar包整合

2.创建一个实现类去继承我们之前写过的接口

如下

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.ssm1234.dao.CustomerMapper;
import cn.ssm1234.domain.Customer;
public class CustomerMapperimpl extends SqlSessionDaoSupport  implements CustomerMapper {
  public void saveCustomer(Customer customer) {
    // TODO Auto-generated method stub
    //这里不需要事物的提交`在这里插入代码片`
    SqlSession sqlSession=this.getSqlSession();
    sqlSession.insert("saveCustomer",customer);
  }
}

但是可以发现这里我们直接通过getSQLSession()获得了SQLSession对象,而之前我们都是通过sSqlSessionFactory对象从而获得的,所以我们等会需要去配置

2.其次实现类编写好了。我们接下来就是配置数据库的相关信息,以及其他相关的配置

我们创建applicationcontext.xml文件来配置

这里为了方便我们创建jdbc.properties文件,使得之后的数据库配置信息能够可以直接从该文件中读出


jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

之后在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-3.1.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       ">
       <!-- 读取数据库的相关配置文件 -->
       <context:property-placeholder location="classpath:config/jdbc.properties"/>
       <!-- 创建数据源DataSource -->
       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="url" value="${jdbc.url}"></property>
       <property name="driverClassName" value="${jdbc.driverClass}"></property>
       <property name="username" value="${jdbc.user}"></property>
       <property name="password" value="${jdbc.password}"></property>
       <!-- 最大连接数 -->
       <property name="maxActive" value="10"></property>
       <!-- 最大空闲数 -->
       <property name="maxIdle" value="5"></property>
       </bean>
       <!-- 创建一个SqlSessionFactory对象 -->
       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 关联连接池 -->
       <property name="dataSource" ref="dataSource"></property>
       <!--加载sql映射文件 -->
       <property name="mapperLocations" value="classpath:mapper/*.xml"></property>       
       </bean>
       <!-- 创建一个CustomerMapperImpl对象,然后向里面注入SQLSessionFactory的对象 -->
       <bean id="customerMapper" class="cn.ssm1234.dao.impl.CustomerMapperimpl">
       <!-- 关联或者输入sqlSeesionFactory -->
       <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
       </bean>
</beans>

3.我们就可以去编写测试类测试功能

import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ssm1234.dao.CustomerMapper;
import cn.ssm1234.domain.Customer;
public class MybatisSpringTest {
  @Test
  public void test() throws Exception
  {
    //加载Spring的配置
    ApplicationContext ac=new ClassPathXmlApplicationContext("config/applicationcontext.xml");
    //获取对象
    CustomerMapper customerMapper=(CustomerMapper)ac.getBean("customerMapper");
    Customer customer=new Customer();
      customer.setName("你ni");
      customer.setGender("男");
      customer.setTelephone("6666");
      customer.setAddres("北京");
    customerMapper.saveCustomer(customer);
  }
}

错误1

报错说是找不到applicationcontext.xml文件

这里主要是如果大家是直接放在src文件一下的话,那么直接写

ApplicationContext ac=new ClassPathXmlApplicationContext(“applicationcontext.xml”);即可

但是如果是在包裹着文件夹下的话,就需要写出相应的全路径,比如作者自己的:

ApplicationContext ac=new ClassPathXmlApplicationContext(“config/applicationcontext.xml”);

错误2

数据库连接出错

作者自己的错误是jdbc.driverClass=com.mysql.jdbc.Driver写成了jdbc.driverClass=com.mysql.jdbc.driver这东西是区分大小写的

错误3

bean的一个property的name属性不合法,应该是这个意思

<property name="mapperLocations" value=“classpath:mapper/*.xml”>

这里面的l必须要大写,具体我也不知道是为什么

错误4

还是路径的问题,说是找不到jdbc.properties文件

这里就需要理解另外一个classpath,这个本身也是从src文件往下找,如果之久就是没有包括文件就可以直接写classpath:。。。。。

但是如果上一层还包括了其他的文件,就需要写出具体的文件路径:

比如:classpath:config/jdbc.properties

错误5

版本的不匹配问题

一种是mybatis的版本过高,另外一种是mybatis-spring的版本过低,建议大家用mybatis3.4.4和mybatis-Spring1.3.0


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5天前
|
SQL Java 数据库连接
SpringBoot整合Mybatis
SpringBoot整合Mybatis
21 2
|
1月前
|
Java Spring 容器
Spring系列文章:Spring6集成MyBatis3.5
Spring系列文章:Spring6集成MyBatis3.5
|
1月前
SpringBoot+Mybatis-Plus+PageHelper分页+多条件查询
SpringBoot+Mybatis-Plus+PageHelper分页+多条件查询
29 0
|
18天前
|
运维 监控 安全
云HIS医疗管理系统源码——技术栈【SpringBoot+Angular+MySQL+MyBatis】
云HIS系统采用主流成熟技术,软件结构简洁、代码规范易阅读,SaaS应用,全浏览器访问前后端分离,多服务协同,服务可拆分,功能易扩展;支持多样化灵活配置,提取大量公共参数,无需修改代码即可满足不同客户需求;服务组织合理,功能高内聚,服务间通信简练。
32 4
|
1月前
|
SQL Java 数据库连接
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
|
5天前
|
Java 数据库连接 Spring
Spring 整合mybatis
Spring 整合mybatis
17 2
|
1天前
|
JSON Java 数据格式
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
|
9天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
29 4
|
12天前
|
XML Java 数据库连接
Spring Boot与MyBatis:整合与实战
【4月更文挑战第29天】在现代的Java Web应用开发中,持久化层框架扮演了至关重要的角色。MyBatis作为一款优秀的持久化框架,被广泛应用于Java开发中。Spring Boot提供了简化开发流程的功能,而与MyBatis的整合也变得更加便捷。
24 0
|
16天前
|
Java 数据库连接 数据库
spring+mybatis_编写一个简单的增删改查接口
spring+mybatis_编写一个简单的增删改查接口
16 2