1.创建SpringBoot工程

根据

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

说明创建SpringBoot项目。

2.添加相关依赖

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

  <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

3.创建测试数据表

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

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

4.添加数据库相关配置

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

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

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

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

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

5.添加mapper文件和接口

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

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.business.dictionary.dao.DictionaryDao"> <resultMap id="DictionaryResultMap" type="com.example.demo.business.dictionary.Dictionary">
<result property="id" column="id"></result>
<result property="dictKey" column="dict_key"></result>
<result property="dictValue" column="dict_value"></result>
<result property="parentId" column="parent_id"></result>
<result property="description" column="description"></result>
</resultMap> <select id="list" resultMap="DictionaryResultMap">
SELECT * FROM `dictionary`
</select> <select id="listChildrenByKey" resultMap="DictionaryResultMap">
SELECT * FROM dictionary where parent_id= (select id from dictionary where dict_key= #{key})
</select> <delete id="delete" parameterType="int">
delete from dictionary where id = #{id}
</delete> <insert id="insert" parameterType="com.example.demo.business.dictionary.Dictionary">
INSERT INTO `dictionary`(`dict_key`,`dict_value`,`parent_id`,`description`)
VALUES(#{dictKey}, #{dictValue}, #{parentId}, #{description})
</insert>
</mapper>

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

创建controller类,代码如下:

@RestController
@RequestMapping(value = "/dictionary")
public class DictionaryController { @Autowired
private DictionaryDao dictionaryDao; @GetMapping
public Response<List<Dictionary>> get(){
Response<List<Dictionary>> response = new Response<>();
response.setData(dictionaryDao.list());
return response;
}
}

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

http://localhost:8080/demo/dictionary

返回以下数据:

{
"code": 200,
"message": "Success",
"data": [
{
"id": 27,
"dictKey": "test",
"dictValue": "testvalue",
"parentId": 1,
"description": "test"
},
{
"id": 30,
"dictKey": "test",
"dictValue": "testvalue",
"parentId": 1,
"description": "test"
},
{
"id": 32,
"dictKey": "test",
"dictValue": "testvalue",
"parentId": 1,
"description": "test"
},
{
"id": 33,
"dictKey": "test",
"dictValue": "testvalue",
"parentId": 1,
"description": "test"
}
]
}

表示数据库配置成功。

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. php类中双冒号和->的区别

    就是为了区分对象的方法和属性,和是访问类的静态方法和静态变量,类的静态方法和静态变量是类公用的,不需要实例化也能访问,而对象的方法和属性是每个对象特有的,因此必须先实例化.其他语言如C++,JAVA等 ...

  2. Shiro入门这篇就够了【Shiro的基础知识、回顾URL拦截】

    前言 本文主要讲解的知识点有以下: 权限管理的基础知识 模型 粗粒度和细粒度的概念 回顾URL拦截的实现 Shiro的介绍与简单入门 一.Shiro基础知识 在学习Shiro这个框架之前,首先我们要先 ...

  3. Mysql自连接的一些用法

    自连接是连接的一种用法,但并不是连接的一种类型,因为他的本质是把一张表当成两张表来使用. mysql有时在信息查询时需要进行对自身连接(自连接),所以我们需要为表定义别名. 我们举例说明,下面是商品采 ...

  4. NGUI_Depth

    四.深度(Depth)概念; 1. (1).每一个UIPanel和每一个UI控件都一定会有一个Depth,深度值大代表显示的优先级高(会趋向于在界面更上层显示) (2).Depth决定的是UI的显示层 ...

  5. 如何正确使用Java异常处理机制

    文章来源:leaforbook - 如何正确使用Java异常处理机制作者:士别三日 第一节 异常处理概述 第二节 Java异常处理类 2.1 Throwable 2.1.1 Throwable有五种构 ...

  6. Oracle查询优化改写--------------------范围处理

    一.定位连续值的范围 二.查找同一组或分区中行之间的差

  7. [bzoj1565][NOI2009]植物大战僵尸_网络流_拓扑排序

    植物大战僵尸 bzoj1565 题目大意:给你一张网格图,上面种着一些植物.你从网格的最右侧开始进攻.每个植物可以对僵尸提供能量或者消耗僵尸的能量.每个植物可以保护一个特定网格内的植物,如果一个植物被 ...

  8. SQLite3创建数据库的方法

    上次刚接触SqlLite,不知道怎么创建数据库,现在做下总结: 界面和MYSQL一样,都是CMD界面,但不是在SQLite.exe中创建数据库: 首先还是说一下cmd下sqlite的使用网上已经很多了 ...

  9. drbd(一):简介和安装

    本文目录:1.drbd简介2.drbd工作原理和术语说明 2.1 drbd工作原理 2.2 drbd复制协议模型 2.3 drbd设备的概念 2.4 drbd资源角色 2.5 drbd工作模式 2.6 ...

  10. shell命令总结一

    简述:这篇总结是在360企业安全实习第一周学到的. Linux中的 2>&1 .if文件命令 .tr .$0等相关参数含义的用法 1. 2>&1 command>/d ...