例子1:
mapper层:
List<Map> queryBookCondition(@Param("params") Map<String, Object> params);
对应的mapper实现层:
public PageInfo<Map> queryBookCondition(Map<String, Object> params) {
PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
List<Map> maps = bookMapper.queryBookCondition(params);
PageInfo<Map> mapPageInfo = new PageInfo<>(maps);
return mapPageInfo;
}
对应的mybatis写法:
<select id="queryBookCondition" resultType="map">
select
book_id bookId, book_name bookName, book_number bookNumber, book_picture bookPicture, price, type_name typeName, deleted
from A.book b LEFT JOIN A.book_type t ON b.type_id = t.type_id
where 1 = 1
<if test="params.searchKey == 'bookName'">
AND b.book_name like CONCAT("%",#{params.searchValue},"%")
</if>
<if test="params.searchKey == 'typeName'">
AND type_name like CONCAT("%",#{params.searchValue},"%")
</if>
<if test="params.searchKey == 'price'">
AND price like CONCAT("%",#{params.searchValue},"%")
</if>
<if test="params.searchKey == 'bookNumber'">
AND book_number like CONCAT("%",#{params.searchValuer},"%")
</if>
order by b.book_id DESC
</select>
例子2:
int insertBatch(@Param("book") List<Book> entities);
<insert id="insertBatch" keyProperty="bookId" useGeneratedKeys="true">
insert into A.book(book_name, book_number, book_picture, description, book_press, price,
book_type)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.bookName}, #{entity.bookNumber}, #{entity.bookPicture}, #{entity.description},
#{entity.bookPress}, #{entity.price}, #{entity.bookType})
</foreach>
</insert>
关于keyProperty可以参考:关于Mybatis中keyProperty属性
springboot整合mybatis-plus的sql输出到日志文件上——百度:mybatisplus输出到文件
springboot 下mybatis-plus 如何打印sql日志和参数到日志文件|来源→
#控制台方式(本地启动日志打印到控制台常用方式):
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#文件方式,需结合logginglevel
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
logging:
level:
com.baomidou.mybatisplus: DEBUG
#这个是配置的mapper路径,如:cn.example.mapper
cn.example.mapper: DEBUG