首页 > 其他分享 >springboot整合mybatis

springboot整合mybatis

时间:2023-06-29 15:55:34浏览次数:33  
标签:reMap springboot Dept dept 整合 mybatis import com public

1, 在application.yml中配置

点击查看代码
#mybatis
#配置mapper路径
mybatis:
 mapper-locations: classpath:mapper/*.xml

2, 在每个DAO接口上面追加 @Mapper 注解

-------------------------------------------------------------

增删改查的实现代码
application.yml配置文件
#tomcat 端口
server:
  port: 8888
  servlet:
    context-path: /api

# 数据库四大金刚
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/class009_fjg?characterEncoding=utf8
    username: root
    password: root
#mybatis
#配置mapper路径
mybatis:
  mapper-locations: classpath:mapper/*.xml
po类
package com.bh.po;

public class Dept {
    private int deptno;
    private  String dname;
    private String loc;

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

dao层
package com.bh.dao;

import com.bh.po.Dept;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface DeptDAO {
public List<Dept> findall();
public Dept findById(Dept dept);
public int removeByPk(Dept dept);
public int save(Dept dept);
public int modify(Dept dept);
}

mapper.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.bh.dao.DeptDAO">

    <select id="findall" resultType="com.bh.po.Dept">
        select * from dept
    </select>

    <select id="findById" resultType="com.bh.po.Dept">
        select * from dept where deptno=#{deptno}
    </select>

    <delete id="removeByPk" parameterType="com.bh.po.Dept">
        delete from dept where deptno=#{deptno}
    </delete>

    <insert id="save"  parameterType="com.bh.po.Dept">
        insert into dept values(#{deptno},#{dname},#{loc})
    </insert>

    <update id="modify" parameterType="com.bh.po.Dept">
        update  dept  set dname=#{dname},loc=#{loc} where deptno=#{deptno}
    </update>

</mapper>
R类
package com.bh.po;

import java.util.Map;

public class R {
    private int code;
    private String message;
    private Map<String,Object> result;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getResult() {
        return result;
    }

    public void setResult(Map<String, Object> result) {
        this.result = result;
    }
}

service层
package com.bh.service;

import com.bh.po.Dept;
import com.bh.po.R;


public interface DeptServic {
    public R findall();
    public R findById(Dept dept);
    public R remove(Dept dept);
    public R save(Dept dept);
    public R modify(Dept dept);
}

service的实现类
package com.bh.service.impl;

import com.bh.dao.DeptDAO;
import com.bh.po.Dept;
import com.bh.po.R;
import com.bh.service.DeptServic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptServiceImpl implements DeptServic {
    @Autowired
    private DeptDAO deptDAO;
    @Override
    public R findall() {
        R r = new R();
       Map<String, Object> reMap = new HashMap<>();
        List<Dept> resultList = deptDAO.findall();
        if (resultList.size() ==0 || resultList ==null){
            r.setMessage("检索不成功");
            r.setCode(2000);
            reMap.put("items",null);
            r.setResult(reMap);
        }else {
            r.setMessage("检索成功");
            r.setCode(200);
            reMap.put("items",resultList);
            r.setResult(reMap);
        }
        return r;
    }



    @Override
    public R findById(Dept dept) {
        Dept byId = deptDAO.findById(dept);
        R r = new R();
        Map<String, Object> reMap = new HashMap<>();
        if (byId == null){
            r.setMessage("检索不成功");
            r.setCode(2000);
            reMap.put("items",null);
            r.setResult(reMap);
        }else {
            r.setMessage("检索成功");
            r.setCode(200);
            reMap.put("items",byId);
            r.setResult(reMap);
        }
        return r;
    }

    @Override
    public R remove(Dept dept) {
        R r = new R();
        Map<String, Object> reMap = new HashMap<>();
        int i = deptDAO.removeByPk(dept);
        if (i == 0){
            r.setCode(2000);
            r.setMessage("删除失败");
            reMap.put("items","失败");
            r.setResult(reMap);
        }else {
            r.setCode(200);
            r.setMessage("删除成功");
            reMap.put("items",i);
            r.setResult(reMap);
        }
        return r;
    }

    @Override
    public R save(Dept dept) {
        R r = new R();
        Map<String, Object> reMap = new HashMap<>();
        int save = deptDAO.save(dept);
        if (save == 0){
            r.setCode(2000);
            r.setMessage("保存失败");
            reMap.put("items","失败");
            r.setResult(reMap);
        }else {
            r.setCode(200);
            r.setMessage("保存成功");
            reMap.put("items",save);
            r.setResult(reMap);
        }
        int a=100;
        int c=0;

        return r;
    }

    @Override
    public R modify(Dept dept) {
        R r = new R();
        Map<String, Object> reMap = new HashMap<>();
        int modify = deptDAO.modify(dept);
        if (modify == 0){
            r.setCode(2000);
            r.setMessage("修改失败");
            reMap.put("items","失败");
            r.setResult(reMap);
        }else {
            r.setCode(200);
            r.setMessage("修改成功");
            reMap.put("items",modify);
            r.setResult(reMap);
        }
        return r;
    }
}

controller层
package com.bh.controller;

import com.bh.po.Dept;
import com.bh.po.R;
import com.bh.service.impl.DeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptServiceImpl deptService;
    /*查询全部人员信息*/
    @RequestMapping("/findall")
    public R findall(){
        R findall = deptService.findall();
        return findall;

    }
    /*根据主键查询*/
    @RequestMapping("/findbyid")
    public R findById(@RequestBody Map<String,Object> params){
        Dept dept = new Dept();
        dept.setDeptno((Integer) params.get("id"));
        R byId = deptService.findById(dept);
        return byId;
    }
    /*根据主键进行删除*/
    @RequestMapping("/removebypk")
    public R removeByPk(@RequestBody Map<String,Object> params){
        Dept dept = new Dept();
        dept.setDeptno((Integer) params.get("id"));
        R remove = deptService.remove(dept);
        return remove;

    }
    /*保存一条数据*/
    @RequestMapping("/save")
    public R save(@RequestBody Map<String,Object> params){
        Dept dept = new Dept();
        dept.setDeptno((Integer) params.get("id"));
        dept.setDname((String) params.get("name"));
        dept.setLoc((String) params.get("loc"));
        R save = deptService.save(dept);
        return save;
    }
    /*根据主键修改*/
    @RequestMapping("/modify")
    public R modify(@RequestBody Map<String,Object> params){
        Dept dept = new Dept();
        dept.setDeptno((Integer) params.get("id"));
        dept.setDname((String) params.get("name"));
        dept.setLoc((String) params.get("loc"));
        R modify = deptService.modify(dept);
        return modify;
    }
}

