springBoot集成mybatis-plus

自动创建springboot项目

配置maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo.example</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置properties文件

spring.application.name=springbootdemo
server.port=8080
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&charaterEncoding=utf-8



创建entity

package com.demo.example.springbootdemo.model;

import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

/**
 * @author :yye
 * @date :Created in 2024/9/23 11:17
 * @description:user实体类
 */
@Data
public class User {
    @TableId
    private String userId;
    private String userName;
    private String userPassword;
    private Integer age;
}

创建controller

package com.demo.example.springbootdemo.controller;

import com.demo.example.springbootdemo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :yye
 * @date :Created in 2024/9/19 14:59
 * @description:UserController
 */
@RestController
public class HelloController {
    @Autowired
    private IUserService userService;

    @GetMapping("add")
    public String addUser() {
        int insert = userService.addUser();
        if (1 == insert) {
            return "保存成功";
        }
        return "保存失败";
    }


}

创建service

接口

package com.demo.example.springbootdemo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.demo.example.springbootdemo.model.User;

/**
 * @author :yye
 * @date :Created in 2024/9/23 11:16
 * @description:user接口
 */
public interface IUserService extends IService<User> {
    int addUser();
}

Impl

package com.demo.example.springbootdemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.example.springbootdemo.dao.UserMapper;
import com.demo.example.springbootdemo.model.User;
import com.demo.example.springbootdemo.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :yye
 * @date :Created in 2024/9/23 14:31
 * @description:user serviceImpl
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public int addUser() {
        User user = new User();
        user.setUserId("001");
        user.setAge(15);
        user.setUserName("admin");
        user.setUserPassword("123456");
        return userMapper.insert(user);
    }
}

创建dao

package com.demo.example.springbootdemo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.example.springbootdemo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * @author :yye
 * @date :Created in 2024/9/23 14:30
 * @description:user mapper
 */
@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

启动项目运行

运行main函数,查看控制台

打开页面访问:

http://localhost:8080/add

运行结果:

查看数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值