xxxMapper.xml

前端之家收集整理的这篇文章主要介绍了xxxMapper.xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

BlogMapper.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">
<!-- 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);
原文链接:https://www.f2er.com/xml/295118.html

猜你在找的XML相关文章