目录
后端在使用mybatis的时候,与sql语句也是常常打照面的,这里小编经过了一个项目,现在总结一下遇到过的sql语句吧。
1、查询表的全部信息(获取全部信息)
<select id="findAllCat" resultType="com.test5.test.entity.Cat">
select * from cat;
</select>
id为该sql语句的名字(可以自己设置),resultType为查询之后返回给前端的数据类型。
2、根据某一个字段查找整行的数据
<select id="findCatPopularity" parameterType="java.lang.String" resultType="com.test5.test.entity.Cat">
select * from cat where name = #{name}
</select>
pararmeterType为传递给sql语句的数据类型
3、查询数据限制表的行数(适用于分页查询)
<select id="queryCatInformation" resultType="com.test5.test.entity.Cat" parameterType="com.test5.test.entity.Paging">
select * from cat limit #{start_number},#{line_number};
</select>
从第start_number条开始向下数line_number条数据传递给前端
4、模糊查询字段
<select id="queryByKeyWord" resultType="com.test5.test.entity.Cat" parameterType="java.lang.String">
select * from cat where name like CONCAT('%',#{name,jdbcType=VARCHAR},'%')
</select>
这里使用的方法需要改变cat为你自己的表名,两个地方的name换成你需要模糊查询的字段,然后根据你自己的需求去修改传入参数的数据类型和传出参数的数据类型即可
5、使用数组进行查询(同一个字段多个值查询)
<select id="getPecificCat" parameterType="com.test5.test.entity.CatPecific" resultType="com.test5.test.entity.CatDetails">
select * from catDetails
<where>
<if test="coat_color != null">
coat_color IN
<foreach collection="coat_color" item="value" open="(" close=")" separator=",">
#{value}
</foreach>
</if>
<if test="coat_color != null and school_place != null">
and
</if>
<if test="school_place != null">
school_place IN
<foreach collection="school_place" item="value" open="(" close=")" separator=",">
#{value}
</foreach>
</if>
<if test="(school_place != null and sex != null) or (coat_color != null and sex != null)">
and
</if>
<if test="sex != null">
sex IN
<foreach collection="sex" item="value" open="(" close=")" separator=",">
#{value}
</foreach>
</if>
</where>
</select>
在mybatis的sql语句中我们可以使用where、if、foreach标签,foreach标签则是主要针对于数组使用的,在使用的时候修改coat_color、school_place、sex这些字段即可,if标签之中的test可以理解为if语句的判断条件,这个要根据具体的情况进行修改
6、某个固定字段的改变(数字自增、数据上线下线)
<update id="updatePopularity" parameterType="java.lang.String">
update cat set popularity = popularity + 1 where name=#{name};
</update>
<update id="updatePassed" parameterType="com.test5.test.entity.InformationUpVO">
update informationUp set pass = "passed",judge = #{judge} where id = #{id}
</update>
7、插入数据
<insert id="addCatStory" parameterType="com.test5.test.entity.CatStory">
INSERT INTO catStory(cat_name,cat_story,uploader)
VALUES (#{cat_name},#{cat_story},#{uploader})
</insert>
插入数据的sql语句,要求必须是一一对应的,不能偏差,写上去的字段在插入的时候也可以不传给sql语句,不会出现报错
8、动态更新数据库字段
<update id="updateCatStory">
update catStory set
<trim suffixOverrides=",">
<if test="cat_story!=null and cat_story!=''">
cat_story=#{cat_story},
</if>
<if test="cat_name!=null and cat_name!=''">
cat_name=#{cat_name},
</if>
<if test="uploader!=null and uploader!=''">
uploader=#{uploader},
</if>
</trim>
where story_id=#{story_id}
</update>
其中trim标签中的suffixOverrides=","的意思是删除掉最后一个逗号,避免sql语句报错说语法错误
9、根据某个字段删除行数据
<delete id="deleteCatStroy">
delete from catStory where cat_name=#{name}
</delete>
10、批量插入数据
<insert id="batchInsert" parameterType="list">
insert into mmall_order_item (id, name, password) values
<foreach collection="List" item="item" index="index" separator=",">
(#{item.id},#{item.name},#{item.password} )
</foreach>
</insert>
11、自定义插入某个表
<update id="updateObjectVersion" parameterType="com.xx.xx">
UPDATE ${dataCode} set OBJECT_VERSION_NUMBER=#{objectVersionNumber}
<where>
<if test="codeId != null">
CODE_ID = #{codeId}
</if>
<if test="codeValueId != null">
AND CODE_VALUE_ID = #{codeValueId}
</if>
<if test="productId != null">
AND PRODUCT_ID = #{productId}
</if>
<if test="propertyId != null">
AND PROPERTY_ID = #{propertyId}
</if>
<if test="cmdId != null">
AND CMD_ID = #{cmdId}
</if>
<if test="paramId != null">
AND PARAM_ID = #{paramId}
</if>
<if test="templateId != null">
AND TEMPLATE_ID = #{templateId}
</if>
</where>
</update>
12、sql语句中的关键词(sql语句关键词不区分大小写)
- SELECT:用于从数据库中选择数据。
- FROM:指定要查询的表或视图的名称。
- WHERE:用于对查询结果进行条件筛选。
- GROUP BY:按照指定的列对结果进行分组。
- HAVING:对GROUP BY子句分组后的结果进行条件筛选。
- ORDER BY:指定结果集的排序方式。
- JOIN:用于将多个表连接在一起。
- INSERT INTO:用于向数据库插入新的记录。
- UPDATE:用于更新表中的记录。
- DELETE FROM:用于从表中删除记录。
- CREATE TABLE:用于创建新表。
- ALTER TABLE:用于修改现有表的结构。
- DROP TABLE:用于删除表。
- INDEX:用于创建索引以提高查询性能。
- UNIQUE:确保某个列中的值是唯一的。
- PRIMARY KEY:定义表的主键。
- FOREIGN KEY:定义表之间的关系。
- DISTINCT:用于返回唯一不同的值。
- AS:用于为列或表指定别名。
- UNION:用于合并两个或多个SELECT语句的结果集。
13、总结
这些就是小编自己在写项目的时候遇到的sql语句了,还结合了一些别的地方找到的资料,哦,对了,各位在使用sql语句的时候,数据库的字段千万不要是关键词,不然sql语句会报错的,之前小编遇到这个问题,找了半天错误的,那今天的分享就到这里了,各位我们下期再见咯!