基于SpringBoot .xml配置方式

spring boot集成mybatis时使用xml配置的方式

  1. 新建User.xml文件
    在resource文件夹下新建mapper文件夹,然后新建User.xml.
    在这里插入图片描述

  2. 安装mybatisx插件
    mybatisx插件,方便xml文件与数据层切换。在.xml文件出现小鸟图标说明建立成功并且.xml与mapper建立了关联。
    在这里插入图片描述

在这里插入图片描述
添加代码如下:

<?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">
<!--namespace指向mapper.xml所对应的Mapper接口-->
<mapper namespace="com.example.demo.demos.web.demo.mapper.UserMapper">
    <update id="update">
        update sys_user
        <set><!--这里我们做这样的判断,主要是为了实现不将原有的数据置空,当然,也可以不判断,就写最基本的SQL语句-->
            <if test="username !=null ">
                username=#{username},
            </if>
            <if test="password !=null ">
                password=#{password},
            </if><!--一般情况password不能修改,修改密码我们通常再做一个单独的业务-->
            <if test="nickname !=null ">
                nickname=#{nickname},
            </if>
            <if test="email !=null ">
                email=#{email},
            </if>
            <if test="phone !=null ">
                phone=#{phone},
            </if>
            <if test="address !=null ">
                address=#{address}
            </if>
        </set>
        <where>
            id=#{id}
        </where>
    </update>

</mapper>

  1. 删除Usermapper中的@update注解
    @update与.xml配置只能二选一,因此去掉注解。
    在这里插入图片描述
  2. 修改yml配置文件
    在这里插入图片描述
server:
  port: 8080
spring:
  datasource:
    username: root
    password: 123456
    #url中database为对应的数据库名称,这里为class
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    cache: false
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis:
  mapper-locations: classpath:mapper/*.xml   #扫描所有mapper文件夹
  1. 测试结果

    postman测试结果:
    在这里插入图片描述
    数据库查询结果:
    在这里插入图片描述
    数据改变,成功,如果有不明白请看之前博客。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值