SpringBoot集成Mybatis
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的更多相关文章
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- 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 ...
- BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析
重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...
- SpringBoot集成Mybatis配置动态数据源
很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...
- SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)
下面将在用户和账户进行一对一查询的基础上进行介绍SpringBoot集成Mybatis实现多表查询的基于xml的两种方式. 首先我们先创建两个数据库表,分别是user用户表和account账户表 ...
- SpringBoot集成MyBatis的Bean配置方式
SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...
随机推荐
- AI行业需要什么样的人才
自AI人工智能诞生以来,它的领域逐步扩大,技术层面也越来越多样化,投身于该领域的人才也越来越多,那么AI行业到底需要什么样的人才?我们应该如何定位自己,找到适合的领域?3月8日晚,在飞马网线上直播中, ...
- ELK重难点总结和整体优化配置
本文收录在Linux运维企业架构实战系列 做了几周的测试,踩了无数的坑,总结一下,全是干货,给大家分享~ 一.elk 实用知识点总结 1.编码转换问题(主要就是中文乱码) (1)input 中的cod ...
- shell 文本操作命令
vi 编辑器中有三种状态模式 [vi 文件名(或路径+文件名)] 1.命令模式 2.输入模式 3.末行模式 三种模式间的相互转换 vi编辑器的启动与退出 直接进入编辑环境 $ vi 进入编辑环境并打 ...
- 基于hi-nginx的web开发(python篇)——起步
hi-nginx不仅让python web 应用跑得飞快,而且让相关开发变得简单敏捷. 关于hi-nginx的安装,请参考:https://www.cnblogs.com/hi-nginx/p/862 ...
- 【HTML】 HTML基础知识 一些标签
额..是这样的,去年为了学习web框架,自己开发了一个zbx配置管理的二次开发系统,当时从零开始接触web开发,也第一次接触到了前端的一些知识.其中最基础的就是html/css了.我把那部分笔记整理上 ...
- Spring AOP 的proxy详解
spring 提供了多种不同的方案实现对 bean 的 aop proxy, 包括 ProxyFactoryBean, 便利的 TransactionProxyFactoryBean 以及 AutoP ...
- 04_Python的数据类型1数值和字符串_Python编程之路
上一节我们通过一个helloworld程序学习python的一些简单操作,还有输入与输出 这节我们来讲Python的数据类型与变量的操作 Python的交互器 在讲这个之前,我要先讲一下python的 ...
- hibernate框架学习笔记5:缓存
缓存不止存在与程序中,电脑硬件乃至于生活中都存在缓存 目的:提高效率 比如IO流读写字节,如果没有缓存,读一字节写一字节,效率低下 hibernate中的一级缓存:提高操作数据库的效率 示例: 抽取的 ...
- 在CentOS7.1上安装Gitlab碰到的问题及解决方法
一 前言 关于在CentOS7上安装Gitlab, 官方文档已经很详细了,步骤大家按照官方的安装文档一步一步安装即可, 这里就不在累述.官方安装文档地址: https://about.gitlab. ...
- DEVC使用问题集锦
一.DEVC++编译出现"Id return 1 exit status" 这是初学者刚用DEVC经常碰到问题,一般有如下解决方法: 1.首先检查下是否有c的exe程序开着,若开着 ...