Springboot整合mybatis框架(含实例Demo)
最近要交⼀份project项⽬作业,前后端分离,我负责后端,选⽤了Springboot+mybatis进⾏整合操作。
⼀.前期准备:
下完上述两样后,IDEA安装略,我们开始使⽤IDEA配置默认的maven库
准备.gif
从GIF中可以看到,我们点击configure菜单项下的Project Defaults来设置项⽬的默认配置,从settings选项中,到Maven库⼀栏,选择我们刚才下载好的maven仓库的安装⽬录。
创建好我们的数据表。数据库名为jiguo,表名为jiguo_user。
数据表.JPG
以上,前期准备就准备好啦。
⼆.项⽬结构⼀栏以及实现步骤。
概览.JPG
实现步骤:
1.Springboot项⽬创建
2.在l中,使⽤maven架包导⼊mybatis依赖dependency
3.逆向⼯程,配置⾃⼰的generatorConfig逆向⼯程的配置⽂件,然后使⽤maven插件执⾏出来,逆向出DAO层还有Model层
4.在application.properties中,配置我们的数据源 mybatis所需要的配置
5.MyMapper接⼝的创建
6.service层代码的创建
7.创建controller,处理前端的请求。
8.启动⽂件中设置DAO层扫描,这样就能识别出DAO层注解
@MapperScan(basePackages = "dao")
步骤⼀:Spr ingbo o t项⽬创建
1.1.JPG
1.2.JPG
1.3.JPG
完成创建⼀个名为demospringboot的项⽬
步骤⼆:在 l中,使⽤ma ven架包导⼊myba tis等依赖
ven架包导⼊m yba
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSche
ma-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId&le.sl</groupId>
<artifactId>demospringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demospringboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
springboot框架的作用
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--alibaba-start-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--alibaba-end-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!--mybatis-start-->
<!--mybatis-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--generator-->
<dependency>
<groupId&ator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!--mapper-->
<dependency>
<groupId&batis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--mybatis-end-->
<!-- 引⼊ redis 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论