欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > SpringBoot集成Mybatis(包括Mybatis-Plus)和日志

SpringBoot集成Mybatis(包括Mybatis-Plus)和日志

2026/5/2 23:38:52 来源:https://blog.csdn.net/qq_45923849/article/details/145970616  浏览:    关键词:SpringBoot集成Mybatis(包括Mybatis-Plus)和日志

一、使用Mybatis

1.添加依赖

   <!--Mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version> <!-- 选择与Java 8兼容的版本 --></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version> <!-- 请根据需要调整版本号 --></dependency><!--注解相关--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>

2.添加配置

spring:datasource:url: jdbc:mysql://192.168.101.128:3306/test1?useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: abcdefdriver-class-name: com.mysql.cj.jdbc.Driver
mybatis:# 配置mapper.xml文件位置mapper-locations: classpath:mapper/*.xml# 开启自动驼峰映射configuration:map-underscore-to-camel-case: true

3.然后就能使用了

例如:

实体类

package com.example.springbootdemo3.model;import org.springframework.data.annotation.Id;import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import java.time.LocalDateTime;/*** description** @author PC 2025/03/02 19:26*/
@Table(name = "user_test")
public class UserTest {@Id // JPA annotation for marking the primary key@GeneratedValue(strategy = GenerationType.IDENTITY) // Auto increment strategyprivate Integer id;@Column(name = "name", length = 255)private String name;@Column(name = "age")private Integer age;@Column(name = "create_date")private LocalDateTime createDate;@Column(name = "update")private LocalDateTime lastUpdate;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public LocalDateTime getCreateDate() {return createDate;}public void setCreateDate(LocalDateTime createDate) {this.createDate = createDate;}public LocalDateTime getLastUpdate() {return lastUpdate;}public void setLastUpdate(LocalDateTime lastUpdate) {this.lastUpdate = lastUpdate;}
}

java接口

package com.example.springbootdemo3.mapper;import com.example.springbootdemo3.model.UserTest;
import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** description** @author PC 2025/03/02 20:52*/
@Mapper
public interface UserTestMapper {List<UserTest> selectAllUsers();
}

xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.springbootdemo3.mapper.UserTestMapper"><!-- 查询所有用户 --><select id="selectAllUsers" resultType="com.example.springbootdemo3.model.UserTest">SELECT id, `name`, age, create_date, last_updateFROM user_test</select>
</mapper>

测试

package com.example.springbootdemo3.controller;import com.example.springbootdemo3.mapper.UserTestMapper;
import com.example.springbootdemo3.model.UserTest;
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;/*** description** @author PC 2025/03/02 20:54*/
@RestController
@RequestMapping("/date-test")
public class DateDourceController {@Autowiredprivate UserTestMapper userTestMapper;@GetMapping("/get-all")public List<UserTest> getAll() {return userTestMapper.selectAllUsers();}}

二、使用Mybatis-Plus(和上面二选一即可)

1.添加依赖

 <!--Mybatis Plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3</version> <!-- 请根据需要选择最新版本 --></dependency><!-- MySQL连接器 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version> <!-- 请根据需要调整版本号 --></dependency><!--注解相关--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>

2.添加配置

spring:datasource:url: jdbc:mysql://192.168.101.128:3306/test1?useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: abcdefdriver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:# 配置mapper.xml文件位置mapper-locations: classpath:mapper/*.xml# 开启自动驼峰映射configuration:# 配置驼峰命名map-underscore-to-camel-case: true

3.然后就可以用了

package com.example.springbootdemo3.controller;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootdemo3.mapper.UserTestMapper;
import com.example.springbootdemo3.model.UserTest;
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;/*** description** @author PC 2025/03/02 20:54*/
@RestController
@RequestMapping("/date-test")
public class DateDourceController {@Autowiredprivate UserTestMapper userTestMapper;@GetMapping("/getByName")public List<UserTest> getList(String name) {QueryWrapper<UserTest> queryWrapper = new QueryWrapper<>();queryWrapper.like("name", name);return userTestMapper.selectList(queryWrapper);}
}

三、日志(包含Mybatis-plus日志)

1.加依赖

<!-- SLF4J API -->
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency>
<!-- Logback Classic -->
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId>
</dependency>

2.加配置(搞定)

mybatis-plus:configuration:# 配置日志实现类log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
logging:level:com.example.springbootdemo3: INFOcom.baomidou.mybatisplus: DEBUGcom.zaxxer.hikari: INFOcom.example.springbootdemo3.mapper: DEBUG

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词