详细参考开发包中的开发向导文档。
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>调用方法:
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