Springboot整合mybatis框架
前言
SpringBoot快速整合mybatis框架
提示:以下是本篇文章正文内容,下面案例可供参考
一、创建spring boot工程
新建一个springboot工程
springboot新建成功后的目录
二、整合mybatis框架
1.引入依赖
打开pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ly.springboot</groupId>
<artifactId>10-srpingboot-mybatis02</artifactId>
<version>1.0.0</version>
<name>10-srpingboot-mybatis02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.假如mybati所需的依赖文件
分别是驱动和mybatis的配置文件
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
另外还需在< build >标签里面配置上表示扫描src/main/java目录下的所有文件
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.java</include>
</includes>
</resource>
</resources>
3.打开application.properties配置数据库链接
#配置端口号
server.port=8080
#配置路径
server.servlet.context-path=/test
#配值访问的数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springdb?serverTimezone=GMT%2B8
spring.datasource.password=root
spring.datasource.username=root
1.可以配置多个application.properties文件但是必须是application开头然后
4.到这里就算整合完毕了基本下面使用mybatis逆向工程自动创建实体类和mapper接口mapper文件
1.在文件目录下创建GeneratorMapper.xml文件
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="C:\mysql-connector-java-8.0.22.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://localhost:3306/springdb?serverTimezone=GMT%2B8&characterEncoding=utf-8&autoReconnect=true"
userId="root"
password="root">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!--生成model 类,targetPackage 指定 model 类的包名,targetProject 指定
生成的 model放在eclipse的哪个工程下面-->
<javaModelGenerator targetPackage="com.nylg.mybatis.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.nylg.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成 MyBatis的 Mapper接口类文件,targetPackage 指定 Mapper 接口类的包名,targetProject 指定生成的 Mapper 接口放在eclipse 的哪个工程下面
-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.nylg.mybatis.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" />
<table tableName="city" domainObjectName="City"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" />
<table tableName="province" domainObjectName="Province"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" />
<!--
<table tableName="user" domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" />-->
</context>
</generatorConfiguration>
2.pom.xml文件中配置逆向工程插件代码
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>GeneratorMapper.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
3.好了这里就配置完逆向工程所需要的东西了下面操作
4.效果图
这里也可以将所有的mapper.xml文件放在resources文件目录下
然后再application.properties中指定就可以
//指定方式
mybatis.mapper-locations=classpath:mapper/*.xml
总结
以上就是SpringBoot整合mybatis开发并且利用mybatis逆向工程生成对应的dao和pojo类