这篇文章主要是说明了Spring3和MyBatis的整合方法,之所以要整合就是将MyBatis创建factory等各种繁冗的工作以及对session的管理、事务的管理交由spring容器帮我们管理,省心省力。
这个项目是在我的前几篇文章的基础上加入spring的内容,关于MyBatis的项目如何建立请查阅我之前的关于MyBatis的帖子。
整个的项目结构如下:
- 先把包都导入吧,下面是所有需要的包,spring的包最恶心,共计需要18个包,而且从spring官网下载的zip里面仅仅有spring的jar包,引用的其他相关的包没有,好不容易终于凑齐了,全部都在最后面的示例项目中
- 建立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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置c3p0数据源 -->
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="30" />
<property name="acquireIncrement" value="10" />
<property name="maxIdleTime" value="10" />
<property name="maxStatements" value="0" />
</bean>
<!-- 配置SessionFactory,使用上面的c3p0数据源作为数据库连接 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="c3p0DataSource"/>
</bean>
<!-- 声明式事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0DataSource" />
</bean>
<!-- 配置Mapper,相当于dao -->
<bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.qiuqiu.dao.PersonMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
上面的配置文件我就不多说了,一个是数据源,连接数据库用的,第二个是让Spring管理session用的,第三个是声明式事务,最后一个就是dao了。
有一点必须注意,interface、mapper file、mapper namespace这三个必须对应(并不是完全一样,而是对应),正确的写法如下:
interface:org.qiuqiu.dao.PersonMapper
mapper file:org/qiuqiu/dao/PersonMapper.xml
mapper namespace:org.qiuqiu.dao.PersonMapper
这三个必须对应,否则会报以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ...
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:594)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:436)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:428)
at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:188)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:51)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:37)
at $Proxy5.selectByExample(Unknown Source)
3.测试类,如下:
package org.qiuqiu.test;
import java.util.List;
import org.qiuqiu.dao.PersonMapper;
import org.qiuqiu.vo.Person;
import org.qiuqiu.vo.PersonExample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonMapper pm = ctx.getBean("personMapper", PersonMapper.class);
PersonExample example = new PersonExample();
example.createCriteria().andAgeLessThan(10);
List<Person> list = pm.selectByExample(example);
for (Person p : list) {
System.out.println(p.getName() + " " + p.getAge() + " "+ p.getPassword());
}
}
}
附件中会包含所有的jar包以及数据库表
- 大小: 16.7 KB
- 大小: 12 KB
分享到:
相关推荐
Spring+SpringMVC+Mybatis框架整合例子——亲测可用.zip
Spring整合MyBatis完整实例 最新 +详细注释 标准的MVC模式 包含详细注释
本实例是mybatis与spring的整合实例,代码一目了然,很容易看懂
spring整合mybatis实例,可以通过test1类测试
struts2_mybatis_spring_框架整合实例_数据库 源码中包括数据库文件 文档介绍
spring+netty+mybatis整合实例 实现一个简单的socket服务,启动服务后,客户端发送一条消息,服务端从数据库查询数据并返回给客户端。
spring mvc 整合mybatis实例,spring mvc 整合mybatis实例
使用springmvc spring mybatis 3.2 实现了整合。
Spring+SpringMvc+Mybatis整合实例模板代码分享
小弟最近研究mybatis,写了个mybatis与spring4MVC结合使用的简单实例,仅供各位有兴趣的同行参考,DAO层的封装正在研究,不日上传
《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练...
springboot与mybatis整合实例,页面使用thymeleaf,数据库建表请参照UserMapper.xml,欢迎分享交流
SpringBoot整合Mybatis,可直接运行实例,并附有整合说明文档
这是一个简单的mybatis-spring整合的实例代码,使用maven进行管理,可以直接导入运行,给初学者一个参考
spring+springMVC+mybatis+maven整合实例
mybatis基于maven的整合spring的入门实例
【Spring+MyBatis+MySQL实战入门】一、MyBatis操作入门的源码
spring mvc 和 mybatis整合的实例。对登录的一个小的实现
spring3.0mvc自学教程ppt+注解教程+注解实例+springmybatis教程+项目实例+中文api 初学(自学)着的不二法定,从入门到项目实例深入学习(浅-深)