spring boot mybatis 整合教程

spring boot mybatis 整合教程,第1张

概述本项目使用的环境:开发工具:Intellij IDEA 2017.1.3springboot: 1.5.6jdk:1.8.0_161maven:3.3.9额外功能PageHelper 分页插件mybatis generator 自动生成代码插件步骤:1. 创建一个springboot项目:2. 创建项目的文件结构以及jdk的版本3. 选择项目所需要的依赖然后点击finish4. 看一下文件的结构:5. 查看一下pom.xml:1 <?xml version="1.0" encoding="UTF-8"?>2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">4 <modelVersion>4.0.0</modelVersion>56 <groupId>com.winter</groupId>7 <artifactId>springboot-mybatis-demo</artifactId>8 <version>0.0.1-SNAPSHOT</version>9 <packaging>jar</packaging>1011 <name>springboot-mybatis-demo</name>12 <description>Demo project for Spring Boot</description>1314 <parent>15 <groupId>org.springframework.boot</groupId>16 <artifactId>spring-boot-starter-parent</artifactId>17 <version>1.5.6.RELEASE</version>18 <relativePath/> <!-- lookup parent from repository -->19 </parent>2021 <properties>22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>24 <java.version>1.7</java.version>25 </properties>2627 <dependencies>28 <dependency>29 <groupId>org.mybatis.spring.boot</groupId>30 <artifactId>mybatis-spring-boot-starter</artifactId>31 <version>1.3.0</version>32 </dependency>33 <dependency>34 <groupId>org.springframework.boot</groupId>35 <artifactId>spring-boot-starter-thymeleaf</artifactId>36 </dependency>37 <dependency>38 <groupId>org.springframework.boot</groupId>39 <artifactId>spring-boot-starter-web</artifactId>40 </dependency>4142 <dependency>43 <groupId>org.springframework.boot</groupId>44 <artifactId>spring-boot-starter-test</artifactId>45 <scope>test</scope>46 </dependency>47 <dependency>48 <groupId>mysql</groupId>49 <artifactId>mysql-connector-java</artifactId>50 <version>5.1.35</version>51 </dependency>525354 <dependency>55 <groupId>com.fasterxml.jackson.core</groupId>56 <artifactId>jackson-core</artifactId>57 </dependency>58 <dependency>59 <groupId>com.fasterxml.jackson.core</groupId>60 <artifactId>jackson-databind</artifactId>61 </dependency>62 <dependency>63 <groupId>com.fasterxml.jackson.datatype</groupId>64 <artifactId>jackson-datatype-joda</artifactId>65 </dependency>66 <dependency>67 <groupId>com.fasterxml.jackson.module</groupId>68 <artifactId>jackson-module-parameter-names</artifactId>69 </dependency>70 <!-- 分页插件 -->71 <dependency>72 <groupId>com.github.pagehelper</groupId>73 <artifactId>pagehelper-spring-boot-starter</artifactId>74 <version>1.1.2</version>75 </dependency>76 <!-- alibaba的druid数据库连接池 -->77 <dependency>78 <groupId>com.alibaba</groupId>79 <artifactId>druid-spring-boot-starter</artifactId>80 <version>1.1.0</version>81 </dependency>82 </dependencies>8384 <build>85 <plugins>86 <plugin>87 <groupId>org.springframework.boot</groupId>88 <artifactId>spring-boot-maven-plugin</artifactId>89 </plugin>90 <!-- mybatis generator 自动生成代码插件 -->91 <plugin>92 <groupId>org.mybatis.generator</groupId>93 <artifactId>mybatis-generator-maven-plugin</artifactId>94 <version>1.3.2</version>95 <configuration>96 <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>97 <overwrite>true</overwrite>98 <verbose>true</verbose>99 </configuration>100 </plugin>101 </plugins>102 </build>103104105 &

<div ID="article_content" data-pID="blog" data-mod="popu_307" data-dsm="post">
<h2 >本项目使用的环境:
<div ID="content_vIEws" >

开发工具:Intellij IDEA 2017.1.3springboot: 1.5.6href="https://www.baIDu.com/s?wd=jdk&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd" target="_blank">jdk:1.8.0_161maven:3.3.9额外功能pageHelper 分页插件mybatis generator 自动生成代码插件步骤:



然后点击finish


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


将原有的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

user_name VARCHAR(255 password VARCHAR(255 phone VARCHAR(255 ) ENGINE=INNODB auto_INCREMENT=1000 DEFAulT CHARSET=utf8;

配置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"> @H_957_502@ 点击run-Edit Configurations

添加配置

运行

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

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

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=

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

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


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

现在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>
总结

以上是内存溢出为你收集整理的spring boot mybatis 整合教程全部内容,希望文章能够帮你解决spring boot mybatis 整合教程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)