MyBatis 简介
MyBatis 是一个基于 Java 的持久层框架,它内部封装了 JDBC,使开发者只需关注 SQL 语句本身,而不用再花费精力去处理诸如注册驱动、创建 Connection、配置 Statement 等繁杂过程。
Mybatis 通过  或注解的方式将要执行的各种 Statement、PreparedStatement 等配置起来,并通过 Java 对象和 Statement 中 SQL 的动态参数进行映射生成最终执行的 SQL 语句,最后由 MyBatis 框架执行 SQL 并将结果映射成 Java 对象并返回。
MyBatis 与 Hibernate
Hibernate 框架是提供了全面的数据库封装机制的 全自动 ORM,即实现了 POJO 和数据库表之间的映射,以及 SQL 的自动生成和执行。
相对于此,MyBatis 只能算作是 半自动 ORM。其着力点,是在 POJO 类与 SQL 语句之间的映射关系。也就是说,MyBatis 并不会为程序员自动生成 SQL 语句。具体的 SQL 需要程序员自己编写,然后通过 SQL 语句映射文件,将 SQL 所需的参数,以及返回的结果字段映射到指定 POJO。
MyBatis 特点
- 在 
文件中配置SQL语句,实现了SQL语句与代码的分离,给程序的维护带来了很大便利。 - 可以结合数据库自身的特点灵活控制 
SQL语句,因此能够实现比Hibernate等全自动ORM框架更高的查询效率,能够完成复杂查询。 

