Mybatis和Mybatis-Plus的配置

2023-10-26

目录

一、springMVC中Mybatis的配置

1、添加 MyBatis 和 MyBatis-Spring 的依赖

 2、配置数据源

3、配置 MyBatis

4、编写 Mapper 接口和对应的 XML 文件

 二、springnboot里mybatis的yml配置

1、添加 MyBatis 和 MyBatis-Spring-Boot-Starter 依赖

2、配置数据源

3、编写 Mapper 接口和对应的 XML 文件

4、编写对应的 XML 文件 userMapper.xml

三、springmvc里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖

2、配置数据源

 3、使用 MyBatis-Plus 提供的注解和功能

 4、运行 Spring MVC 

四、springboot里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖

2、配置数据源

3、使用 MyBatis-Plus 提供的注解和功能

 4、运行 Spring MVC 

控制台显示sql日志


一、springMVC中Mybatis的配置

1、添加 MyBatis 和 MyBatis-Spring 的依赖
<dependencies>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <!-- MyBatis-Spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring.version}</version>
    </dependency>
</dependencies>
 2、配置数据源

application.propertiesapplication.yml 中添加数据源的配置。例如,使用 MySQL 数据库的配置示例如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password
3、配置 MyBatis

application-context.xml 中添加 MyBatis 的配置信息,示例如下:

<!-- MyBatis 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.example.entity"/>
    <property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example.mapper"/>
</bean>

这里的 typeAliasesPackage 指定实体类所在的包名, mapperLocations 指定 Mapper 文件的位置。 MapperScannerConfigurer 用于指定 Mapper 接口所在的包名,这里的 com.example.mapper 是示例的包名,你需要替换为实际使用的包名。

4、编写 Mapper 接口和对应的 XML 文件

定义 Mapper 接口

// 定义 Mapper 接口
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    public User getUserById(int id);
}

对应的 XML 文件 userMapper.xml

// 对应的 XML 文件 userMapper.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.example.mapper.UserMapper">
    <select id="getUserById" resultType="com.example.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>

 二、springnboot里mybatis的yml配置

1、添加 MyBatis 和 MyBatis-Spring-Boot-Starter 依赖
<dependencies>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <!-- MyBatis-Spring-Boot-Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.starter.version}</version>
    </dependency>
</dependencies>
2、配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password
3、编写 Mapper 接口和对应的 XML 文件
// 定义 Mapper 接口
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    User getUserById(int id);
}
4、编写对应的 XML 文件 userMapper.xml
// 对应的 XML 文件 userMapper.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.example.mapper.UserMapper">
    <select id="getUserById" resultType="com.example.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>

三、springmvc里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖
<dependencies>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.plus.version}</version>
    </dependency>
</dependencies>
2、配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password

# 配置MyBatis-Plus属性
mybatis-plus.mapper-locations=classpath*:/mapper/**/*Mapper.xml
mybatis-plus.type-enums-package=com.example.enums
 3、使用 MyBatis-Plus 提供的注解和功能
// 定义实体类
@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private Integer age;
}
// 定义 Mapper 接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 可以直接使用继承自 BaseMapper 的方法,不需要编写具体的 SQL 语句
}
 4、运行 Spring MVC 

注意:MyBatis-Plus 默认情况下会根据实体类的命名规则映射到数据库中相应的表,如果实体类和表名不一致,可以使用 @TableName 注解进行指定。同时,@TableId 注解指定了主键的生成策略。

四、springboot里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖
<dependencies>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.plus.version}</version>
    </dependency>
</dependencies>
2、配置数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=your_username
spring.datasource.password=your_password

# 配置MyBatis-Plus属性
mybatis-plus.mapper-locations=classpath*:/mapper/**/*Mapper.xml
mybatis-plus.type-enums-package=com.example.enums
3、使用 MyBatis-Plus 提供的注解和功能
// 定义实体类
@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private Integer age;
}
// 定义 Mapper 接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 可以直接使用继承自 BaseMapper 的方法,不需要编写具体的 SQL 语句
}
 4、运行 Spring MVC 

注意:MyBatis-Plus 默认情况下会根据实体类的命名规则映射到数据库中相应的表,如果实体类和表名不一致,可以使用 @TableName 注解进行指定。同时,@TableId 注解指定了主键的生成策略。

控制台显示sql日志

 springboot配置mybatis 控制台显示sql日志,在application.properties

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

springboot配置mybatis-plus 控制台显示sql日志,在application.properties

logging.level.com.baomidou.mybatisplus=DEBUG
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mybatis和Mybatis-Plus的配置 的相关文章

