先在新建的XML文件中写入如下内容:
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserInfoMapper">
mapper>
再在mapper标签里写入操作数据库的增删查改。
mapper层声明的方法为:
Integer insert(UserInfo userInfo);
XML文件中的实现为:
<insert id = "insert">
insert into
userinfo
(username, password, age, gender, phone)
values
(#{username}, #{password}, #{age}, #{gender}, #{phone})
insert>
mapper层声明的方法为:
Integer delete(Integer id);
XML文件中的实现为:
<delete id="delete">
delete from userinfo where id = #{id}
delete>
mapper层声明的方法为:
List<UserInfo> queryUserList();
XML文件中的实现为:
<select id="queryUserList" resultType="com.example.demo.model.UserInfo">
select * from userinfo
select>
mapper层声明的方法为:
Integer update(UserInfo userInfo);
XML文件中的实现为:
<update id="update">
update userinfo
set password = #{password}
where id = #{id}
update>
使用示例:
<update id = "updateBook">
update book_info
<set>
<if test = "bookName != null">
book_name = #{bookName},
if>
<if test = "author != null">
author = #{author},
if>
<if test = "count != null">
count = #{count},
if>
<if test = "price != null">
price = #{price},
if>
<if test = "publish != null">
publish = #{publish},
if>
<if test = "status != null">
status = #{status},
if>
set>
where id = #{id}
update>
如果满足bookName!=null这个条件,则会显示if标签里的内容。
使用示例:
<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
insert into
userinfo
<trim prefixOverrides="," prefix="(" suffix=")" suffixOverrides=",">
<if test="username!=null">
username,
if>
<if test="password!=null">
password,
if>
<if test="age!=null">
age,
if>
<if test="gender!=null">
gender,
if>
<if test="phone!=null">
phone,
if>
trim>
values
<trim prefixOverrides="," prefix="(" suffix=")" suffixOverrides=",">
<if test="username!=null">
#{username},
if>
<if test="password!=null">
#{password},
if>
<if test="age!=null">
#{age},
if>
<if test="gender!=null">
#{gender},
if>
<if test="phone!=null">
#{phone},
if>
trim>
insert>
使用示例:
<select id="queryUserByWhere" resultType="com.yixing.mybatis.model.UserInfo">
select * from userinfo
<where>
<if test="userName!=null">
username= #{userName}
if>
<if test="age!=null">
and age=#{age}
if>
where>
select>
where标签的作用是删除代码块最前面的and;当查询条件为空时,会去掉where关键字。
使用示例:
<update id="update2">
update userinfo
<set>
<if test="username!=null">
username = #{username},
if>
<if test="password!=null">
password = #{password},
if>
<if test="age!=null">
age = #{age}
if>
set>
where id = #{id}
update>
set标签会删除代码块最后面的逗号。
使用示例:
<update id="batchDelete">
update book_info
set `status` = 0
where id in
<foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
foreach>
update>
默认情况下,如果mapper层声明方法的参数是List类型,则foreach标签里的collection会等于"list";如果mapper层声明方法的参数是数组类型,则foreach标签里的collection会等于"array",这时mybatis自动做的。我们可以在mapper层声明方法中用@Param注解对声明方法的参数进行重命名。
<sql id="cols">
id, username,password,gender,age,phone,
sql>
<select id="queryUserList" resultType="com.yixing.mybatis.model.UserInfo">
select
<include refid="cols">include>
delete_flag,
create_time,
update_time
from userinfo
select>
我们可以将XML中重复出现的内容提取出来放到sql标签中,当需要用到sql标签中的内容时,用include标签将sql标签中的内容引进来即可。