增:
<insert id="addUser" parameterType="org.javaboy.mybatis.model.User">
insert into user (username,address) values (#{username},#{address});
</insert>
也可以使用 UUID 做主键,使用 UUID 做主键。使用 MySQL 自带的 UUID 函数,整体思路是这样:首先调用 MySQL 中的 UUID 函数,获取到一个 UUID,然后,将这个 UUID 赋值给 User 对象的 ID 属性,然后再去执行 SQL 插入操作,再插入时使用这个 UUID。
<insert id="addUser2" parameterType="org.javaboy.mybatis.model.User">
<selectKey resultType="java.lang.String" keyProperty="id" order="BEFORE">
select uuid();
</selectKey>
insert into user (id,username,address) values (#{id},#{username},#{address});
</insert>
删:
<delete id="deleteUserById" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
改:
<update id="updateUser" parameterType="org.javaboy.mybatis.model.User">
update user set username = #{username} where id=#{id};
</update>
查:
<select id="getAllUser" resultType="org.javaboy.mybatis.model.User">
select * from user;
</select>
$和#:
在旧的 MyBatis 版本中,如果使用 $,变量需要通过 @Param 取别名,在最新的 MyBatis 中,无论是 # 还是 是拼接的,也就是查询时没有参数。而#相当于占位符,是带参数的形式
由于参数拼接的方式存在 SQL 注入的风险,因此我们使用较少
简单类型:
可以通过给参数添加 @Param 来指定参数名(一般在又多个参数的时候,需要加),一旦用 @Param 指定了参数类型之后,可以省略掉参数类型,就是在 xml 文件中,不用定义 parameterType 了
对象参数:
Integer addUser(User user);
<insert id="addUser" parameterType="org.javaboy.mybatis.model.User">
insert into user (username,address,favorites) values (#{username},#{address},#{favorites,typeHandler=org.javaboy.mybatis.typehandler.List2VarcharHandler});
</insert>
假如添加了@Param 注解
Integer addUser(@Param("user") User user);
<insert id="addUser" parameterType="org.javaboy.mybatis.model.User">
insert into user (username,address,favorites) values (#{user.username},#{user.address},#{user.favorites,typeHandler=org.javaboy.mybatis.typehandler.List2VarcharHandler});
</insert>
resutlMap:自己定义映射的结果集
<resultMap id="MyResultMap" type="org.javaboy.mybatis.model.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="address" property="address"/>
</resultMap>
id 用来描述主键,column 是数据库查询出来的列名,property 则是对象中的属性名。在查询结果中,定义返回值时使用这个 ResultMap
<select id="getUserById" resultMap="MyResultMap">
select * from user where id=#{id};
</select>
动态SQL:
<select id="getUserByPage" resultType="org.javaboy.mybatis.model.User">
select * from user
<if test="start !=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="getUserByUsernameAndId" resultType="org.javaboy.mybatis.model.User">
select * from user
<where>
<if test="id!=null">
and id>#{id}
</if>
<if test="name!=null">
and username like concat('%',#{name},'%')
</if>
</where>
</select>
<select id="getUserByIds" resultType="org.javaboy.mybatis.model.User">
select * from user where id in
<foreach collection="ids" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
</select>
大家知道,在 SQL 查询中,一般不建议写 *,因为 select * 会降低查询效率。但是,每次查询都要把字段名列出来,太麻烦。这种使用,我们可以利用 SQL 片段来解决这个问题。
<sql id="Base_Column">
id,usename,address
</sql>
<select id="getUserByIds" resultType="org.javaboy.mybatis.model.User">
select <include refid="Base_Column" /> from user where id in
<foreach collection="ids" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
</select>
set 关键字一般用在更新中。因为大部分情况下,更新的字段可能不确定,如果对象中存在该字段的值,就更新该字段,不存在,就不更新。
<update id="updateUser" parameterType="org.javaboy.mybatis.model.User">
update user
<set>
<if test="username!=null">
username = #{username},
</if>
<if test="address!=null">
address=#{address},
</if>
<if test="favorites!=null">
favorites=#{favorites},
</if>
</set>
where id=#{id};
</update>
一对一查询:
<mapper namespace="org.javaboy.mybatis.mapper.BookMapper">
<resultMap id="BookWithAuthor" type="org.javaboy.mybatis.model.Book">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="author" javaType="org.javaboy.mybatis.model.Author">
<id column="aid" property="id"/>
<result column="aname" property="name"/>
<result column="aage" property="age"/>
</association>
</resultMap>
<select id="getBookById" resultMap="BookWithAuthor">
SELECT b.*,a.`age` AS aage,a.`id` AS aid,a.`name` AS aname FROM book b,author a WHERE b.`aid`=a.`id` AND b.`id`=#{id}
</select>
</mapper>
或是
<mapper namespace="org.javaboy.mybatis.mapper.BookMapper">
<resultMap id="BaseResultMap" type="org.javaboy.mybatis.model.Book">
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
<resultMap id="BookWithAuthor" type="org.javaboy.mybatis.model.Book" extends="BaseResultMap">
<association property="author" javaType="org.javaboy.mybatis.model.Author">
<id column="aid" property="id"/>
<result column="aname" property="name"/>
<result column="aage" property="age"/>
</association>
</resultMap>
<select id="getBookById" resultMap="BookWithAuthor">
SELECT b.*,a.`age` AS aage,a.`id` AS aid,a.`name` AS aname FROM book b,author a WHERE b.`aid`=a.`id` AND b.`id`=#{id}
</select>
</mapper>
一对多查询:
<resultMap id="UserWithRole" type="org.javaboy.mybatis.model.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<collection property="roles" ofType="org.javaboy.mybatis.model.Role">
<id property="id" column="rid"/>
<result property="name" column="rname"/>
<result property="nameZh" column="rnameZH"/>
</collection>
</resultMap>
<select id="getUserById" resultMap="UserWithRole">
SELECT u.*,r.`id` AS rid,r.`name` AS rname,r.`nameZh` AS rnameZh FROM USER u,role r,user_role ur WHERE u.`id`=ur.`uid` AND ur.`rid`=r.`id` AND u.`id`=#{id}
</select>