SpringBoot整合Mybatis-Puls

简介

MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为 简化开发、提高效率而生。

1.在XML文件中引入依赖

        <!--mybatis-plusqu驱动--> 
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <!--mysql连接驱动-->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

2.properties.yml文件中配置数据库连接池

#这里以mysql8为例
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/所要连接的mysql数据库名称?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC

数据中的表(student):

创建数据库表对应的实体类

/**
 * 
 * @TableName student
 */
@TableName(value ="student")
@Data
public class Student implements Serializable {
    /**
     * 
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 
     */
    private String username;

    /**
     * 
     */
    private String password;

    /**
     * 
     */
    private String sex;

    /**
     * 
     */
    private String email;

    /**
     * 
     */
    private Long phone;

    /**
     * 
     */
    private String school;

    /**
     * 
     */
    private Date createTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}

添加mapper

import com.gyk.spss.domain.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
  *BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,
  *泛型为操作的实体类型
  *BaseMapper<T> 泛型T的类型是表所对应的实体类名称,这里是Student类
  */
public interface StudentMapper extends BaseMapper<Student> {

}

在resource目录下创建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.gyk.spss.mapper.StudentMapper"><!--namespace="Mapper所对应类的包路径"-->

    <resultMap id="BaseResultMap" type="com.gyk.spss.domain.Student">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="username" column="username" jdbcType="VARCHAR"/>
            <result property="password" column="password" jdbcType="VARCHAR"/>
            <result property="sex" column="sex" jdbcType="CHAR"/>
            <result property="email" column="email" jdbcType="VARCHAR"/>
            <result property="phone" column="phone" jdbcType="BIGINT"/>
            <result property="school" column="school" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,username,password,
        sex,email,phone,
        school,create_time
    </sql>
</mapper>

创建service及实现接口

import com.gyk.spss.domain.Student;
import com.baomidou.mybatisplus.extension.service.IService;

/**
* @author Optimisticlt
* @description 针对表【student】的数据库操作Service
*/
public interface StudentService extends IService<Student> {

}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyk.spss.domain.Student;
import com.gyk.spss.service.StudentService;
import com.gyk.spss.mapper.StudentMapper;
import org.springframework.stereotype.Service;

/**
* @author Optimisticlt
* @description 针对表【student】的数据库操作Service实现
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

}

结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值