总结 MyBatis 的XML实现方法

发布于:2025-04-05 ⋅ 阅读:(13) ⋅ 点赞:(0)

目录

1 配置连接字符串和MyBatis

2 写持久层代码

2.1 添加mapper接⼝

3 增删改查操作 

3.1 增(Insert)

3.2 删(Delete) 

3.3 改(Update) 

3.4 查(Select) 


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

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

写持久层代码

1 配置连接字符串和MyBatis

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

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

2 写持久层代码

持久层代码分两部分

1. ⽅法定义Interface

2. ⽅法实现:XXX.xml

2.1 添加mapper接⼝

数据持久层的接⼝定义:

 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 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 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 单元测试 

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

3 增删改查操作 

3.1 增(Insert)

UserInfoMapper接⼝:

Integer insertUser(UserInfo userInfo);

UserInfoMapper.xml实现:

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

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

Integer insertUser(@Param("userinfo") UserInfo userInfo);

UserInfoMapper.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属性.

<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接⼝:

Integer deleteUser(Integer id);

 UserInfoMapper.xml实现:

<delete id="deleteUser">
 delete from userinfo where id = #{id}
</delete>

3.3 改(Update) 

UserInfoMapper接⼝:

Integer updateUser(UserInfo userInfo);

UserInfoMapper.xml实现:

<update id="updateUser">
 update userinfo set username=#{username} where id=#{id}
</update>

3.4 查(Select) 

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

<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

<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>