MyBatis查询
MyBatis主要方法:
1.编写接口方法:Mapper接口
eg:List
2.编写SQL语句:
SQL映射文件eg:
点击查看代码
<select id="selectAll" resultType="Brand">
select *
from tb_brand;
</select>
1)查询所有:
点击查看代码
<!--
id:完成主键映射
column:表的列名
property:实体类的属性名
result:完成一般映射
column:表的列名
property:实体类的属性名
-->
<result column="brand_name" property="brandName"/>
<result column="compant_name" property="companyName"/>
</resultMap>
<!-- <sql id="huhu">-->
<!-- id, brand_name as brandName, company_name as companyName, ordered, description, status-->
<!-- </sql>-->
<!-- <select id="selectAll" resultType="brand">-->
<!-- select *-->
<!-- from tb_brand;-->
<!-- </select>-->
<!-- <select id="selectAll" resultType="brand">-->
<!-- select-->
<!-- <include refid="huhu"/>-->
<!-- from tb_brand;-->
<!-- </select>-->
<select id="selectAll" resultMap="brandResultMap">
select
*
from tb_brand;
</select>
点击查看代码
<!--
* 参数占位符
1.#{}:替换为? 防止SQL注入
2.${}:拼sql 存在SQL注入
3.使用时机
* 参数传递——#{}
*表名或列名不固定——${}
* 参数类型:parameterType:可以省略
* 特殊字符处理:
1.转义字符:< ___ <
2.CDATA
<![CDATA[
]]>
-->
<select id="selectById" parameterType="int" resultMap="brandResultMap">
select *
from tb_brand where id <![CDATA[
]]> = #{id};
</select>
点击查看代码
/**
* 条件查询
* * 参数接受
* 1.散装参数:如果有多个参数,需要使用@Param("SQL参数占位符名称")
* 2.对象参数:对象属性名称要与参数占位符名称一致
* 3.map合集参数
*
* @param status
* @param companyName
* @param brandName
* @return
*/
// List<Brand> selectByCondition(@Param("status")int status,
// @Param("companyName")String companyName,
// @Param("brandName")String brandName);
List<Brand> selectByCondition(Brand brand);
List<Brand> selectByCondition(Map map);
//SQL语句:
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
where status = #{status}
and company_name like #{companyName}
and brand_name like #{brandName}
</select>
点击查看代码
<!-- 动态条件查询
*if 条件查询
*test 逻辑表达式
*问题:
*恒等式
*<where>替换where
-->
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status! = null">
status = #{status}
</if>
<if test="companyName !=null and companyName !=' ' ">
and company_name like #{companyName}
</if>
<if test="brandName !=null and brandName!= ' ' ">
and brand_name like #{brandName}
</if>
</where>
</select>
点击查看代码
<!-- <select id="selectByConditionSingle" resultMap="brandResultMap">-->
<!-- select *-->
<!-- from tb_brand-->
<!-- where -->
<!-- <choose> <!– 相当于switch–>-->
<!-- <when test="status!=null"><!–相当与case–>-->
<!-- status =#{status}-->
<!-- </when>-->
<!-- <when test="companyName != null and companyName != ''"><!–相当与case–>-->
<!-- company_name like #{companyName}-->
<!-- </when>-->
<!-- <when test="brandName != null and brandName != '' "><!–相当与case–>-->
<!-- brand_name like #{brandName}-->
<!-- </when>-->
<!-- <otherwise>-->
<!-- 1=1-->
<!-- </otherwise>-->
<!-- </choose>-->
<!-- </select>-->
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
<where>
<choose> <!-- 相当于switch-->
<when test="status!=null"><!--相当与case-->
status =#{status}
</when>
<when test="companyName != null and companyName != ''"><!--相当与case-->
company_name like #{companyName}
</when>
<when test="brandName != null and brandName != '' "><!--相当与case-->
brand_name like #{brandName}
</when>
</choose>
</where>
</select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具