一 .添加依赖
二.动态sql
1.直接在方法名上写sql(不推荐)
2.使用Provider
或者Provider中使用new SQL()编写
在mapper中调用
3.使用xml (推荐)
application.properties中配置xml路径:
#mybatis配置
mybatis.mapper-locations=classpath:com/study/springboot/mapper/*.xml
编写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">
<!-- 无法添加 useGenereatedKeys="true" keyProperty="id" keyColumn="id" -->
<mapper namespace="com.study.springboot.mapper.CatMapper" >
<resultMap id="baseRM" type="com.study.springboot.entity.Cat">
<id property="id" column="id"></id>
<result column="cat_name" property="catName"></result>
<result column="cat_age" property="catAge"></result>
</resultMap>
<insert id="saveWithXml">
insert into Cat(cat_name,cat_age)
values(#{catName},#{catAge})
</insert>
<delete id="deleteWithXml">
DELETE FROM Cat WHERE id=${id}
</delete>
<select id="selectAllWithXml" resultMap="baseRM">
SELECT * FROM Cat
<where>
<if test="cat_name!='' and cat_name!= null">
cat_name = #{catName}
</if>
</where>
</select>
<select id="selectAll" resultMap="baseRM">
SELECT * FROM Cat
<where>
<if test="list!= null">
cat_name in
<foreach collection="list" item="catName" open="(" separator="," close=")">
#{catName}
</foreach>
</if>
</where>
</select>
</mapper>
mapper添加对应方法
最后controller调用service调用mapper
ohter
xml语法
choose相当于java中的switch语法