欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

mybatis入门示例代码,mybatis示例代码,今天在使用mybatis

来源: javaer 分享于  点击 27800 次 点评:126

mybatis入门示例代码,mybatis示例代码,今天在使用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提供的,用来对映射进行自动装配。

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

代码太清晰了。

相关栏目:

用户点评