SpringBoot——SpringBoot整合Mybatis

SpringBoot——SpringBoot整合MybatisSpringBoot——SpringBoot整合Mybatis

大家好,又见面了,我是你们的朋友全栈君。

1.首先需要在pom.xml文件中添加依赖。

需要添加的依赖如下:

         <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

2.配置application.properties文件

代码如下:

 #配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.1.121:3306/world?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

#Mybatis配置
#设置mapper文件的位置
mybatis.mapperLocations=classpath:mapper/*.xml

到此为止mybatis的整合就完成了,下面我们来写一下代码测试一下。

3.创建实体类

 package com.youyou.address.entity;


import lombok.Data;

/**
 * 数据库实体,联系人
 */
@Data
public class ContacterEO {
    /**
     * 主键

     */
    private String id;
    /**
     * 姓名
     */
    private String name;

    /**
     * 性别(0,女;1,男)
     */
    private String sex;

    /**
     * 年龄
     */
    private Integer age ;

    /**
     * 电话
     */
    private  String phone;

    /**
     * 地址
     */
    private String location;

    /**
     * 删除(0,未删除;1,删除)
     */
    private String dflag ;
}

注意:如果不能识别@Data注解,则需要手动添加getter和setter方法。

4.创建dao接口

 package com.youyou.address.dao;


import com.youyou.address.entity.ContacterEO;

import java.util.List;

public interface ContacterDao {

    void insertOne(ContacterEO contacterEO);

    /**
     * 查询所有
     * @return
     */
    List<ContacterEO> findAll();

}

5.编写对应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.youyou.address.dao.ContacterDao" >

    <select id="insertOne"  parameterType="com.youyou.address.entity.ContacterEO" >
        INSERT INTO ts_contacter VALUES(#{id} , #{name} , #{sex} , #{age} , #{phone} , #{location} , #{dflag})
    </select>

    <select id="findAll" resultType="com.youyou.address.entity.ContacterEO">
        select * from ts_contacter
    </select>

</mapper>

6.编写service类

 package com.youyou.address.service;

import com.youyou.address.dao.ContacterDao;
import com.youyou.address.entity.ContacterEO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ContacterService {

    @Autowired
    private ContacterDao dao;

    public void insertOne(ContacterEO contacterEO){
         dao.insertOne(contacterEO);
    }

    public List<ContacterEO> findAll(){
        return dao.findAll();
    }

}

7.编写controller类

 package com.youyou.address.controller;


import com.youyou.address.entity.ContacterEO;
import com.youyou.address.service.ContacterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "联系人接口")
@RestController
@RequestMapping(value = "/contacter")
public class ContacterController {

    @Autowired
    private ContacterService service;

    @ApiOperation(value = "新增")
    @GetMapping("/add")
    public ContacterEO  add(ContacterEO contacterEO){
        System.out.println("添加一个联系人");

        service.insertOne(contacterEO);

        return contacterEO;
    }

    @ApiOperation(value = "查询所有")
    @GetMapping("/findAll")
    public List<ContacterEO> findAll(){
        return service.findAll();
    }

}

运行结果

数据库中数据

SpringBoot——SpringBoot整合Mybatis

运行结果

SpringBoot——SpringBoot整合Mybatis

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106467.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Latex 公式换行问题,(换行,等号对齐)

    Latex 公式换行问题,(换行,等号对齐)1.换行后等式对齐\begin{equation}\begin{aligned}R(S_2)&=p_2\cdotS_2=\sum_{i\in\mathcal{I}^+(p_2)}B_i+\betaB_{l’}\\&\leq\sum_{i\in\mathcal{I}^+(p_2)}B_i+B_{l’}\leq\sum_{i\in\mat

    2022年4月30日
    180
  • ehcache缓存原理_实现lru缓存

    ehcache缓存原理_实现lru缓存运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。实现 LRUCache 类:LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久

    2022年8月8日
    3
  • 教你win10系统显卡驱动安装失败的解决方法

    教你win10系统显卡驱动安装失败的解决方法我们日常在对电脑的使用过程中,经常都会遇到这样或那样的问题。比如说win10系统显卡驱动安装失败该怎么办呢?别着急,还有小编在呢?接下来小编就来告诉大家win10电脑系统显卡驱动安装失败怎么解决。详细教你win10系统显卡驱动安装失败怎么办:方法一,删除之前的显卡驱动文件重新安装1,首先,右键点击“此电脑”,菜单栏选择“管理”。2,进入计算机管理界面后,点击“设备管理器”,然后在界面右侧展开“显示适配器”选项,并右键点击显卡驱动程序,菜单栏选择“属性”下一步。3,点击“卸载设备”。4,显卡

    2022年6月13日
    34
  • ubuntu16.04安装教程_刚安装ETC不能马上使用吗

    ubuntu16.04安装教程_刚安装ETC不能马上使用吗Ubuntu的安装与使用1、请参考:https://jingyan.baidu.com/album/ca00d56c2b5257e99febcf41.html?picindex=29注意网络连接选

    2022年8月1日
    4
  • npm使用淘宝镜像(npm切换淘宝镜像)

    1.通过cnpm使用淘宝镜像:npminstall-gcnpm–registry=https://registry.npm.taobao.org2.将npm设置为淘宝镜像:npmconfigsetregistryhttps://registry.npm.taobao.org3.查看cnpm镜像设置:cnpmconfiggetregistry

    2022年4月10日
    359
  • ikbc 104键win键失效

    ikbc 104键win键失效fn+del键(或者fn+r)长按五秒,看到3个指示灯闪烁,说明键盘恢复出厂设置了,也解决了当前问题。

    2022年6月4日
    182

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号