用来干什么?
好处?
如何使用?
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.daos.UserMapper">
<resultMap id="userMap" type="Customer">
<result column="pwd" property="password"/>
resultMap>
<select id="getUserList" resultMap="userMap">
SELECT * FROM mybatis.user
select>
<select id="getUserListByRowBounds" resultMap="userMap">
SELECT * FROM mybatis.user
select>
<select id="getUserListForFuzzyQuery" resultType="org.example.pojo.User">
SELECT * FROM mybatis.user where name like "%"#{name}
select>
<select id="getUserById" resultType="org.example.pojo.User">
SELECT * FROM mybatis.user where id = #{id}
select>
<insert id="addUser" parameterType="org.example.pojo.User">
INSERT INTO mybatis.user(id, name, pwd) values (#{id},#{name},#{pwd})
insert>
<update id="updateUserByUser" parameterType="org.example.pojo.User">
UPDATE mybatis.user set name=#{name}, pwd=#{pwd} where id=#{id}
update>
<update id="updateUserByMap" parameterType="map">
UPDATE mybatis.user set name=#{userName}, pwd=#{userPwd} where id=#{userId}
update>
<delete id="deleteUser" parameterType="int">
DELETE FROM mybatis.user where id=#{id}
delete>
mapper>
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.daos.StudentMapper">
<select id="getStudentList" resultMap="StudentTeacher2">
select s.id sid, s.name sname, t.id tid, t.name tname from student s, teacher t where s.tid = t.id
select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<id column="id" property="tid"/>
<result property="name" column="tname"/>
association>
resultMap>
mapper>
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.daos.TeacherMapper">
<select id="getTeacherById" resultMap="TeacherStudent">
select t.id tid, t.name tname, s.id sid, s.name sname from teacher t, student s where t.id = s.tid and t.id=#{tid}
select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="studentList" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
collection>
resultMap>
<select id="getTeacherById2" resultMap="TeacherStudent2">
select * from teacher where id=#{tid}
select>
<resultMap id="TeacherStudent2" type="Teacher">
<result column="id" property="id"/>
<collection property="studentList" column="id" ofType="Student" select="getStudentByTid"/>
resultMap>
<select id="getStudentByTid" resultType="Student">
select * from student where tid=#{tid}
select>
mapper>
<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
insert>
<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
foreach>
insert>
<update id="updateAuthor">
update Author set
username = #{username},
password = #{password},
email = #{email},
bio = #{bio}
where id = #{id}
update>
<delete id="deleteAuthor">
delete from Author where id = #{id}
delete>
<sql id="if_title_author">
<if test="title!= null">
AND title = #{title}
if>
<if test="author != null">
AND author = #{author}
if>
sql>
<select id="queryBlog1" parameterType="map">
select * from blog where 1=1
<include refid="if_title_author"/>
select>
<sql id="someinclude">
from
<include refid="${include_target}"/>
sql>
<select id="select" resultType="map">
select
field1, field2, field3
<include refid="someinclude">
<property name="prefix" value="Some"/>
include>
select>
#{average,javaType=double,jdbcType=NUMERIC,typeHandler=MyTypeHandler,numericScale=2}
@Select("select * from user where ${column} = #{value}")
User findByColumn(@Param("column") String column, @Param("value") String value);
association 联表查询
<association property="author" column="blog_author_id" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
</association>
association 子表查询
<resultMap id="blogResult" type="Blog">
<association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>
collection子表查询
<collection property="posts" column="id" ofType="Post" select="selectPostsForBlog"/>
collection联表查询
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
</collection>
</resultMap>
<collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
可以读作: “posts 是一个存储 Post 的 ArrayList 集合” 。且在一般情况下,MyBatis 可以推断 javaType 属性,因此并不需要填写。
缓存Cache:
, LRU, FIFO,开启二级缓存,需要在对于mapper上,加入标签元素
即可动态Sql:解决在定义Sql映射时,拼接sql语句:where子句条件,SET子句,多条语句foreach的编写。参考链接
分页:limit
selectList (String statement, Object parameter, RowBounds rowBounds)
)注解开发: 参考链接