xxxMapper.xml

来源:互联网 发布:js 图片旋转 编辑:程序博客网 时间:2024/06/04 19:48

详细参考开发包中的开发向导文档。

BlogMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespce是查找SQL映射文件的唯一标识,必须唯一 --><mapper namespace="org.mybatis.example.BlogMapper"><!-- id是SQL映射文件中方法的唯一标识 --><select id="selectBlog" resultType="Blog">select * from Blog where id = #{id}</select></mapper>
调用方法:

namespace+id方式,只需要SQL映射文件

Blog blog = session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);

mapper类方法调用方式,只需要接口,并在接口方法上写注解和SQL语句

public interface BlogMapper {@Select(select * from blog where id=#{id})public Blog getBlog();@Insert(insert into blog(name,age) values(#{name},#{age}))public void addBlog(Blog blog);@Delete(delete from  blog where id=#{id})public void deleteBlog(int id);@Update(update blog set name=#{name},age=#{age} where id=#{id})public void updateBlog(Blog blog);}

也需要再mybatis-config.xml的<mappers>中注册:

<mappers><mapper class="cn.fuzhou.mapper.BlogMapper"></mapper></mappers>

BlogMapper mapper = session.getMapper(BlogMapper.class);Blog blog = mapper.selectBlog(101);


0 0