时间:2021-07-01 10:21:17 帮助过:27人阅读
1 每个语句的结束记得加分号;
2where条件里再做if分支 SELECT *FROM `table` WHERE IF( `parentID` is null, `plan_id` <10, `plan_id` >500 )3 is null, is not null 用于判断某个字段或是变量为null或不为null.
4 isnull(expr) 的用法:
如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0。
5 ifnull(exp1,exp2)如果exp1是null的话,就用exp2的值,否则还是用exp1的值
6 NULLIF(expr1,expr2) 如果expr1 = expr2 成立,那么返回值为NULL,否则返回值为 expr1
7 out参数,存储过程的参数要指明in,out。在调用存储过程时,用带@开头的变量来接值,比如 call procedure1(1,1,@result);
8 用户变量不用声明,直接在变量前加@就可用
9 select count(distinct id) as rowCount from table1
10 Mysql数据库里表名大小写敏感,字段名大小写不敏感,sql server里大小写不敏感
if
<if test="state != null">
state = #{state}
</if>
choose when otherwise,不想写太多 条件语句的时候,可以用这个
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
where, 会自动去掉多余的and 或or
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
trim ,显示的去掉字符
<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
set,会自动去掉多余的逗号
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio},</if>
</set>
where id=#{id}
</update>
foreach
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
bind
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="‘%‘ + _parameter.getTitle() + ‘%‘" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>
mybatis里所有的判断都是用test ="",比如test="username != null"
mybatis调用存储过程
<!-- 创建多个文档 -->
<select id="createProcedures" parameterType="map" >
<foreach collection="entities" item="entity" index="index" >
call SP_CREATE_PROCEDURE(
#{entity.fileName, mode=IN, jdbcType=VARCHAR},
#{entity.filePath, mode=IN, jdbcType=VARCHAR},
#{entity.deviceIdsWithComma, mode=IN, jdbcType=VARCHAR},
#{entity.createUserId, mode=IN, jdbcType=VARCHAR}
);
</foreach>
</select>
myBatis JDBC type和java数据类型对应表:

<resultMap id="bookMapping" type="com.expample.base.repository.entity.BookEntity">
<result property="id" column="Id"/>
<result property="Name" column="Name"/>
<result property="filePath" column="FilePath"/>
<result property="createUserName" column="CreateUserName"/>
<result property="createUserId" column="CreateUserId"/>
<result property="createTime" column="CreateTime"/>
<result property="isPublished" column="IsPublished"/>
<collection property="assDevices" ofType="com.expample.base.repository.entity.BookCategoryAssEntity">
<result property="id" column="CateotryID"/>
<result property="name" column="CateotryName"/>
</collection>
</resultMap>
<select id="getBookById" parameterType="Integer" resultMap="bookMapping">
SELECT
B.Id,
B.Name,
B.FilePath,
E.Name as CreateUserName,
E.Code as Code,
B.CreateUserId,
B.CreateTime,
B.IsPublished,
C.ID as CateotryID,
C.Name as CateotryName
FROM `Book` AS B
LEFT JOIN `BookCategoryAss`AS BC on B.Id = BC.ProcedureId
LEFT JOIN `Employee` AS E ON ON B.CreateUserId = E.Id
LEFT JOIN `Category` AS C ON BC.Category.Id = C.Id
WHERE B.id = #{id}
</select>
mybatis语法,从数据库查出来的字段可以多于或少于resultType指定的类的属性,都会按能对应的上的名字进行映射。
MYSQL mybatis
标签:device ase jdb 返回值 rom 显示 ber 分支 ping