1 基于XML 操作
本文全篇讲MyBatis的XML版
1.1 注解和xml对比图
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5HBq3vsG-1633679023815)(assets/image-20210928153354783.png)]](https://i-blog.csdnimg.cn/blog_migrate/12bbca8a77b6ce54cf37498092bfbb46.png)
1.2 入门案例
1.2.1 环境搭建
-
项目:day04_mybatis_xml
-
拷贝内容:
- 拷贝jar包
- 拷贝配置文件:核心配置文件、数据库properties、日志配置
- 拷贝工具类
- 拷贝JavaBean:User
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0g7xjh3L-1633679023821)(assets/image-20210928154111656.png)]](https://i-blog.csdnimg.cn/blog_migrate/7e3bfd539c8c70dfef5355060b66bde5.png)
-
空的测试类
package com.czxy.ssm; import com.czxy.ssm.utils.MyBatisUtils; import java.util.List; /** * @author LiReign */ public class Test01_XML_SelectAll { public static void main(String[] args) { //1 获得mapper //2 查询素有 //3 释放 MyBatisUtils.commitAndclose(); } }
1.2.2 代码实现
- 编写功能接口和方法(注意:没有sql语句)
- 编写Mapper映射文件(也就是写SQL语句)
- 修改核心配置文件(确定mapper映射文件位置)
- 测试
-
编写功能接口和方法(注意:没有sql语句)
package com.czxy.ssm.mapper; import com.czxy.ssm.domain.User; import java.util.List; /** * @author LiReign */ public interface UserMapper { /** * 查询所有 * @return */ public List<User> selectAll(); } -
编写Mapper映射文件(也就是写SQL语句)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x1ReIqVt-1633679023825)(assets/image-20210928155312069.png)]](https://i-blog.csdnimg.cn/blog_migrate/f15ec4efec734d394ab36f43b434a488.png)
<?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.czxy.ssm.mapper.UserMapper"> <!-- 编写查询语句 --> <select id="selectAll" resultType="com.czxy.ssm.domain.User"> select * from user </select> </mapper> -
修改核心配置文件(确定mapper映射文件位置)
<mappers> <!-- 确定映射文件位置 --> <mapper resource="mapper/UserMapper.xml"/> </mappers> -
测试
package com.czxy.ssm; import com.czxy.ssm.domain.User; import com.czxy.ssm.mapper.UserMapper; import com.czxy.ssm.utils.MyBatisUtils; import java.util.List; /** * @author LiReign */ public class Test01_XML_SelectAll { public static void main(String[] args) { //1 获得mapper UserMapper userMapper = MyBatisUtils.getMapper(UserMapper.class); //2 查询素有 List<User> list = userMapper.selectAll(); list.forEach(System.out::println); //3 释放 MyBatisUtils.commitAndclose(); } }
1.3 增删改
1.3.1 注解和xml对比
| 注解 | XML标签 |
|---|---|
| @Select(“SQL语句”) | <select>SQL语句</select> |
| @Insert(“SQL语句”) | <insert>SQL语句</insert> |
| @Update(“SQL语句”) | <update>SQL语句</update> |
| @Delete(“SQL语句”) | <delete>SQL语句</delete> |
这里就举添加和修改两个例子
1.3.2 添加
-
编写功能接口
/** * 添加 * @param user * @return */ public Integer insertUser(User user); -
编写xml
<!-- 添加 --> <insert id="insertUser" parameterType="com.czxy.ssm.domain.User"> INSERT INTO `user`(uid,user_name,`password`) VALUES(#{uid},#{userName},#{password}) </insert> -
测试
@Test public void testInsert() { //1 获得mapper UserMapper userMapper = MyBatisUtils.getMapper(UserMapper.class); //2 添加 User user = new User(); user.setUid("x002"); user.setUserName("xxxx"); user.setPassword("溜溜"); Integer result = userMapper.insertUser(user); System.out.println(result); //3 释放 MyBatisUtils.commitAndclose(); }
1.3.3 修改
-
修改功能接口,添加修改方法
/** * 更新 * @param user * @return */ public Integer updateUser(User user); -
修改xml,为方法编写sql语句
<!-- 修改 --> <update id="updateUser"> update `user` set user_name=#{userName}, password=#{password} where uid=#{uid} </update> -
测试类
@Test public void testUpdate() { //1 获得mapper UserMapper userMapper = MyBatisUtils.getMapper(UserMapper.class); //2 添加 User user = new User(); user.setUid("x002"); user.setUserName("yyyyy"); user.setPassword("遛遛"); Integer result = userMapper.updateUser(user); System.out.println(result); //3 释放 MyBatisUtils.commitAndclose(); }
1.4 API详解:映射文件
1.4.1 参数类型:parameterType
-
案例1:查询详情
-
接口,需要使用
@Param设置别名/** * 通过id查询详情 * @param id * @return */ public User selectById(@Param("uid") String id); -
xml
<select id="selectById" parameterType="string" resultType="com.czxy.ssm.domain.User"> select * from user where uid =#{uid} </select>
-
-
案例2:用户名、密码查询
-
接口
/** * 通过用户名和密码查询 * @param username * @param password * @return */ public User selectByUsernameAndPassword(@Param("username") String username,@Param("password") String password); -
xml
<select id="selectByUsernameAndPassword" resultType="com.czxy.ssm.domain.User"> select * from user where user_name = #{username} and password = #{password} </select>
-
1.4.2 结果集类型:resultType
-
案例:总条数
-
接口
/** * 总条数 * @return */ public Long countUser(); -
xml
<select id="countUser" resultType="long"> select count(*) from user </select>
-
1.4.3 映射关系(自定义映射):resultMap
-
在注解版学习过程,使用
@Results可以配置自定义映射 -
在XML版中,使用 resultMap 完成相同的工作。
-
使用
<!--声明--> <resultMap id="名称"> <id/> <!--主键映射--> <result/> <!--普通列映射--> </resultMap> <!--使用--> <select resultMap="名称"></select> -
实例:修改查询所有
<!-- 查询所有版本2:resultMap --> <resultMap id="userResultMap" type="com.czxy.ssm.domain.User"> <id property="uid" column="uid"></id> <result property="userName" column="user_name"></result> </resultMap> <select id="selectAll" resultMap="userResultMap"> select * from user </select>
1.4.4 自定义别名
-
定义别名:在核心配置中,定义别名。
-
使用别名:在映射文件中,使用别名。
-
定义别名
-
给一个类定义别名
<typeAlias type="com.czxy.ssm.domain.User" alias="user"></typeAlias> -
给包下面的所有类,定义别名(规则:首字母小写驼峰)
<package name="com.czxy.ssm.domain"/>
-
-
使用
<resultMap id="" type="user">
2.1 动态SQL
2.1.1 为什么学习动态SQL
- 动态SQL可以满足实际开发中
多条件查询功能。
2.1.2 什么是动态SQL
- 动态SQL:根据数据的不同,动态的拼凑SQL语句技术。
- 拼凑技术:
- 判断 if
- 循环forEach
- 简化条件 where
2.1.3 判断 if
-
场景1:通过id查询用户详情,如果id不存在查询所有。
-
功能接口的功能方法
List<User> selectBySQL(String uid); -
xml配置
<!-- 通过id查询详情 --> <select id="selectBySQL" parameterType="string" resultType="com.czxy.ssm.domain.User"> select * from user <if test="uid != null and uid != ''"> where uid =#{uid} </if> </select>
-
-
场景2:通过用户名和密码查询用户详情,如果用户名和密码不存在查询所有。
-
功能接口的功能方法
public List<User> selectBySQL2(@Param("username") String username,@Param("password") String password); -
xml配置
<!-- 动态SQL,2个条件 ,提供恒等条件 where 1=1 --> <select id="selectBySQL2" resultType="com.czxy.ssm.domain.User"> select * from user where 1=1 <if test="username != null and username != ''"> and user_name = #{username} </if> <if test="password != null and password != ''"> and password = #{password} </if> </select>
-
2.1.4 简化条件 where
<select id="selectBySQL2" resultType="com.czxy.ssm.domain.User">
select * from user
<where>
<if test="username != null and username != ''">
and user_name = #{username}
</if>
<if test="password != null and password != ''">
and password = #{password}
</if>
</where>
</select>
2.1.5 循环forEach
-
目标:使用forEach拼凑SQL片段
uid in (1,2,3) -
步骤:
- 编写条件查询封装对象:UserVo. ids 集合
- 编写功能接口:condition
- 编写XML,拼凑条件
-
实现:
-
编写条件查询封装对象:UserVo. ids 集合
package com.czxy.ssm.vo; import java.util.ArrayList; import java.util.List; /** * @author LiReign */ public class UserVo { private List<String> ids = new ArrayList<>(); public List<String> getIds() { return ids; } public void setIds(List<String> ids) { this.ids = ids; } } -
编写功能接口:condition
/** * 多条件查询 * @param userVo * @return */ public List<User> condition(UserVo userVo); -
编写XML,拼凑条件
<!-- select * from user where uid in ('u001','u002','u003') --> <select id="condition" resultType="com.czxy.ssm.domain.User"> select * from user <where> <foreach collection="ids" open="uid in (" item="id" separator="," close=")"> '${id}' </foreach> </where> </select>
- 注意事项:
- 使用forEach拼凑,如果是字符类型,需要使用引号。
2.1.6 if + forEach
- 通过size可以判断list计划的元素数量
<!-- select * from user where uid in ('u001','u002','u003') -->
<select id="condition" resultType="com.czxy.ssm.domain.User">
select * from user
<where>
<if test="ids != null and ids.size > 0">
<foreach collection="ids" open="uid in (" item="id" separator="," close=")">
'${id}'
</foreach>
</if>
</where>
</select>

本文详细介绍了MyBatis的XML映射文件使用,包括查询所有、添加、修改、删除操作。通过比较注解和XML的差异,展示了XML在SQL语句编写上的灵活性。此外,还讲解了参数类型、结果集类型、自定义映射和自定义别名的配置,并通过案例解释了动态SQL中的if、forEach标签的用法,以及如何实现多条件查询。
2233

被折叠的 条评论
为什么被折叠?



