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

首先讲一下作者自己的配置路线:
1.首先是找好那些我们需要用到的jar包

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以及第三方的数据源jar包
在这里插入图片描述
这是作者已经整理好的链接所需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

猜你喜欢

转载自blog.csdn.net/lovely__RR/article/details/89602565
今日推荐