1.创建SpringBoot工程

根据

http://www.cnblogs.com/vitasyuan/p/8765329.html

说明创建SpringBoot项目。

2.添加相关依赖

在pom.xml文件中添加数据库连接和mybatis的相关依赖,完整的pom文件如下:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.1.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  10. <java.version>1.8</java.version>
  11. </properties>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>
  22. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>mysql</groupId>
  29. <artifactId>mysql-connector-java</artifactId>
  30. <scope>runtime</scope>
  31. </dependency>
  32. <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
  33. <dependency>
  34. <groupId>org.mybatis.spring.boot</groupId>
  35. <artifactId>mybatis-spring-boot-starter</artifactId>
  36. <version>1.3.2</version>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. </plugin>
  45. </plugins>
  46. </build>

3.创建测试数据表

创建测试数据库:springbootdemo,并添加以下数据表:

  1. CREATE TABLE `dictionary` (
  2. `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  3. `dict_key` varchar(50) NOT NULL DEFAULT '' COMMENT '字典key',
  4. `dict_value` varchar(50) NOT NULL DEFAULT '' COMMENT 'value',
  5. `parent_id` int(11) NOT NULL COMMENT '上级节点id',
  6. `description` varchar(100) NOT NULL DEFAULT '' COMMENT '描述信息',
  7. PRIMARY KEY (`id`),
  8. KEY `Index_dictKey_parentId` (`dict_key`,`parent_id`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4 COMMENT='数据字典表';

4.添加数据库相关配置

数据库配置使用多环境配置,具体多环境配置方法参考:http://www.cnblogs.com/vitasyuan/p/8782612.html

在application-dev.properties配置文件中添加数据库相关配置:

  1. #数据库配置
  2. spring.datasource.url=jdbc:mysql://localhost:3306/springbootdemo
  3. spring.datasource.username=root
  4. spring.datasource.password=123456
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

在application.properties文件中添加使用dev环境配置文件的内容:

  1. #配置使用的配置环境,值为application-{profile}.properties中的profile值
  2. spring.profiles.active=rc
  3. #mapper文件的路径
  4. mybatis.mapper-locations=classpath:mapper/**/*.xml

5.添加mapper文件和接口

在resource文件夹下添加mapper/demo-server文件夹,并添加dictionary.xml配置文件,配置文件内容如下:

  1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  2. <mapper namespace="com.example.demo.business.dictionary.dao.DictionaryDao">
  3. <resultMap id="DictionaryResultMap" type="com.example.demo.business.dictionary.Dictionary">
  4. <result property="id" column="id"></result>
  5. <result property="dictKey" column="dict_key"></result>
  6. <result property="dictValue" column="dict_value"></result>
  7. <result property="parentId" column="parent_id"></result>
  8. <result property="description" column="description"></result>
  9. </resultMap>
  10. <select id="list" resultMap="DictionaryResultMap">
  11. SELECT * FROM `dictionary`
  12. </select>
  13. <select id="listChildrenByKey" resultMap="DictionaryResultMap">
  14. SELECT * FROM dictionary where parent_id= (select id from dictionary where dict_key= #{key})
  15. </select>
  16. <delete id="delete" parameterType="int">
  17. delete from dictionary where id = #{id}
  18. </delete>
  19. <insert id="insert" parameterType="com.example.demo.business.dictionary.Dictionary">
  20. INSERT INTO `dictionary`(`dict_key`,`dict_value`,`parent_id`,`description`)
  21. VALUES(#{dictKey}, #{dictValue}, #{parentId}, #{description})
  22. </insert>
  23. </mapper>

6.添加controller测试数据库连接

创建controller类,代码如下:

  1. @RestController
  2. @RequestMapping(value = "/dictionary")
  3. public class DictionaryController {
  4. @Autowired
  5. private DictionaryDao dictionaryDao;
  6. @GetMapping
  7. public Response<List<Dictionary>> get(){
  8. Response<List<Dictionary>> response = new Response<>();
  9. response.setData(dictionaryDao.list());
  10. return response;
  11. }
  12. }

启动服务,在浏览器中输入访问url:

  1. http://localhost:8080/demo/dictionary

返回以下数据:

  1. {
  2. "code": 200,
  3. "message": "Success",
  4. "data": [
  5. {
  6. "id": 27,
  7. "dictKey": "test",
  8. "dictValue": "testvalue",
  9. "parentId": 1,
  10. "description": "test"
  11. },
  12. {
  13. "id": 30,
  14. "dictKey": "test",
  15. "dictValue": "testvalue",
  16. "parentId": 1,
  17. "description": "test"
  18. },
  19. {
  20. "id": 32,
  21. "dictKey": "test",
  22. "dictValue": "testvalue",
  23. "parentId": 1,
  24. "description": "test"
  25. },
  26. {
  27. "id": 33,
  28. "dictKey": "test",
  29. "dictValue": "testvalue",
  30. "parentId": 1,
  31. "description": "test"
  32. }
  33. ]
  34. }

表示数据库配置成功。

SpringBoot集成Mybatis的更多相关文章

  1. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  2. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  3. SpringBoot 集成Mybatis 连接Mysql数据库

    记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...

  4. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  5. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  6. Springboot集成mybatis(mysql),mail,mongodb,cassandra,scheduler,redis,kafka,shiro,websocket

    https://blog.csdn.net/a123demi/article/details/78234023  : Springboot集成mybatis(mysql),mail,mongodb,c ...

  7. BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析

    重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...

  8. SpringBoot集成Mybatis配置动态数据源

    很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...

  9. SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)

     下面将在用户和账户进行一对一查询的基础上进行介绍SpringBoot集成Mybatis实现多表查询的基于xml的两种方式.   首先我们先创建两个数据库表,分别是user用户表和account账户表 ...

  10. SpringBoot集成MyBatis的Bean配置方式

    SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...

随机推荐

  1. 关于Android SDK Manager更新速度慢的解决方法

    因为我的C盘比较小,android sdk安装在c盘那么他下载的东西也会默认在c盘.所以我选择安装在其他的盘.而且我发现android sdk manager可以开多个窗口,这样的话如果每个窗口都很慢 ...

  2. centos6上yum安装drbd(内核:2.6.32.696)

    author:headsen  chen date: 2017-11-20  15:11:21 notice: 个人原创,转载请注明,否则依法追究法律责任 前期准备: 两台机器:配置主机名分别为: l ...

  3. SQL 存储过程 多条件 分页查询 性能优化

    最优化查询代码 -- 注意:此处可能会出现 字符串过长问题,所以 必要的情况下请分段处理 set @sql1 =' SELECT * FROM ( select ROW_NUMBER() OVER(O ...

  4. java swing 下拉框与文本框

    import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; publi ...

  5. vue+webpack+element-ui+git

    webpack.config.jsconst { resolve } = require('path') const webpack = require('webpack') const HtmlWe ...

  6. C#设置和获取系统环境变量

    C#设置和获取环境变量 1.前言 本来想拿学校机房的Android编辑器直接粘到自己电脑上用,发现它的eclipse是 32位的,而我的JDK是64位的,于是想到干脆装两个JDK,用C#做一个能够更改 ...

  7. Android学习笔记1——开发环境配置

    一.JDK配置 Android是基于Java进行开发的,首先需要在电脑上配置JDK(Java Development Kit).在http://www.androiddevtools.cn/下载对应系 ...

  8. Jenkins集成Docker镜像实现自动发布

    1. 思路&流程 Jenkins集成Docker镜像实现自动发布与Jenkins发布mavne项目思路一样总体流程 为:Jenkins 拉去远端源码 -- gitl实现应用打包 -- jenk ...

  9. 数据库 --> MySQL存储引擎介绍

    MySQL存储引擎介绍 MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然 ...

  10. KVM之七:KVM克隆

    1.在克隆虚拟机之前,必须先暂停或者停掉kvm 虚拟机.以虚拟机 snale 为例,先暂停虚拟机,如下 [root@kvm ~ ::]#virsh list Id 名称 状态 ------------ ...