在spring中使用mybatis

今天在使用mybatis的时候,发现dao的实现竟然可以不需要写一行代码,让我小小的震惊了一翻。

以下是截取mybatis官方文档中的一个小例子。比较好的说明了这个情况

 

需要的jar文件。 spring 3.0.5版本。 mybatis-3.0.5  mybatis-spring-1.0.1以及其依赖的包

 

 

package org.mybatis.jpetstore.domain;

import java.io.Serializable;

public class Category implements Serializable {

  private static final long serialVersionUID = 3992469837058393712L;

  private String categoryId;
  private String name;
  private String description;

  public String getCategoryId() {
    return categoryId;
  }

  public void setCategoryId(String categoryId) {
    this.categoryId = categoryId.trim();
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String toString() {
    return getCategoryId();
  }

}

 

 

注意,必须实现序列化,不然报java.io.NotSerializableException异常

 

service层web层暂时省略直接持久层吧

 

package org.mybatis.jpetstore.persistence;

import java.util.List;

import org.mybatis.jpetstore.domain.Category;

public interface CategoryMapper {

  List<Category> getCategoryList();

  Category getCategory(String categoryId);

}

 

这就是持久层的全部代码。只有一个接口而已。实现已经不必要再写了。完全有mybatis配置文件搞定

 

CategoryMapper.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.mybatis.jpetstore.persistence.CategoryMapper">

  <cache />
  
  <select id="getCategory" parameterType="string" resultType="Category">
    SELECT
      CATID AS categoryId,
      NAME,
      DESCN AS description
    FROM CATEGORY
    WHERE CATID = #{categoryId}
  </select>

  <select id="getCategoryList" resultType="Category">
    SELECT
      CATID AS categoryId,
      NAME,
      DESCN AS description
    FROM CATEGORY
  </select>

</mapper>

 其中id=getCategory表示调用的是getCategory(),parameterType表示参数的类型。这个是String类型。resultType表示的是返回值类型。会由mybatis自动封装。id=getCategoryList调用的是getCategoryList()。返回值Category。会被自动包装成List<Category>。

     没有什么查询,封装之类的东西。这就是持久层的全部代码。表示以前没见过。

 

接下来是applicationContext.xml中配置了。

最主要是下面两个配置:

 

 <!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
    </bean>
 

配置SqlSessionFactory。这是mybatis-spring-1.0.1提供的。而不是用spring里面的SqlMap....的包。

 

 

 <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.mybatis.jpetstore.persistence" />
    </bean>
 

这也是mybatis-spring-1.0.1提供的,用来对映射进行自动装配。

具体如何装配,可以参考源码,我也没有看代码。只是拿过来用。所以也不是十分清楚。不过挺值得研究的。

代码太清晰了。

 

首发于http://inmethetiger.iteye.com/blog/1725836

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值