标签:reMap,springboot,Dept,dept,整合,mybatis,import,com,public
From: https://www.cnblogs.com/liangkuan/p/17514418.html

相关文章

  • Mybatis中查询出来的部分数据为空,能查出行部分字段为空
    同样的sql语句,在数据库中能查出完整的行数据在mybatis中返回数据中能查出行,但是部分有值的字段为空。首先检查缓存,没问题的话大概率是因为mybatis自动映射的关系:1.列名不匹配:自动映射会根据数据库中的列名和对象属性名进行映射,如果列名和属性名不匹配,就会导致数据为空。确保数......
  • springboot 通用限流方案设计与实现
    一、背景限流对于一个微服务架构系统来说具有非常重要的意义,否则其中的某个微服务将成为整个系统隐藏的雪崩因素,为什么这么说?举例来讲,某个SAAS平台有100多个微服务应用,但是作为底层的某个或某几个应用来说,将会被所有上层应用频繁调用,业务高峰期时,如果底层应用不做限流处理,该应用......
  • SpringBoot 2 种方式快速实现分库分表,轻松拿捏!
    大家好,我是小富~(一)好好的系统,为什么要分库分表?(二)分库分表的21条法则,hold住!本文是《分库分表ShardingSphere5.x原理与实战》系列的第三篇文章,本文将为您介绍ShardingSphere的一些基础特性和架构组成,以及在Springboot环境下通过JAVA编码和Yml配置两种方式快速实现分库......
  • idea springboot本地打包配置
    cleanpackage-plrenren-admin-am-amd......
  • springboot入门教程,大家都是怎么学习的?
     学习SpringBoot可以帮助你提高Java后端开发的效率和质量,更快速地构建应用程序,并与当前的开发趋势保持一致。不过,建议你始终关注最新的版本和技术发展,及时了解并学习最新的特性和最佳实践。SpringBoot入门教程对于初学者来说是非常好学的。B站上动力节点王妈妈的springboot3教程......
  • mybatis 动态数据源核心--AbstractRoutingDataSource
    1publicabstractclassAbstractRoutingDataSourceextendsAbstractDataSourceimplementsInitializingBean{2@Nullable3privateMap<Object,Object>targetDataSources;4@Nullable5privateObjectdefaultTargetDataSource;......
  • springboot入门教程,大家都是怎么学习的?
    ​学习SpringBoot可以帮助你提高Java后端开发的效率和质量,更快速地构建应用程序,并与当前的开发趋势保持一致。不过,建议你始终关注最新的版本和技术发展,及时了解并学习最新的特性和最佳实践。​Springboot对于初学者来说是非常好学的。B站上动力节点王妈妈的springboot3教程......
  • springboot3视频教程,很多可用的教学资源
    不得不说,学习SpringBoot是非常有必要的,尤其是对于Java后端开发人员来说。SpringBoot是基于SpringFramework的快速开发框架,通过自动配置和约定优于配置的原则,大大简化了Spring应用的开发和部署。它提供了丰富的功能和开箱即用的特性,可以帮助开发者快速搭建高效、可靠的企业级应用......
  • 掌握这40个 SpringBoot 常用注解,开发效率翻几倍!
    SpringWebMVC与SpringBean注解SpringWebMVC注解@RequestMapping@RequestMapping注解的主要用途是将Web请求与请求处理类中的方法进行映射。SpringMVC和SpringWebFlux都通过RquestMappingHandlerMapping和RequestMappingHndlerAdapter两个类来提供对@RequestMappin......
  • springboot mybatis mapper 注入原理浅析
    spring+mybatis是我们常用的开发组合,一般情况,我们只需要写一个Mapper接口 加上@Mapper注解就可以使用了,那么他的工作原理是什么呢?标准mybatis调用应该是这样的流程1//读取配置2InputStreamconfig=Resources.getResourceAsStream("mybatis-config.xml");3//根......