随机推荐

  • 页面中引入高德地图

    一 打开高德开放平台网站https lbs amap com 点击右上角注册 如下图所示 二 按要求填写注册信息 三 注册完成之后 进入控制台 点击右上角 创建新应用 名称随便写 如下图所示 四 在应用右上角点击 添加 新增key 五 将新
  • ES6知识点总结三:数组的扩展(扩展运算符···、Array.from()、Array.of()...)

    4 数组扩展之扩展运算符 扩展运算符 spread 是三个点 它好比 rest 参数的逆运算 将一个数组转为用逗号分隔的参数序列 var arr 1 2 3 9 8 arr 9 8 1 2 3 a 1 2 3 a 1 2 空数组不会有有变化
  • 赞!一篇博客讲解清楚 Python queue模块,作为Python爬虫预备知识,用它解决采集队列问题

    Queue 模块 只要涉及到多线程爬虫 就会涉及到数据采集队列的优先级问题 在 Python 中 Queue 模块提供了一个同步的 线程安全的队列类 它包括常见的 FIFO 先入先出 LIFO 后入先出 PriorityQueue 按优先级
  • 【JAVA】代码规范

    一 命名规约 1 强制 所有编程相关命名均不能以下划线或美元符号开始 也不能以下划线或美元符号结束 反例 name name Object name name Object 2 强制 类名使用UpperCamelCase风格 必须遵从驼峰形
  • 计算机传输速率127,计算机基础知识2

    3 提高可靠性 计算机网络中的各台计算机可以通过网络互相设置为后备机 一旦某台计算机出现故障时 网络中的后备机即可代替继续执行 保证任务正常完成 避免系统瘫痪 从而提高了计算机的可靠性 4 分担负荷 当网上某台计算机的任务过重时 可将部分任
  • visjs 数据的增删改筛选(DataSet & DataView)

    visjs中的 DatatSet 用于数据的保存 可操作非结构化数据 并可监听数据的变化 DataSet构造 var data new vis DataSet data options 构造后 可通过 add updateOnly upda
  • Pandas知识点-DataFrame数据结构介绍

    Pandas知识点 DataFrame数据结构介绍 一 Pandas简介和安装 Pandas是Python中用于数据处理和数据分析的开源库 2008年由金融数据分析师Wes McKinney开发 开发Pandas的初衷是为了方便进行金融数据
  • 如何在pycharm中使用git

    一 环境配置 1 下载安装git 安装成功后 打开git bash here输入以下命令配置基础信息 git config global user name 用户名 git config global user name 邮箱地址 输入命令
  • 关于配置Golang语言环境中的一些问题以及配置yum源的方法

    关于配置Go语言环境中的一些问题 坚持不做无用的 重复的 混字数的博客 所以主要谈及安装使用过程中遇到的问题 具体安装参考博客 https pmlpml github io ServiceComputingOnCloud ex instal
  • SpringBoot中注入RedisTemplate报空指针

    现象 RedisTemplate不能作为静态变量注入 否则报空指针 错误做法 private static RedisTemplate
  • Pandas 中 SettingwithCopyWarning 的原理和解决方案

    Pandas 中 SettingwithCopyWarning 的原理和解决方案 原文链接 https www dataquest io blog settingwithcopywarning 原文标题 Understanding Sett
  • 防御第二天-防火墙演示实验

    1 上课思维导图 2 防火墙演示实验 防火墙FW1 原用户名 admin 原密码 Admin 123 配地址
  • 狂神docker学习笔记

    文章目录 前言 Docker 概述 1 Docker 为什么会出现 2 Docker 历史 3 Docker 能干嘛 Docker 安装 1 Docker的基本组成 2 安装Docker 3 阿里云镜像加速 4 回顾HelloWorld流程
  • Application is running inside IIS process but is not configured to use IIS server

    删除Program webBuilder UseKestrel
  • JSON字符串转换成List对象集合

    简单说下 有一个json字符串 我想通过jackson把json字符串转换成list对象集合 网上找了很多 但都不尽人意 后来还是看jackson文档 才知道怎么做 需要的包
  • ubuntu20.04配置安装frp内网穿透

    1 frp所在的github地址 https github com fatedier frp 2 下载 wget https github com fatedier frp releases download v0 38 0 frp 0 3
  • from keras.engine.topology import Layer 无此模块问题

    这可以说是深度学习必踩坑 就是版本问题 复现别人得代码时出现得问题 一开始没发现这篇博文 在GitHub上找了一圈都没找到这个引入 还走了弯路 以为是新版本包不一样了 修改也不可行 还是见识少了 这篇博客没营养 只作踩坑记录 参考博客 Ke
  • 什么是测试开发工程师(SET)?

    经常有人问到 什么是 软件测试开发工程师 Software Engineers in Test 缩写为SET 借用Google的规范来说其实就是 在测试中的软件工程师 其工作性质上首先是测试 然后才是开发 那么这里会让大家产生一个矛盾的感觉
  • Quick - Hello World

    文章目录 背景 谈一谈为我什么学QtQuick 环境搭建 Qt 安装 VS2019 安装 Qt Visual Studio Tools Hello World pro main cpp main qml 运行效果 参考鸣谢 背景 Qt4自2
  • Mybatis和Mybatis-Plus的配置

    目录 一 springMVC中Mybatis的配置 1 添加 MyBatis 和 MyBatis Spring 的依赖 2 配置数据源 3 配置 MyBatis 4 编写 Mapper 接口和对应的 XML 文件 二 springnboot