介绍

从Spring Boot项目名称中的Boot可以看出来,SpringBoot的作用在于创建和启动新的基于Spring框架的项目,它的目的是帮助开发人员很容易的创建出独立运行的产品和产品级别的基于Spring框架的应用。SpringBoot会选择最适合的Spring子项目和第三方开源库进行整合。大部分Spring Boot应用只需要非常少的配置就可以快速运行起来。

SpringBoot包含的特性

  • 创建可以独立运行的Spring应用
  • 直接嵌入Tomcat或Jetty服务器,不需要部署WAR文件
  • 提供推荐的基础POM文件来简化Apache Maven配置
  • 尽可能的根据项目依赖来自动配置Spring框架
  • 提供可以直接在成产环境中使用的功能,如性能指标,应用信息和应用健康检查
  • 没有代码生成,也没有XML配置文件

SpringBoot与Mybatis的完美融合

首先得先创建一个SpringBoot项目,然后基于这个项目在融合进Mybatis

下面给出pom.xml的完整配置:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>20171111</groupId>
  6. <artifactId>springboot</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>springboot</name>
  11. <url>http://maven.apache.org</url>
  12.  
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>1.3.2.RELEASE</version>
  17. <relativePath></relativePath>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. </properties>
  22.  
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.mybatis.spring.boot</groupId>
  26. <artifactId>mybatis-spring-boot-starter</artifactId>
  27. <version>1.1.1</version>
  28. </dependency>
  29.  
  30. <dependency>
  31. <groupId>mysql</groupId>
  32. <artifactId>mysql-connector-java</artifactId>
  33. <version>5.1.6</version>
  34. </dependency>
  35.  
  36. <dependency>
  37. <groupId>junit</groupId>
  38. <artifactId>junit</artifactId>
  39. <version>3.8.1</version>
  40. <scope>test</scope>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-web</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.hibernate</groupId>
  48. <artifactId>hibernate-validator</artifactId>
  49. <version>4.2.0.Final</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>net.sourceforge.nekohtml</groupId>
  57. <artifactId>nekohtml</artifactId>
  58. <version>1.9.22</version>
  59. </dependency>
  60.  
  61. </dependencies>
  62. <build>
  63. <plugins>
  64. <plugin>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-maven-plugin</artifactId>
  67. </plugin>
  68. </plugins>
  69. </build>
  70. </project>

之后创建一个启动类,其实在配置SpringBoot项目的时候就已经创建过一个启动类了,这里只是贴下代码:

  1. package springboot;
  2.  
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
  7. import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
  8.  
  9. @SpringBootApplication
  10. public class HelloworldDemoApplication implements EmbeddedServletContainerCustomizer{
  11. public static void main(String[] args){
  12. SpringApplication.run(HelloworldDemoApplication.class, args);
  13. }
  14.  
  15. public void customize(ConfigurableEmbeddedServletContainer container) {
  16. // TODO �Զ����ɵķ������
  17. container.setPort(8088);
  18. }
  19. }

然后,创建配置文件在resource根目录下创建application.properties文件:

  1. spring.thymeleaf.mode=LEGACYHTML5
  2. spring.datasource.url=jdbc:mysql://10.0.20.252:3306/mybatis
  3. spring.datasource.username=root
  4. spring.datasource.password=Free-Wi11
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  6. server.port=8088

这里的server.port=8088是定义了该项目的端口,如果不指定,则使用默认端口8080

然后定义一个java的实体类,User.java

  1. package com.fpc.Entity;
  2.  
  3. public class User {
  4. private int id;
  5. private String name;
  6. private int age;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public int getAge() {
  20. return age;
  21. }
  22. public void setAge(int age) {
  23. this.age = age;
  24. }
  25.  
  26. }

这里实体类的字段要和数据库的字段对应起来,如果实体类的字段和数据库的字段不相同,则需要使用别名:

这里我们看下数据库中的字段和数据:

  1. mysql> select * from users;
  2. +----+-----------+------+
  3. | id | name | age |
  4. +----+-----------+------+
  5. | 1 | Lily | 24 |
  6. | 2 | b | 27 |
  7. | 5 | userrrrrr | 25 |
  8. +----+-----------+------+
  9. 3 rows in set (0.00 sec)

之后,定义一个dao的接口:

  1. package springboot.Mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.ibatis.annotations.Mapper;
  6. import org.apache.ibatis.annotations.Select;
  7.  
  8. import com.fpc.Entity.User;
  9.  
  10. @Mapper
  11. public interface UserDao {
  12. @Select("select * from users where age=#{age}")
  13. public List<User> get(int age);
  14. }

@Mapper就是我们要与mybatis融合关键的一步,只要一个注解就搞定了。

然后再写一个测试类,也就是Controller:

  1. package springboot.Controller;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11.  
  12. import com.fpc.Entity.User;
  13.  
  14. import springboot.Mapper.UserDao;
  15.  
  16. @Controller
  17. @RequestMapping("/b")
  18. public class UserController {
  19. @Autowired
  20. private UserDao userDao;
  21.  
  22. @RequestMapping("/showDao/{age}")
  23. @ResponseBody
  24. public List<User> show(@PathVariable("age") int age) {
  25. // return userServices.show();
  26. // return "hellllllllllllllllllllllll";
  27. System.out.println(age);
  28. return userDao.get(age);
  29. }
  30.  
  31. // @RequestMapping("/showDao")
  32. // public Object showDao(int age) {
  33. // return userServicesImpl.showDao(age);
  34. // }
  35. }

