`
QiuQiu0034
  • 浏览: 121793 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

Mybatis入门实例(五)——MyBatis与Spring 3.X的整合

阅读更多

这篇文章主要是说明了Spring3和MyBatis的整合方法,之所以要整合就是将MyBatis创建factory等各种繁冗的工作以及对session的管理、事务的管理交由spring容器帮我们管理,省心省力。

这个项目是在我的前几篇文章的基础上加入spring的内容,关于MyBatis的项目如何建立请查阅我之前的关于MyBatis的帖子。

 

整个的项目结构如下:

  1. 先把包都导入吧,下面是所有需要的包,spring的包最恶心,共计需要18个包,而且从spring官网下载的zip里面仅仅有spring的jar包,引用的其他相关的包没有,好不容易终于凑齐了,全部都在最后面的示例项目中
  2. 建立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
2
0
分享到:
评论
6 楼 胡哥123456 2014-01-08  
能不能把ibatis下写的sql语句的方法;各种情况都总结一下啊
5 楼 xiegaolong 2012-06-25  
狂顶,好博文,支持下
4 楼 yanhao_unique 2012-05-17  
  顶起来
3 楼 QiuQiu0034 2012-03-29  
where条件从句,自动判断各种条件,方便查询或者更新的时候复用条件语句
hycx227 写道
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" suffix=")" prefixOverrides="and">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
                </when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
                </when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and
#{criterion.secondValue}
                </when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem"
open="(" close=")" separator=",">
#{listItem}
                  </foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<!--
WARNING - @mbggenerated This element is automatically generated by
MyBatis Generator, do not modify. This element was generated on Thu
Sep 01 10:08:21 CST 2011.
-->
<where>
<foreach collection="example.oredCriteria" item="criteria"
separator="or">
<if test="criteria.valid">
<trim prefix="(" suffix=")" prefixOverrides="and">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
                </when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
                </when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and
#{criterion.secondValue}
                </when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem"
open="(" close=")" separator=",">
#{listItem}
                  </foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
这两段是什么意思?

2 楼 hycx227 2012-03-29  
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" suffix=")" prefixOverrides="and">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
                </when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
                </when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and
#{criterion.secondValue}
                </when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem"
open="(" close=")" separator=",">
#{listItem}
                  </foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<!--
WARNING - @mbggenerated This element is automatically generated by
MyBatis Generator, do not modify. This element was generated on Thu
Sep 01 10:08:21 CST 2011.
-->
<where>
<foreach collection="example.oredCriteria" item="criteria"
separator="or">
<if test="criteria.valid">
<trim prefix="(" suffix=")" prefixOverrides="and">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
                </when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
                </when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and
#{criterion.secondValue}
                </when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem"
open="(" close=")" separator=",">
#{listItem}
                  </foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
这两段是什么意思?
1 楼 zhulin10541 2011-12-11  
顶起啊!! 相当贴心的博客~ 感觉这些包如果使用maven管理可能会方便些吧

相关推荐

Global site tag (gtag.js) - Google Analytics