今天在使用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.iyunv.com/blog/1725836
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com