Spring 整合 Mybatis
引入依赖
在 pom. 引入 Mybatis 相关依赖。
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version></dependency><dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.17.RELEASE</version></dependency>创建 Mybatis 配置文件
在 resource 目录下创建 mybatis-config 配置文件。
<?创建集成配置文件
在 resource 目录下创建一个 spring-context-mybatis. 的集成配置文件。
<?CRUD 案例
新增
<insert id="insert"> INSERT INTO tb_user (  id,  username,  password,  phone,  email,  created,  updated ) VALUES  (  #{id},  #{username},  #{password},  #{phone},  #{email},  #{created},  #{update}  )</insert>删除
<delete id="delete"> DELETE FROM tb_user WHERE id = #{id}</delete>查询
<select id="getById" resultType="TbUser"> SELECT  a.id,  a.username,  a.password,  a.phone,  a.email,  a.created,  a.updated AS "update" FROM  tb_user AS a WHERE  a.id = #{id}</select>更新
<update id="update"> UPDATE  tb_user SET  username = #{username},  password = #{password},  phone = #{phone},  email = #{email},  created = #{created},  updated = #{update} WHERE id = #{id}</update>MyBatis 动态 SQL
动态 SQL 主要用于解决查询条件不确定的情况:在程序运行期间,根据用户提交的查询条件进行查询。提交的查询条件不同,执行的 SQL 语句不同。若将每种可能的情况均逐一列出,对所有条件进行排列组合,将会出现大量的 SQL 语句。此时,可使用动态 SQL 来解决这样的问题。这里的条件判断使用的表达式为 OGNL 表达式。常用的动态 SQL 标签有 <if>、<where>、<choose>、<foreach> 等。
注意:在 mapper 的动态 SQL 中若出现 >、<、>=,<= 等符号,最好将其转换为实体符号。否则,< 在 
if 标签
对于该标签的执行,当 test 的值为 true 时,会将其包含的 SQL 片断拼接到其所在的 SQL 语句中。
案例
为了解决两个条件均未做设定的情况,在 where 后添加了一个"1=1"的条件。这样就不至于两个条件均未设定而出现只剩下一个 where,而没有任何可拼接的条件的不完整 SQL 语句。
<?where 标签
案例
<select id="selectByWhere" resultType="com.antoniopeng.ssm.entity.Student"> SELECT		 * FROM  student <where>  <if test="name != null and name != ''">   AND name LIKE concat('%', #{name}, '%')  </if>  <if test="age != null and age > 0">   AND age > #{age}  </if> </where></select>choose 标签
该标签中只可以包含 <when/>、<otherwise/>,可以包含多个 <when/> 与一个 <otherwise/>。它们联合使用,完成 Java 中的开关语句 switch case 功能。
案例
本例要完成的需求是,若姓名不空,则按照姓名查询;若姓名为空,则按照年龄查询;若没有查询条件,则没有查询结果。
<!-- choose --><select id="selectByChoose" resultType="com.antoniopeng.ssm.entity.Student"> SELECT  * FROM  student <where>  <choose>   <when test="name != null and name != ''">    AND name LIKE concat('%', #{name}, '%')   </when>   <when test="age != null and age > 0">    AND age > #{age}   </when>   <otherwise>    AND 1 != 1   </otherwise>  </choose> </where></select>foreach 标签
该标签用于实现对于数组与集合的遍历。对其使用,需要注意:collection 表示要遍历的集合类型,这里是数组,即 array。open、close、separator 为对遍历内容的 SQL 拼接。
遍历数组案例
<!-- foreach --><select id="selectByForeach" resultType="com.antoniopeng.ssm.entity.Student"> SELECT  * FROM  student <if test="array != null and array.length > 0">  WHERE id IN  <foreach collection="array" open="(" close=")" item="id" separator=",">   #{id}  </foreach> </if></select>遍历泛型为基本类型的集合案例
/** * 使用 foreach 标签以 list 基本类型的形式查询 * @param ids * @return */public List<Student> selectByForeachWithListBase(List<Long> ids);<!-- foreach --><select id="selectByForeachWithListBase" resultType="com.antoniopeng.ssm.entity.Student"> SELECT  * FROM  student <if test="list != null and list.size > 0">  WHERE id IN  <foreach collection="list" open="(" close=")" item="id" separator=",">   #{id}  </foreach> </if></select>遍历泛型为自定义类型的集合案例
/** * 使用 foreach 标签以 list 自定义类型的形式查询 * @param students * @return */public List<Student> selectByForeachWithListCustom(List<Student> students);<!-- foreach --><select id="selectByForeachWithListCustom" resultType="com.antoniopeng.ssm.entity.Student"> SELECT		 * FROM  student <if test="list != null and list.size > 0">  WHERE id IN  <foreach collection="list" open="(" close=")" item="student" separator=",">   #{student.id}  </foreach> </if></select>sql 标签
该标签用于定义 SQL 片断,以便其它 SQL 标签复用。而其它标签使用该 SQL 片断, 需要使用 <include/> 子标签。该标签可以定义 SQL 语句中的任何部分,所以 <include/> 子标签可以放在动态 SQL 的任何位置。
案例
<sql id="select"> SELECT  id,  name,  age,  score FROM  student</sql>使用 sql 标签
<!-- foreach --><select id="selectByForeachWithListCustom" resultType="com.antoniopeng.ssm.entity.Student"> <include refid="select" /></select>- 文章作者:彭超
 - 本文首发于个人博客:https://antoniopeng.com
 - 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 彭超的博客!
 
原文转载:http://www.shaoqun.com/a/502696.html
斑马物联网:https://www.ikjzd.com/w/1316
打折网站:https://www.ikjzd.com/w/74
MyBatis简介MyBatis是一个基于Java的持久层框架,它内部封装了JDBC,使开发者只需关注SQL语句本身,而不用再花费精力去处理诸如注册驱动、创建Connection、配置Statement等繁杂过程。Mybatis通过或注解的方式将要执行的各种Statement、PreparedStatement等配置起来,并通过Java对象和Statement中SQL的动态参数进行映射生成最终执行
浩方:浩方
mil:mil
湖南凤凰古城必做那些?该怎么玩?:湖南凤凰古城必做那些?该怎么玩?
2020六一森波拉奇妙世界门票多少钱?森波拉奇妙世界儿童节:2020六一森波拉奇妙世界门票多少钱?森波拉奇妙世界儿童节
2008奥运村地址在哪里?:2008奥运村地址在哪里?
没有评论:
发表评论