SpringBoot简单整合mybatis

2023-12-31 13:38:05

1.?maven依赖

         <!-- mybatis -->     
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

2.?编写Mapper接口和Mapper.xml

package com.example.mybatis.mapper;
import com.example.mybatis.pojo.Demo;
import java.util.List;
public interface DemoMapper {
    List<Demo> selectList();
}
<?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.mybatis.mapper.DemoMapper" >
    <select id="selectList" resultType="com.example.mybatis.pojo.Demo">
        select * from tb_demo
    </select>
</mapper>

3.?启动类添加注解

//配置Mapper接口包
@MapperScan("com.example.mybatis.mapper")

4.?配置文件配置

# 配置数据库url
spring.datasource.url=jdbc:mysql://localhost:3306/learn?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
# 配置数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置数据账号
spring.datasource.username=root
# 配置数据库密码
spring.datasource.password=zproot_zjroot449057
# 配置实体类包
mybatis.typeAliasesPackage=com.example.mybatis.pojo
# 配置mapper.xml位置
mybatis.mapperLocations= classpath:mapper/*.xml     

文章来源:https://blog.csdn.net/qq_41609208/article/details/135314426
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。