@RestController是对应的restful风格的控制器,但是我这里直接用的@Controller

运行该项目,Run as - Maven Build

打开浏览器,输入:localhost:8088/b/showDao/27,运行的结果如下:

按名字查找年龄为27的user,如果直接在数据库中查找也会得到一样的结果:

  1. mysql> select * from users where age=27;
  2. +----+------+------+
  3. | id | name | age |
  4. +----+------+------+
  5. | 2 | b | 27 |
  6. +----+------+------+
  7. 1 row in set (0.00 sec)

总结

  1. Controller,Entity,Mapper都要在跟启动类HelloworldDemoApplication在同一个目录下,而且启动类要在外层,项目目录结构会在下面给出。
  2. 声明一个UserDao的接口,在Mapper文件夹下,其中用注解的形式写了SQL语句,而且要在接口的外面协商@Mapper(这是关键)
  3. 在Controller中,直接通过@Autowired将UserDao直接注入到Controller中,然后就可以在Controller中进行操作了。

项目工程目录

SpringBoot与Mybatis整合实例详解的更多相关文章

  1. MyBatis与Spring的整合实例详解

    从之前的代码中可以看出直接使用 MyBatis 框架的 SqlSession 访问数据库并不简便.MyBatis 框架的重点是 SQL 映射文件,为方便后续学习,本节讲解 MyBatis 与 Spri ...

  2. SpringBoot与PageHelper的整合示例详解

    SpringBoot与PageHelper的整合示例详解 1.PageHelper简介 PageHelper官网地址: https://pagehelper.github.io/ 摘要: com.gi ...

  3. 深入浅出mybatis之启动详解

    深入浅出mybatis之启动详解 MyBatis功能丰富,但使用起来非常简单明了,今天我们来追踪一下它的启动过程. 目录 如何启动MyBatis 如何使用MyBatis MyBatis启动过程 如何启 ...

  4. 【python3+request】python3+requests接口自动化测试框架实例详解教程

    转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...

  5. 《深入理解mybatis原理2》 Mybatis初始化机制详解

    <深入理解mybatis原理> Mybatis初始化机制详解 对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程 ...

  6. python+requests接口自动化测试框架实例详解

    python+requests接口自动化测试框架实例详解   转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...

  7. 事件驱动模型实例详解(Java篇)

    或许每个软件从业者都有从学习控制台应用程序到学习可视化编程的转变过程,控制台应用程序的优点在于可以方便的练习某个语言的语法和开发习惯(如.net和java),而可视化编程的学习又可以非常方便开发出各类 ...

  8. Mybatis案例超详解(上)

    Mybatis案例超详解(上) 前言: 本来是想像之前一样继续跟新Mybatis,但由于种种原因,迟迟没有更新,快开学了,学了一个暑假,博客也更新了不少,我觉得我得缓缓,先整合一些案例练练,等我再成熟 ...

  9. MyBatis Mapper XML 详解

    MyBatis Mapper XML 详解 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JD ...

随机推荐

  1. 一条经典SQL语句优化实例

    1.概述 如下SQL语句发生严重消耗资源的问题,使得OS's load average会在30以上,一条语句需要执行上百秒. /*PIXPatient 184176条DomainPatient 184 ...

  2. BZOJ 3922 - Karin的弹幕

    Karin的弹幕 Problem's Link ---------------------------------------------------------------------------- ...

  3. [工具使用] 如何访问github

    1.ping github.com,记录github的ip:192.30.252.129 2.找到系统的 hosts文件位置: C:\Windows\System32\drivers\etc\host ...

  4. 【BZOJ】1637: [Usaco2007 Mar]Balanced Lineup(前缀和+差分+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1637 很神思想.. 前缀和应用到了极点... 我们可以发现当数量一定时,这个区间最前边的牛的前边一个 ...

  5. ThinkPHP项目笔记之模板篇

    顾名思义:模板就是网页页面,有的是静态,有的的动态 基本语法: 1. <li><a href="{:U('User/searchlist')}">返回列表& ...

  6. 『Spring.NET+NHibernate+泛型』框架搭建之DAO(三)★

    本节内容介绍Nhibernate所封装的数据库訪问层.只是我增加了泛型进行封装.大概思路:首先,我们有一个接口层,另一个相应的实现层.在接口层中我们先定义一个父接口,父接口中定义每个接口都可能会用到的 ...

  7. querySelectorAll 和getElementsByClassName的区别

    querySelectorAll 返回的是映射 改变其值不会改变document 而getElementsByClassName 改变它就会改变document 摘自JavaScript权威指南(jQ ...

  8. hdu 1425:sort(排序,经典题。快排模板)

    sort Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  9. Java去除所有非中文字符串

    "fdsfjasd阿斯顿飞机阿斯蒂芬,,,,,,,,....".replaceAll("[^\u4E00-\u9FA5]", "");

  10. Angular2 兼容 UC浏览器、QQ浏览器、猎豹浏览器

    找到/src/polyfills.ts文件 把/** IE9, IE10 and IE11 requires all of the following polyfills. **/下注释掉的代码恢复 ...