springboot + mybatisPlus 入门实例 入门demo

springboot + mybatisPlus 入门实例 入门demo

使用mybatisPlus的优势

集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用mapper的xml文件。

mybatisPlus官网: https://mp.baomidou.com/guide

项目搭建前提条件

1、IDEA,并且已安装lombok插件
2、mysql数据库,本实例采用mysql8.0版本,与mysql5.x版本有些不一样,如果使用mysql5.x,只需要修改相应的配置即可。

1. 导入相关依赖

如果项目中有mybatis依赖可以直接删除了,mybatisPlus里包含了mybatis的依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>


<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.3.2version>
dependency>





<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.20version>
dependency>

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.21version>
dependency>

<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <optional>trueoptional>
dependency>

2. yml 配置

server:
  port: 8080
  servlet:
    context-path: /
  tomcat:
    uri-encoding: UTF-8

spring:
  datasource:
    # mysql5.x 配置,高版本需要加useSSL=false
    #url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
    # mysql8.0 需要加&useSSL=false&serverTimezone=UTC
    url: jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: 123456
    # mysql8.0 驱动
    driver-class-name: com.mysql.cj.jdbc.Driver
    # mysql5.x 驱动
    #driver-class-name: com.mysql.jdbc.Driver
    debug: false
    #Druid#
    name: test
    type: com.alibaba.druid.pool.DruidDataSource
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

#-----------------------------------------------------
# pom.xml引用 mybatis-plus包,需要这样配置
#mybatis:
#  type-aliases-package: com.example.demo.model
#  mapper-locations: classpath:mybatis/mappers/*.xml
#  configuration:
#    map-underscore-to-camel-case: true
#-----------------------------------------------------

#-----------------------------------------------------
# pom.xml引用 mybatis-plus-boot-starter包,需要这样配置
mybatis-plus:
  type-aliases-package: com.example.demo.model
  mapper-locations: classpath:mybatis/mappers/*.xml
  configuration:
    map-underscore-to-camel-case: true
#-----------------------------------------------------

3. 代码

User类

@TableId(type = IdType.AUTO) 表示我们使用自增主键

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.ToString;
import java.util.Date;

@Data
@ToString
@TableName("user_demo")
public class User {

    @TableId(type = IdType.AUTO)
    private Integer id;

    @TableField("name")
    private String name;

    @TableField("password")
    private String password;

    @TableField("age")
    private Integer age;

    @TableField("email")
    private String email;

    /**
     * 注意:此处如果不加 @TableField注解,mybatisPlus会默认将createTime映射成 create_time字段
     * 由于数据库字段为createTime,所以需要指定
     */
    @TableField("createTime")
    private Date createTime;
}

mapper类

继承BaseMapper就可以,会把一些crud的操作帮我们自动生成注入。

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.model.User;

public interface UserMapper extends BaseMapper<User> {
    public User findUserByName(String name);

}

Controller类

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据ID查找
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public String getUserById(@PathVariable String id){
        User user = userMapper.selectById(id);
        return "hello " + user.getName();
    }

    /**
     * 根据name查找
     * @param name
     * @return
     */
    @GetMapping("/name/{name}")
    public String getUserByName(@PathVariable String name){
        User user = userMapper.findUserByName(name);
        return "getUserByName " + user.getName();
    }

}

UserMapper.xml



<mapper namespace="com.example.demo.mapper.UserMapper">
    <resultMap id="userResultMap" type="com.example.demo.model.User" >
        <id column="id" property="id"  />
        <result column="name" property="name" />
        <result column="password" property="password"  />
        <result column="age" property="age"  />
        <result column="email" property="email"  />
        <result column="createTime" property="createTime"  />
    resultMap>

    <select id="findUserByName" resultMap="userResultMap">
        SELECT
        id,
        name,
        password,
        age,
        email,
        createTime
        FROM user_demo
        where name = #{name}
    select>

mapper>

实例运行效果

浏览器输入:http://localhost:8080/user/name/aaa
springboot + mybatisPlus 入门实例 入门demo_第1张图片

你可能感兴趣的:(java技术,springboot,mybatisPlus,spring,boot,mysql,mybatis)