学习spring+vue笔记二--springboot+mybatis使用xml查询数据

本文介绍了如何在SpringBoot项目中使用MyBatis的XML映射文件,包括UserMapper接口的定义、XML文件中的SQL操作(如查询、插入、更新和删除),以及如何配置SpringBoot以找到XML文件。还提到如何通过@Service和@Controller注解组织代码和处理HTTP请求。
摘要由CSDN通过智能技术生成

接上篇日记

在/resources/文件夹下新建mapper文件夹
然后新建user.xml
文件头如下:(idea社区版好多时候没有自动代码提示或者提示错误,需要仔细检查或者自己书写)。

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
        "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhuoaninfo.vueDemo.mapper.UserMapper">
    <resultMap id="user" type="com.zhuoaninfo.vueDemo.entity.User">
        <id column="id" property="id" javaType="int"></id>
        <result column="name" property="name" javaType="string"></result>
        <result column="password" property="password" javaType="string"></result>
        <result column="address" property="address" javaType="string"></result>
    </resultMap>
    <select id="findAll" resultMap="user" resultType="com.zhuoaninfo.vueDemo.entity.User">
        select * from sys_user
    </select>
<insert id="insert">
    insert into sys_user(name,password,address) values(#{name},#{password},#{address})
</insert>
    <update id="updateUser">
        update sys_user
        <set>
            <if test="name != null">
                name=#{name},
            </if>
            <if test="password != null">
                password=#{password},
            </if>
            <if test="address != null">
                address=#{address}
            </if>
        </set>
        <where>
            id=#{id}
        </where>
    </update>
</mapper>

需要注意的是select 语句返回的是list类型,这里需要写一个resulemap,其中的property对应的是实体类中的属性名。
我们再看一下/com/zhuoaninfo/vueDemo/mapper/UserMapper.class的代码

import com.zhuoaninfo.vueDemo.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
public interface UserMapper {
    List<User> findAll();
    Integer insert(User user);
    Integer updateUser(User user);
    @Delete("delete from sys_user where id=#{ida}")
    Integer deleteUserByid(@Param("ida") Integer id);
}

和上一篇的代码不同的是把findAll()方法和insert方法都放到了xml文件中进行书写。
这里说明,mybatis的方法可以写在对应的mpper.class类中,比如deleteUserByid(),也可以写到XML文件中,一般动态SQL语句写到xml文件中。
为了方便mapper.class中的方法使用对应xml中的方法实现的编写查看,可以下载mybatisX插件。
另外需要特别注意的是如果使用xml,需要在application.yml进行配置,否者springboot找不到xml文件。
配置内容为:

server:
  port : 8181
spring:
  datasource:
    driver-class-name: org.mariadb.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/vueDemo?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8
    username: root
    password: 123456
mybatis:
  mapper-locations: classpath:mapper/*.xml #扫描所有mybatis的

我粘贴的是配置的全代码,配置xml的就是
mybatis:
mapper-locations: classpath:mapper/*.xml
这里经常容易出错的是YML的书写格式,配置项的冒号后面一定要加空格,不同级别的配置项也要注意缩进。

对之前的UserController也进行了代码调整,代码如下:

package com.zhuoaninfo.vueDemo.controller;

import com.zhuoaninfo.vueDemo.entity.User;

import com.zhuoaninfo.vueDemo.mapper.UserMapper;

import com.zhuoaninfo.vueDemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Controller
@RequestMapping("/vueDemo")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index() {
        return "welcome to springboot!";
    }

    @RequestMapping("/users")
    public List<User> getUsers() {
        return userMapper.findAll();
    }

    @PostMapping("/user")
    public Integer addUser(@RequestBody User vo){
        return userService.save(vo);
    }

    @PutMapping("/user")
    public Integer updateUser(@RequestBody User vo){
        return userMapper.updateUser(vo);
    }

    @DeleteMapping("/{id}")  //这里的id和下面的id需要保持一致
    public Integer deleteUser(@PathVariable Integer id){
      return  userMapper.deleteUserByid(id);
    }
}

为了让@PostMapping(“/user”)既可以插入也可以实现更新,新建了一个UserService.class
在com/zhuoaninfo/vueDemo/ 新建service文件夹,然后新建UserService.Class
文件结构如图:
在这里插入图片描述UserService.class代码如下:

package com.zhuoaninfo.vueDemo.service;

import com.zhuoaninfo.vueDemo.entity.User;
import com.zhuoaninfo.vueDemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public Integer save(User user){
        if(user.getId() == null){
          return   userMapper.insert(user);
        }else{
           return userMapper.updateUser(user);
        }
    }
}

启动项目,使用postman测试成功。
在这里插入图片描述

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cngm110

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值