Spring Boot整合mybatis实现数据库增删改查测试demo

之前练习spring boot整合mybaits实现数据库的增删改查,加强记忆留作以后不时之需查看。

一、所用工具:

开发工具:Intellij IDEA
开发环境:Java version1.8
所用技术:springboot+mybatis+mysql

创建项目

file->NEW->Module创建一个新的Module
是飒飒'd)
选中spring Initializr 开始创建项目

注意:选择对应的Java version版本,我的是1.8
在这里插入图片描述

代码与数据库

我这里只创建了一个部门表用于练习

数据库连接

连接数据库和mybaits的设置

spring:
  datasource:
    username: root
    password: 123
    url: jdbc:mysql://localhost:3306/bokedemo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  type-aliases-package: com.atguigu.demo.pojo
  mapper-locations: classpath:mybatis.mapper/*.xml

创建部门实体类,这里用注解@Data、@AllArgsConstructor、@NoArgsConstructor是实现有参构造、无参构造、get\set方法还有些别的方法,需要了解的自己可以百度一下,这里是我找的一个基本的介绍:lombok几个基本注解的使用@Data@AllArgsConstructor@NoArgsConstructor@Builder

https://blog.csdn.net/ChenXvYuan_001/article/details/84961992

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    private Integer id;
    private String departmentName;
}

mapper接口

import com.atguigu.demo.pojo.Department;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DepartmentMapper {
    List<Department> getdepartment();
    Department getdepartmentById(int id);
    int adddepartment(Department department);
    int updepartment(Department department);
    int delectpartment(int id);
}

mapper映射文件

我的xml文件写在resource里面,我的文件名字是mybatis.mappper(本来写的是子文件,不知道怎么回事只能识别是一个文件)
在这里插入图片描述
namespace是你的mapper文件的全限类名,
注意这里resultMap标签中id是接口中的方法名,type是你实体类的全限定名,由于起了别名,这里只写Department,(增删改查中的id总是指向接口中的方法),parameterType是接口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">
<mapper namespace="com.atguigu.demo.mapper.DepartmentMapper">
    <resultMap id="department" type="Department">
        <id property="id" column="department_id" ></id>
        <result property="departmentName" column="department_name"></result>
    </resultMap>
    <select id="getdepartment" resultMap="department">
        select * from department;
    </select>

    <select id="getdepartmentById" resultType="department">
        select department_id id,department_name departmentName  from department
        <where>department_id=#{id}</where>
    </select>

    <insert id="adddepartment" parameterType="Department">
        insert into department (department_name) values (#{departmentName})
    </insert>
    <update id="updepartment" parameterType="Department">
        update department set department_name=#{departmentName} where department_id=#{id}
    </update>
   <delete id="delectpartment" parameterType="int">
       delete from department where department_id=#{id}
   </delete>
</mapper>

编写测试类

package com.atguigu.demo;


import com.atguigu.demo.mapper.DepartmentMapper;
import com.atguigu.demo.pojo.Department;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class DepartmentApplicationTest {
    @Autowired
    DepartmentMapper departmentMapper;
    @Test
    void test4(){
        int delectpartment = departmentMapper.delectpartment(6);
        System.out.println(delectpartment);
    }
    @Test
    void test3(){
        int update = departmentMapper.updepartment(new Department(2, "生活部"));
        System.out.println(update);
    }
    @Test
    void test2(){
        int adddep = departmentMapper.adddepartment(new Department(1,"学生部"));
        System.out.println(adddep);
    }
    @Test
    void  test1(){
        List<Department> getdepartment = departmentMapper.getdepartment();
        System.out.println("getdepartment = " + getdepartment);
    }
}

这里只放一个查询所有部门的测试结果!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值