Git Product home page Git Product logo

springboot-mybatis-demo's Introduction

结合 Cloud Study 的 Mybatis 的简易 demo

项目结构如下

项目结构

其中 main 下面的 mapper 里面都是接口类,相当于 JPA 中的 repository;实体类不用加各种注解,其余 2 个都和 JPA 一样。

resources 下面的 mapper 放的是 mybatis 映射配置文件 (xml)。

pom.xml

使用 mybatis 需要以下两个依赖。

<!-- mysql -->

<dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <scope>runtime</scope>

</dependency>

<!-- mybatis -->
<dependency>

    <groupId>org.mybatis.spring.boot</groupId>

    <artifactId>mybatis-spring-boot-starter</artifactId>

    <version>2.1.1</version>

</dependency>

application.yml

创建新项目时默认的项目配置文件是 application.properties,这里改为用 yml 文件,本质是一样的,格式不同而已。

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/CloudStudy?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 111
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapper/*.xml
server:
  port: 8088

datasource 配置数据库的一些信息,这里我连的是自己的数据库。

mapper-locations 就是 resources 下 mapper 文件夹内的所有文件。

UserMapper.xml

<mapper namespace="com.whu.cloudstudy_server.mapper.UserMapper">

    <select id="findUserByEmail" resultType="com.whu.cloudstudy_server.entity.User">
        SELECT * FROM user WHERE email = #{email}
    </select>

    <select id="findUserByName" resultType="com.whu.cloudstudy_server.entity.User">
        SELECT * FROM user WHERE name = #{name}
    </select>
</mapper>

一个映射文件对应 main 中的一个映射接口。

mapper 是根标签,里面可以有 select, insert, update, delete 子标签,标签内容写 sql 语句。

id 属性就是映射接口中定义的方法名。

测试

数据库中添加一条记录

数据库中添加一条记录

Postman中测试

Postman中测试

参考网址

基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

springboot系列 | 与mybatis整合

Mybatis中的update动态SQL语句

[Mybatis 教程](https://www.w3cschool.cn/mybatis/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.