加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring boot mybatis 整合教程

发布时间:2020-12-15 01:51:17 所属栏目:大数据 来源:网络整理
导读:div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post" h2 class="article-copyright" 本项目使用的环境: div id="content_views" class="markdown_views prism-atom-

<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
<h2 class="article-copyright">本项目使用的环境:
<div id="content_views" class="markdown_views prism-atom-one-dark">

  • 开发工具:Intellij IDEA 2017.1.3
  • springboot: 1.5.6
  • :1.8.0_161
  • maven:3.3.9

额外功能

  • PageHelper 分页插件
  • mybatis generator 自动生成代码插件

步骤:

1. 创建一个springboot项目:

这里写图片描述

2. 创建项目的文件结构以及jdk的版本

这里写图片描述

3. 选择项目所需要的依赖

这里写图片描述

这里写图片描述

然后点击finish

4. 看一下文件的结构:

这里写图片描述

5. 查看一下pom.xml:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.winter springboot-mybatis-demo 0.0.1-SNAPSHOT jar springboot-mybatis-demo Demo project Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.6.RELEASE UTF-8 UTF-8 1.7 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java 5.1.35 com.fasterxml.jackson.core jackson-core com.fasterxml.jackson.core jackson-databind com.fasterxml.jackson.datatype jackson-datatype-joda com.fasterxml.jackson.module jackson-module-parameter-names com.github.pagehelper pagehelper-spring-boot-starter 1.1.2 com.alibaba druid-spring-boot-starter 1.1.0 org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 ${basedir}/src/main/resources/generator/generatorConfig.xml

6. 项目不使用application.properties文件 而使用更加简洁的application.yml文件:

将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件,

文件的内容如下:

port: 8080 url: jdbc:mysql: driver-- maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: testOnBorrow: testOnReturn: poolPreparedStatements: maxOpenPreparedStatements: 20 mapper-locations: classpath:mapping

7. 创建数据库:

user_name VARCHAR(255 password VARCHAR(255 phone VARCHAR(255 ) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;

8. 使用mybatis generator 自动生成代码:

  • 配置pom.xml中generator 插件所对应的配置文件 ${basedir}/src/main/resources/generator/generatorConfig.xml
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  • 点击run-Edit Configurations

这里写图片描述

  • 添加配置

这里写图片描述

  • 运行

注意!!!同一张表一定不要运行多次,因为mapper的映射文件中会生成多次的代码,导致报错,切记

这里写图片描述

最后生成的文件以及结构:

这里写图片描述

9. 生成的文件

UserMapper.java

List }

User.java

.userId = .userName = userName == ? .password = password == ? .phone = phone == ? }

对于sql语句这种黄色的背景,真心是看不下去了(解决方案):

这里写图片描述

**UserMapper.xml **

where user_id = #{userId,jdbcType= values (#{userId,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR},#{password,jdbcType= #{phone,jdbcType= < test="userId != null" > > < test="userName != null" > > < test="password != null" > > < test="phone != null" > > < test="userId != null" > #{userId,jdbcType= > < test="userName != null" > #{userName, > < test="password != null" > #{password, > < test="phone != null" > #{phone, > < test="userName != null" > user_name = #{userName, > < test="password != null" > password = #{password, > < test="phone != null" > phone = #{phone, > where user_id = #{userId,jdbcType= set user_name = #{userName, password = #{password, phone = #{phone,jdbcType= where user_id = #{userId,jdbcType=

10. 打开类SpringbootMybatisDemoApplication.java,这个是springboot的启动类。我们需要添加点东西:

@MapperScan("com.winter.mapper") SpringApplication.run(SpringbootMybatisDemoApplication. }

注意:@MapperScan("com.winter.mapper")这个注解非常的关键,这个对应了项目中mapper(dao)所对应的包路径,很多同学就是这里忘了加导致异常的

11. 到这里所有的搭建工作都完成了,接下来就是测试的工作,没使用junit4进行测试:

首先看一下完成之后的文件的结构:

这里写图片描述

现在controller,service层的代码都写好:

UserController.java

@RequestMapping(value = "/user" @RequestMapping(value = "/add",produces = {"application/json;charset=UTF-8" @RequestMapping(value = "/all/{pageNum}/{pageSize}",produces = {"application/json;charset=UTF-8" Object findAllUser(@PathVariable("pageNum") pageNum,@PathVariable("pageSize") }

UserService.java

List findAllUser( pageNum, }

UserServiceImpl.java

@Service(value = "userService" UserServiceImpl UserMapper userMapper; List findAllUser( pageNum, }

如果强迫症看不下去那个报错:(解决方法)

这里写图片描述

测试我使用了idea一个很用心的功能。

可以发http请求的插件

这里写图片描述

这里写图片描述

点击左侧的运行按钮就可以发送请求了;

如果返回值正确 说明你已经搭建成功了!!

ps:如果出现mapper注入不了的情况,请检查版本,当前博客的搭建方法只适合1.5.*版本的。

        </div>



            </div>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!