总结 MyBatis 的XML实现方法

目录

[1 配置连接字符串和MyBatis](#1 配置连接字符串和MyBatis)

[2 写持久层代码](#2 写持久层代码)

[2.1 添加mapper接⼝](#2.1 添加mapper接⼝)

[3 增删改查操作](#3 增删改查操作)

[3.1 增(Insert)](#3.1 增(Insert))

[3.2 删(Delete)](#3.2 删(Delete))

[3.3 改(Update)](#3.3 改(Update))

[3.4 查(Select)](#3.4 查(Select))


MyBatis XML的⽅式需要以下两步:

配置数据库连接字符串和MyBatis

写持久层代码

1 配置连接字符串和MyBatis

此步骤需要进⾏两项设置,数据库连接字符串设置和MyBatis的XML⽂件配置。

如果是application.yml⽂件,配置内容如下:

2 写持久层代码

持久层代码分两部分

  1. ⽅法定义Interface

  2. ⽅法实现:XXX.xml

2.1 添加mapper接⼝

数据持久层的接⼝定义:

java 复制代码
 import com.example.demo.model.UserInfo;
 import org.apache.ibatis.annotations.Mapper;
 import java.util.List;
 @Mapper
 public interface UserInfoXMlMapper {
     List<UserInfo> queryAllUser();
 }

2.2 添加UserInfoXMLMapper.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="org.example.mybatisdemo.mapper.UserInfoXMLMapper">
    
</mapper>

创建UserInfoXMLMapper.xml,路径参考yml中的配置

查询所有⽤⼾的具体实现:

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.example.demo.mapper.UserInfoXMlMapper">
     <select id="queryAllUser" resultType="com.example.demo.model.UserInfo">
         select username,`password`, age, gender, phone from userinfo
     </select>
 </mapper>

2.3 单元测试

java 复制代码
@SpringBootTest
 class UserInfoMapperTest {
     @Autowired
     private UserInfoMapper userInfoMapper;
     @Test
     void queryAllUser() {
     List<UserInfo> userInfoList = userInfoMapper.queryAllUser();
         System.out.println(userInfoList);
     }
 }

3 增删改查操作

3.1 增(Insert)

UserInfoMapper接⼝:

java 复制代码
Integer insertUser(UserInfo userInfo);

UserInfoMapper.xml实现:

XML 复制代码
<insert id="insertUser">
     insert into userinfo (username, `password`, age, gender, phone) values (#
 {username}, #{password}, #{age},#{gender},#{phone})
</insert>

如果使⽤@Param设置参数名称的话,使⽤⽅法和注解类似

UserInfoMapper接⼝:

java 复制代码
Integer insertUser(@Param("userinfo") UserInfo userInfo);

UserInfoMapper.xml实现:

XML 复制代码
<insert id="insertUser">
 insert into userinfo (username, `password`, age, gender, phone) values
 (#{userinfo.username},#{userinfo.password},#{userinfo.age},#
 {userinfo.gender},#{userinfo.phone})
</insert>

返回⾃增id

接⼝定义不变,Mapper.xml实现设置useGeneratedKeys和keyProperty属性.

XML 复制代码
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
 insert into userinfo (username, `password`, age, gender, phone) values
 (#{userinfo.username},#{userinfo.password},#{userinfo.age},#
 {userinfo.gender},#{userinfo.phone})
</insert>

3.2 删(Delete)

UserInfoMapper接⼝:

java 复制代码
Integer deleteUser(Integer id);

UserInfoMapper.xml实现:

XML 复制代码
<delete id="deleteUser">
 delete from userinfo where id = #{id}
</delete>

3.3 改(Update)

UserInfoMapper接⼝:

java 复制代码
Integer updateUser(UserInfo userInfo);

UserInfoMapper.xml实现:

XML 复制代码
<update id="updateUser">
 update userinfo set username=#{username} where id=#{id}
</update>

3.4 查(Select)

同样的,使⽤XML的⽅式进⾏查询,也存在数据封装的问题 我们把SQL语句进⾏简单修改,查询更多的字段内容.

XML 复制代码
<select id="queryAllUser" resultType="com.example.demo.model.UserInfo">
 select id, username,`password`, age, gender, phone, delete_flag, 
create_time, update_time from userinfo
</select>

结果显⽰:deleteFlag,createTime,updateTime也没有进⾏赋值.

解决办法和注解类似:

  1. 起别名

  2. 结果映射

  3. 开启驼峰命名

其中1,3的解决办法和注解⼀样,不再多说,接下来看下xml如果来写结果映射

Mapper.xml

XML 复制代码
<resultMap id="XmlBaseMap" type="com.example.demo.model.UserInfo">
 <id column="id" property="id"></id>
     <result column="delete_flag" property="deleteFlag"></result>
     <result column="create_time" property="createTime"></result>
     <result column="update_time" property="updateTime"></result>
</resultMap>
<select id="queryAllUser" resultMap="XmlBaseMap">
     select id, username,`password`, age, gender, phone, delete_flag, 
  create_time, update_time from userinfo
</select>
相关推荐
凌波粒1 天前
SpringMVC基础教程(3)--SSM框架整合
java·sql·spring·intellij-idea·mybatis
开开心心就好1 天前
无需函数:Excel数据筛选工具推荐
xml·网络·pdf·华为云·word·excel·音视频
百***17071 天前
Oracle分页sql
数据库·sql·oracle
humors2212 天前
服务端开发案例(不定期更新)
java·数据库·后端·mysql·mybatis·excel
cqsztech2 天前
ORACLE数据库中如何找到过去某个时间某个表被谁修改了
数据库·oracle
朝新_2 天前
【实战】动态 SQL + 统一 Result + 登录校验:图书管理系统(下)
xml·java·数据库·sql·mybatis
百***84452 天前
SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)
spring boot·tomcat·mybatis
q***71852 天前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
原来是好奇心2 天前
Spring Boot缓存实战:@Cacheable注解详解与性能优化
java·spring·mybatis·springboot
战血石LoveYY2 天前
mybatis踩坑值 <if test=> 不能用大写AND
mybatis