应用mybatis框架时,那必然会有对数据库的查问语句的编写,所以这篇文章心愿能够帮忙到你。
什么是Mybatis框架?
MyBatis 是一款优良的长久层框架,它反对定制化 SQL、存储过程以及高级映射。MyBatis 防止了简直所有的 JDBC 代码和手动设置参数以及获取后果集。MyBatis 能够应用简略的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,一般的 Java对象)映射成数据库中的记录。
如何应用?
pom文件依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version></dependency>
yml文件配置,这里匹配 resource/mapper/ 门路下的映射文件。
mybatis: mapper-locations: classpath: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"><mapper namespace="com.flamelephant.fabricmgt.dao.HostMapper"> <resultMap type="com.flamelephant.fabricmgt.entity.po.Host" id="HostMap"> <result property="id" column="id" jdbcType="INTEGER"/> <result property="hostName" column="host_name" jdbcType="VARCHAR"/> <result property="ip" column="ip" jdbcType="VARCHAR"/> <result property="userName" column="user_name" jdbcType="VARCHAR"/> <result property="passWord" column="pass_word" jdbcType="VARCHAR"/> <result property="state" column="state" jdbcType="OTHER"/> <result property="tag" column="tag" jdbcType="VARCHAR"/> <result property="gmtCreated" column="gmt_created" jdbcType="TIMESTAMP"/> <result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/> </resultMap></mapper>
- mapper标签中的namespace属性指定的是咱们长久层接口的我的项目门路
- resultMap是Mybatis最弱小的元素,它能够将查问到的简单数据(比方查问到几个表中数据)映射到一个后果集当中
- resultMap标签的type属性是咱们要映射的实体对象的我的项目门路,id为resultMap的惟一标识。
resultMap中的result标签是实体和数据库表字段的绑定,其中property属性为实体对象的属性名,column为数据库的字段名,jdbcType是字段的类型。
xml映射文件的sql编写
通过实体作为筛选条件查问
<select id="queryAll" resultMap="HostMap"> select id, host_name, ip, user_name, pass_word, state, tag, gmt_created, gmt_modified from host <where> <if test="id != null and id != ''"> and id = #{id} </if> <if test="hostName != null and hostName != ''"> and host_name like CONCAT('%', #{hostName}, '%') </if> <if test="ip != null and ip != ''"> and ip like CONCAT('%', #{ip}, '%') </if> <if test="userName != null and userName != ''"> and user_name = #{userName} </if> <if test="passWord != null and passWord != ''"> and pass_word = #{passWord} </if> <if test="state != null and state != ''"> and state = #{state} </if> <if test="tag != null and tag != ''"> and tag = #{tag} </if> <if test="gmtCreated != null"> and gmt_created = #{gmtCreated} </if> <if test="gmtModified != null"> and gmt_modified = #{gmtModified} /if> </where></select>
- id="queryAll"为长久层接口的形象办法名
resultMap="HostMap" 指定查问后果接管的resultMap的后果集。
长久层接口绑定
/** * 条件查问 * * @param host 条件查问 * @return 对象列表 */List<Host> queryAll(Host host);
通过主键批量删除
<!--通过主键批量删除--><delete id="deleteHostByIds" parameterType="java.lang.Integer"> delete from host where id in <if test="hostIds != null and hostIds.length > 0"> <foreach item="id" collection="hostIds" index="index" open="(" separator="," close=")"> #{id} </foreach> </if> </delete>
以上sql语句的原型为
delete from host where id in(1,2,3)
foreach标签中的属性了解
- collection属性为接管的数据源
- item为汇合中的每一个元素
- index :用于示意在迭代过程中,每次迭代到的地位
- open :示意该语句以什么开始
- separator :示意在迭代时数据以什么符号作为分隔符
- close :示意以什么完结
长久层接口形象办法
/** * 批量删除主机 * * @param hostIds 主机id数组 * @return Integer */Integer deleteHostByIds(@Param("hostIds") Long[] hostIds);
批量新增
<!--批量减少--><insert id="addHostList"> insert into host_and_group(host_group_id, host_id) values <foreach collection="hostGroupIdList" item="hostGroupId" index="index" separator=","> (#{hostGroupId}, #{hostId}) </foreach></insert>
长久层接口办法
/** * 将多个主机增加至一个主机组 * * @param request * @return Integer */Integer addHostList(HostAndGroupRequest request);
我是元素封装在一个对象中,所以这个对象里有批量减少的元素,则间接能够传一个对象。