SpringBoot 集成mybatis完整流程

本文详细介绍了如何在SpringBoot项目中集成MyBatis,包括配置Maven依赖、自动生成Java类、Mapper接口及Mapper.xml文件,以及完成数据查询的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot 集成mybatis,使用maven自动生成工具,生成数据库表对应Java类,Mapper接口及Mapper.xml

 

1.Pom.xml中配置依赖和mybatis自动生成工具,生成数据库表对应Java类,Mapper接口及Mapper.xml

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- mybatis springboot 起步依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
 <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                   <configurationFile>src/main/resources/mybatis-generator/generateMapper.xml
                   </configurationFile>
                   <verbose>true</verbose>
                   <overwrite>true</overwrite>
                </configuration>
 </plugin>

 

2.配置Pom.xml编译配置(不是必须,idea中xml文件不进行编译配置,target目录中找不到.xml文件,导致报错找不到某个sql,使用eclipse可以跳过此步) ,build标签下添加如图代码:

       <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

 

3.在springboot核心配置文件中配置mapper.xml文件的路径和数据源信息

server.port=9090
server.servlet.context-path=/springboot_mybatis_demo

mybatis.mapper-locations=classpath:com/mybatis/springboot/mapper/*.xml

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootdb?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

 

4.配置Mybatis 生成文件的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!-- 指定连接数据库的JDBC驱动包所在位置,指定本机完整路径 -->
    <classPathEntry location="C:/Users/liyang/.m2/repository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar"/>
    <!-- 配置table表信息内容体,targetRuntime指定采用Mybatis3的版本-->
    <context id="tables" targetRuntime="MyBatis3">
        <!--抑制注释生成,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <!--<property name="suppressDate" value="false"/>-->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/springbootdb"
                        userId="root"
                        password="root"/>
        <!--生成Model类,targetPackage指定model类的包名,targetProject指定生成的model
         放在开发工具的哪个工程下面-->
        <javaModelGenerator targetPackage="com.mybatis.springboot.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>
        <!--生成Mybatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名,targetProject指定生成的
        mapper.xml放在开发工具的哪个工程下面 -->
        <sqlMapGenerator targetPackage="com.mybatis.springboot.mapper"
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--生成Mybatis的Mapper接口文件,targetPackage指定Mapper接口文件的包名,targetProject指定生成的
               Mapper接口放在开发工具的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.springboot.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>


        <!--数据库表名及对应的Java Model类名-->
        <table tableName="tb_student" domainObjectName="Student"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

tb_student表结构如下图,可视化工具注册比较麻烦,凑合看下。

 

5.双击如图所示位置:

 我的报了个错:(无报错可跳过该部分)

 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure   either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you     want to utilize time zone support.

解决方法:给time_zone设置值

再次双击生成:

6.在StudentMapper中添加findAll

<?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.mybatis.springboot.mapper.StudentMapper">
  <resultMap id="BaseResultMap" type="com.mybatis.springboot.model.Student">
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
  </resultMap>

  <select id="findAll" resultMap="BaseResultMap">
    select s.id,s.name,s.age from tb_student s

  </select>
</mapper>

 7.给生成的Mapper接口文件添加注解@Mapper

@Mapper
public interface StudentMapper {
    int insert(Student record);

    int insertSelective(Student record);

    List<Student> findAll();
}

8.定义service 接口与实现类

public interface StudentService {
    List<Student> findAll();
}
package com.mybatis.springboot.service;

import com.mybatis.springboot.mapper.StudentMapper;
import com.mybatis.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper mapper;

    @Override
    public List<Student> findAll() {
        return mapper.findAll();
    }

}

9.定义controller

package com.mybatis.springboot.controller;

import com.mybatis.springboot.service.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ListController {
    @Autowired
    private StudentServiceImpl service;
    @GetMapping("/list/findAll")
    public Object findStudents() {
        return service.findAll();
    }
}

10.启动springboot,访问浏览器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值