温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot中如何使用Mybatis

发布时间:2022-04-07 14:46:52 来源:亿速云 阅读:651 作者:iii 栏目:编程语言

本篇内容主要讲解“springboot中如何使用Mybatis”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot中如何使用Mybatis”吧!

springboot集成Mybatis

第一步:

添加Mybatis依赖

<!--mybatis整合springboot框架的起步依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

第二步:

添加mysql驱动
之所以没有版本号,因为它继承的是父工程的。当然你也可以自己指定一个版本号

<!--添加mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!-- 指定版本号 <version>5.1.9<version> -->
</dependency>

springboot中如何使用Mybatis

使用Mybatis提供的逆向工程 生成实体bean,映射文件,DAO接口

第一步:

在项目根目录创建 GeneratorMapper.xml 文件,配置如下:

springboot中如何使用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="D:\biancheng_files\mysql_connect_java\mysql-connector-java-5.1.38.jar"/>
    <!--配置table表信息内容体,targetRuntime 指定采用MyBatis3的版本-->
    <context id="tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--配置数据库连接信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/spring?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"
                        userId="root"
                        password="lvxingchen">
        </jdbcConnection>
        <!--生成Model类,targetPackage指定model类的包名,
        targetProject指定生成的model类放在eclipse的哪个工程下边、-->
        <javaModelGenerator targetPackage="com.lxc.boot_02.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放在eclipse的哪个工程下边-->
        <sqlMapGenerator targetPackage="com.lxc.boot_02.mapper"
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--生成Mybatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名,
        targetProject指定生成的Mapper接口放在eclipse的哪个工程下边-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.lxc.boot_02.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
 
        <!--数据库表名及对应的Java模型类名
        有100张表,就需要指定100个table
        tableName:数据库中表的名字;
        domainObjectName:表对应生成的实体类的名字叫什么
        -->
        <table tableName="user" domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

第二步
在pom.xml中配置如下:

<!--在plugins标签中,添加如下代码-->
<!--mybatis 代码自动生成插件-->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
    </dependencies>
    <configuration>
        <!--配置文件的位置-->
        <configurationFile>GeneratorMapper.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

第三步
双击如下执行时,

springboot中如何使用Mybatis

这里有一个坑,我的在这里报错了,报错信息如下:

Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate (default-cli) on project sprint_boot_01: Communications link failure

springboot中如何使用Mybatis

百分之99是驱动和配置数据库信息中的connectionURL配置错了,我的解决方案:

driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/java_pro?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"

然后在双击执行,成功:

springboot中如何使用Mybatis

 生成的目录如下:

springboot中如何使用Mybatis

到此,相信大家对“springboot中如何使用Mybatis”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI