学习笔记(二十二)数据持久层框架MyBatis的应用

这篇博客通过四个章节详细介绍了如何在SpringBoot项目中应用MyBatis进行数据操作。首先,展示了使用MyBatis和SpringBoot完成第一个查询示例,包括配置mysql.xml、启动程序、编写测试用例和解决启动时jackson依赖问题。接着,讲解了如何利用MyBatis插入数据,包括定义mapper文件、User对象和调试流程。最后,探讨了MyBatis执行数据更新和删除的操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第二节 使用mybatis+SpringBoot完成第一个查询demo

1.mysql.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!--
       Copyright 2015-2016 the original author or authors.
       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
-->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.course">
    <select id="getRecordListCount" resultType="Integer">
        select COUNT(*) from userList_record
    </select>
</mapper>

2.springBoot启动程序Application

package com.course;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PreDestroy;

@EnableScheduling
@SpringBootApplication
public class Application {

    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @PreDestroy
    public void destroy() {
        Application.context.close();
    }

}

3.demo测试

package com.course.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value ="v1", description ="这是一个和数据库关联的demo")
@RequestMapping("v1")
public class Demo {

    //首先获取一个执行sql语句的对象

    @Autowired
    private SqlSessionTemplate template;

    @RequestMapping(value ="/getRecordCount", method = RequestMethod.GET)
    @ApiOperation(value ="获取record表的数量")
    public int getRecordCount(){
       return template.selectOne("getRecordListCount");

    }
}

4.启动springBoot,然后访问:http://localhost:8080/v1/getRecordCount

5.出现的问题---启动springboot报错“Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind”  百度了一下说是jackson的版本与springBoot的版本不兼容,但是我pom里没有引入该包,于是把该包引入后再次启动好啦

 <dependency>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-databind</artifactId>
       <version>2.9.5</version>
 </dependency>

第三节 使用mybatis实现添加数据和idea的debug操作

1.插入数据

    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    @ApiOperation(value = "插入用户数据")
    public int insertUser(@RequestBody User user){
        return template.insert("addUser", user);
    }

2.mysql mapper文件


    <insert id="addUser" parameterType="com.course.module.User">
        insert into user (id, name, age, sex)
        values (#{id},#{name}, #{age}, #{sex})
    </insert>

3.User对象

package com.course.module;

import lombok.Data;

@Data
public class User {
    private String id;
    private String name;
    private String sex;
    private String age;
}

4.重启springboot,访问"localhost:8080/vi/insertUser",最终插入成功一条数据

第四节 使用mybatis实现数据的更新和删除操作

1.mysql.xml

 <update id="updateUser" parameterType="com.course.module.User">
     update user set name = #{name}
     where age = #{age} and id = #{id}
 </update>

 <delete id="deleteUser" parameterType="com,course.module.User">
     delete from user where name = #{name}
 </delete>

2.demo

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation(value = "更新用户数据")
    public int updateUser(@RequestBody User user){
        return template.update("updateUser", user);
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
    @ApiOperation(value = "删除用户数据")
    public int deleteUser(@RequestParam String name){
        return template.delete("deleteUser", name);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值