【MyBatis】_使用XML实现MyBatis

发布于:2025-02-16 ⋅ 阅读:(39) ⋅ 点赞:(0)

目录

1. 配置yml配置文件

1.2 配置数据库

1.3 配置xml的路径

2. xml文件中实现数据库的增删查改操作

2.1 各文件内容

2.2 编写细节


MyBatis作为一个持久层框架,用于进行数据库操作。

MyBatis的实现方式有两种:(1)注解;(2)XML;

本文介绍基于XML实现MyBatis。

1. 配置yml配置文件

1.2 配置数据库

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
    username: root
    password: xxxxxx
    driver-class-name: com.mysql.cj.jdbc.Driver

1.3 配置xml的路径

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/**Mapper.xml

其中:

mapper是resources目录下的子目录名,**Mapper.xml表示该XML文件的命名方式必须以Mapper.xml结尾,二者均可自命名,注意对应即可; 

注:注意mapper-locations与configurations同层,都属于mybatis的下一层,注意对准层次

2. 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.zhouyou.mybatisdemo1.mapper.UserInfoXMLMapper">

</mapper>

其中,namespace的值是待实现接口(UserInfoXMLMapper)的全限定类名


现使用MyBatis操作数据库实现增删查改操作。

创建一个UserInfoXMLMapper接口,用于编写方法声明;

在resource下创建一个mapper包,再创建UserInfoXMLMapper.xml用于进行数据持久层的实现;

目录结构如下:

2.1 各文件内容

1、在Mapper类中编写各个方法声明:

package com.zhouyou.mybatisdemo1.mapper;

import com.zhouyou.mybatisdemo1.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserInfoXMLMapper {
    List<UserInfo> selectAll();

    Integer insert(@Param("userInfo") UserInfo userInfo);

    Integer delete(Integer id);

    Integer update(UserInfo userInfo);
}

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="com.zhouyou.mybatisdemo1.mapper.UserInfoXMLMapper">

    <select id="selectAll" resultType="com.zhouyou.mybatisdemo1.model.UserInfo" >
        select* from userInfo
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into userinfo (username, password, age, gender,phone)
            VALUES (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})
    </insert>

    <delete id="delete">
        delete from userinfo where id=#{id}
    </delete>
    <update id="update">
        update userinfo set age=#{age} where id=#{id}
    </update>
</mapper>

3、创建测试类UserInfoXMLMapperTest内容如下:

package com.zhouyou.mybatisdemo1.mapper;

import com.zhouyou.mybatisdemo1.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
@Slf4j
@SpringBootTest
class UserInfoXMLMapperTest {
    @Autowired
    private UserInfoXMLMapper userInfoXMLMapper;
    @Test
    void selectAll() {
        List<UserInfo>  userInfos=userInfoXMLMapper.selectAll();
        log.info(userInfos.toString());
    }

    @Test
    void delete() {
        userInfoXMLMapper.delete(10);
    }

    @Test
    void update() {
        UserInfo userInfo=new UserInfo();
        userInfo.setAge(21);
        userInfo.setId(8);
        userInfoXMLMapper.update(userInfo);
    }

    @Test
    void insert() {
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername("tianqi");
        userInfo.setPassword("tianqi");
        userInfo.setAge(20);
        userInfo.setGender(2);
        userInfo.setPhone("18612340006");
        Integer result = userInfoXMLMapper.insert(userInfo);
        log.info("affected rows: {}\n"+
                "auto_increment primary key: {}",result,userInfo.getId());
    }
}

 以上四个方法分别实现:全列选择查询、新增、更新/更改、删除;

2.2 编写细节

1、关于<select>标签:

select标签的id指明方法,注意需与接口定义的方法名保持一致,否则会报绑定错误:

select标签的resultType指明返回的数据的类型;

2、关于参数重命名问题,使用xml实现MyBatis和使用注解实现MyBatis方法相同,使用@Param注解实现。并且当参数类型为对象时,若进行了重命名,则需使用 对象名.属性名 作为参数名;

3、对selectAll(或selectOne)、insert、update、delete四个方法,只有selectAll(或selectOne)的<select>标签需设置resultType,其余三个方法无需设置;


网站公告

今日签到

点亮在社区的每一天
去签到