maven + springboot 集成Mybatis的完整实例

1、在这里插入图片描述在这里插入图片描述

2、pom.xml驱动包依赖添加

   <!--	集成springboot的父级项目的依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!--        mybatis 起步依赖 整合springboot-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!--        mysql jdbc 驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
         <build>
        <plugins>
            <plugin>
                <!--            mybatis 代码自动生成插件-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <configurationFile>src/main/resources/GeneratorMapper.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
         <resources>
            <resource>
<!--                指定resource插件要处理哪个目录下的资源文件-->
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

3、rwsources->新建application.yml,文件配置:

server:
  port: 8099
  servlet:
    context-path: /mybatis

mybatis:
  mapper:
    #locations: classpath:com/bj/springboot/mapper/*.xml
    locations: classpath:/mapper/*.xml
#    配置数据库连接信息
spring:
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.12.203:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC

4、resouces->新建GeneratorMapper.xml,文件配置:

<?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:\jar\mysql-connector-java-5.1.47/mysql-connector-java-5.1.47.jar"/>

    <!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
    <context id="tables" targetRuntime="MyBatis3">
        <!--序列化-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>

        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>


        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.12.203:3306/test"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- 生成model类,targetPackage指定model类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
        <javaModelGenerator targetPackage="com.bj.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放在eclipse的哪个工程下面 -->
        <sqlMapGenerator targetPackage="com.bj.springboot.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.bj.springboot.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

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

5、pom.xml文件中添加mybatis代码自动生成的插件:

    <build>
        <plugins>
            <plugin>
                <!--            mybatis 代码自动生成插件-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!--                    配置文件的位置-->
                    <configurationFile>src/main/resources/GeneratorMapper.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

6、maven中查找mybatis-generator:generate,进行build代码。具体详见链接【运行生成自动代码】
在这里插入图片描述
Mybatis自动生成代码成功!
根据GeneratorMapper.xml生成了三个文件:model下的Student类文件(主要是student表的字段get、set方法),mapper下的StudentMapper接口文件,StudentMapper.xml文件.。具体细节了解请点击【Mybatis自动生成代码-GeneratorMapp

!!!!!!!!!!!重点来啦!!!!!!!!
【调用】(以查询数据库返回所有数据列表为例):
1、StudentMapper。xml文件中绑定sql语句
在这里插入图片描述
2、接口中建立getAllStudent方法,注意:保证该接口名字与步骤1中的sql 的id名称一致。注意:该接口要设置@Mapper注解便于能够被自动扫描到或者在运行的主类上添加@MapperScan(“com.bj.springboot.mapper”)注解包。
在这里插入图片描述
3、创建接口service中的getAllStudent方法
在这里插入图片描述
4、继承service接口实现方法。注入StudentMapper,调用步骤2中Studentmapper的getAllStudent方法。
在这里插入图片描述
4、外部调用接口实现。。注意:该类方法需要添加注解@Controller,并且注入步骤4的类对象(studentService),然后调用该类对象的getAllstudent方法。
在这里插入图片描述
5、主类中设置如下:在这里插入图片描述
6、运行主类程序:可以查看到端口号
在这里插入图片描述
7、postman调用该接口:
在这里插入图片描述
调用成功
结束!!

发布了14 篇原创文章 · 获赞 3 · 访问量 309

猜你喜欢

转载自blog.csdn.net/xiaomin1328/article/details/104940282