zoukankan      html  css  js  c++  java
  • springboot-------整合mybatis-plus

    引言

    1、什么是MyBatis-Plus

    MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

    mybatis plus 官网

    建议安装 MybatisX 插件

    2、整合MyBatis-Plus

    添加依赖

      <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.1</version>
            </dependency>
    

    3、编写mapper接口,并且标注@Mapper注解(或者直接在***Application里添加包扫描,如下图)

    AdminMapper

    package com.xiao.boot.mapper;
    
    import com.xiao.boot.bean.Student;
    import com.xiao.boot.bean.Teacher;
    import org.apache.ibatis.annotations.Mapper;
    
    // @Mapper 如果开启了包扫描就不用写注解了
    public interface AdminMapper {
    
        public Integer addTeacher(Teacher teacher);
    
        public Integer addStudent(Student student);
    }
    
    

    4、编写sql映射文件并绑定mapper接口 (默认位置在resources/mapper/下)

    adminMapper.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.xiao.boot.mapper.AdminMapper">
    <!--    添加教师信息-->
        <insert id="addTeacher">
            insert into teacher values (#{id},#{name},#{sex},#{academy},#{title})
        </insert>
    <!--    添加学生信息-->
        <insert id="addStudent">
            insert into student values (#{id},#{name},#{sex},#{classname},#{major})
        </insert>
    </mapper>
    

    5、配置数据源,,并开启驼峰映射规则(采用的yaml文件,默认的是.properties)

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/springboot?useSSL=false&serverTimezone=UTC
    
    mybatis-plus:
      configuration:
        map-underscore-to-camel-case: true
    

    6、编写service和serviceImpl

    service

    package com.xiao.boot.service;
    
    import com.xiao.boot.bean.Student;
    import com.xiao.boot.bean.Teacher;
    
    public interface AdminService {
    
        public Integer addTeacher(Teacher teacher);
    
        public Integer addStudent(Student student);
    }
    

    serviceImpl

    package com.xiao.boot.service.impl;
    
    import com.xiao.boot.bean.Student;
    import com.xiao.boot.bean.Teacher;
    import com.xiao.boot.mapper.AdminMapper;
    import com.xiao.boot.service.AdminService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class AdminServiceImpl implements AdminService {
    
        @Autowired
        AdminMapper adminMapper;
        @Override
        public Integer addTeacher(Teacher teacher) {
            return adminMapper.addTeacher(teacher);
        }
    
        @Override
        public Integer addStudent(Student student) {
            return adminMapper.addStudent(student);
        }
    }
    

    7、编写controller实现业务逻辑

    package com.xiao.boot.controller;
    
    import com.xiao.boot.bean.Student;
    import com.xiao.boot.bean.Teacher;
    import com.xiao.boot.mapper.AdminMapper;
    import com.xiao.boot.service.AdminService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpRequest;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    
    @Controller
    public class AdminController {
    
        @Autowired
        AdminService adminService;
        @GetMapping("/add_teacher_page")
        public String add_teacher_page(){
            return "admin/addTeacherPage";
        }
    
        @GetMapping("/add_student_page")
        public String add_student_page(){
            return "admin/addStudentPage";
        }
    
        @PostMapping("/admin/add_teacher")
        public String addTeacher(Teacher teacher, RedirectAttributes ra){
            Integer row = adminService.addTeacher(teacher);
            if (row == 1){
                ra.addAttribute("message","添加成功");
            }else {
                ra.addAttribute("message","添加失败");
            }
            return "redirect:/add_teacher_page";
        }
    
        @PostMapping("/admin/add_student")
        public String addStudent(Student student, RedirectAttributes ra){
            Integer row = adminService.addStudent(student);
            if (row == 1){
                ra.addAttribute("message","添加成功");
            }else {
                ra.addAttribute("message","添加失败");
            }
            return "redirect:/add_student_page";
        }
    }
    
    
  • 相关阅读:
    Android开发学习之路--Content Provider之初体验
    [NOI2005] 维修数列
    递归算法对完全二叉树的前序遍历
    非递归算法对完全二叉树的前序遍历
    java中的多态
    poj1088滑雪
    在网页中插入flash
    如何采用批处理文件安装一个.dll文件,并且注册
    结构体指针和数组理解
    完全二叉树
  • 原文地址:https://www.cnblogs.com/yangxiao-/p/14256618.html
Copyright © 2011-2022 走看看