SpringBoot 集成Mybatis

文章目录

  • 一、创建SpringBoot项目
  • 二、添加Mybatis相关依赖
  • 三、数据源配置
  • 四、创建事务的模型实体类
  • 五、创建和数据库交互联系的映射关系类
  • 六、创建业务接口和实现类
  • 七、创建控制器类
  • 八、请求验证

一、创建SpringBoot项目

如何创建详见:IDEA 创建 SpringBoot 项目


二、添加Mybatis相关依赖

以前开发Web项目我们都知道要想把数据添加到数据库,不仅必须要数据库的驱动程序,还要有各种各样的配置文件,像java Bean配置,数据源配置,对象和数据库字段的映射配置等等。使用SpringBoot开发,我们只需要加入依赖文件就可以了,SpringBoot已经都帮我配置好了。配置如下图所示:

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.1</version>
</dependency>

三、数据源配置

在application.properties中配置数据库连接的相关信息:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:18103/db_test?characterEncoding=GBK
spring.datasource.username=root
spring.datasource.password=root

四、创建事务的模型实体类

编程是利用面向对象的思想把自然界中的事物抽象成模型,利用模型来解决实际中的问题。如下图:

package com.springboottest.bean;public class StudentBean {private int id;private String name;public StudentBean() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

注:这里的字段名称与数据库表字段名称一致。


五、创建和数据库交互联系的映射关系类

这个类主要是和数据进行交互联系的,需要配置好实体类和数据库字段的映射关系。由于SpringBoot已经做了大量的工作,我们只需要做好相关注解就可以使用了。如下图所示:

package com.springboottest.sql.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;@Mapper
public interface StudentMapper {@Select("select * from tb_student where name=#{name}")StudentBean getStudentInfoByName(String name);
}

@Mapper 表明该类是一个Mapper接口,使用@Select@Insert等注解我们可以直接在类中书写sql语句来实现我们的目的。


六、创建业务接口和实现类

我们在接口类里定义要实现的业务功能接口,在它的实现类里实现接口。接口类如下图:

package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;public interface StudentService {StudentBean getStudentInfoByName(String name);
}

实现类如下图:

package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class StudentServiceImpl implements StudentService{@Autowiredprivate StudentMapper studentMapper;@Override@Transactionalpublic StudentBean getStudentInfoByName(String name) {return studentMapper.getStudentInfoByName(name);}
}

@Service注解表明它是一个服务类Bean,可以被SpringBoot识别使用,相当于以前在xml里配置的bean。


七、创建控制器类

Web项目的请求经过映射找到控制器类里对应的方法,然后再实现完业务返回响应信息。如下图:

package com.springboottest.controller;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.MySQLProcessor;
import com.springboottest.sql.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/mysql")
public class SqlController {@Autowiredprivate StudentService studentService;@RequestMapping(value = "/student")public String studentSelect(@RequestParam String name){StudentBean bean = studentService.getStudentInfoByName(name);if(bean != null){return "Name = " + bean.getName();} else {return "null";}}
}

八、请求验证

请求地址:http://localhost:8991/mysql/student?name=tom
在这里插入图片描述



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++primer 第 3 章 字符串、向量和数组 3.1 命名空间的using声明 3.2标准库类型string

引言 除了第2章介绍的内置类型之外,C语言还定义了 -个内容丰富的抽象数据类型库。其中,string和 vector是两种最重耍的标准库类型&#xff0c;前者支持可变长字符串&#xff0c;后者则 表示可变长的集合。还有…种标准库类型是迭代器&#xff0c;它是string和vector的配套类型…

ClickHouse 四舍五入函数

文章目录一、round(x[,N])二、floor(x[,N])三、ceil(x[,N]),ceiling(x[,N])四、trunc(x[, N]), truncate(x[, N])一、round(x[,N]) 说明&#xff1a;将值取整到指定的小数位数&#xff0c;该函数按顺序返回最近的数字。 语法&#xff1a; round(expression [, decimal_place…

codeforces 59A-C语言解题报告

59A题目网址 题目解析 1.输入字符串,如果大写字母最多,则全部输出为大写;如果小写字母多或大小写字母一样多,则全部输出为小写 举例: 输入: maTRIx 输出: matrix 2.使用a,b两个变量去记录大小写字母的数量 代码 #include<stdio.h> #include<stdlib.h> #includ…

C++primer 第 3 章 字符串、向量和数组 3 . 3 标准库类型vector

标准库类型vector表示对象的集合&#xff0c;其中所有对象的类型都相同。集合中的每个对象都有一个与之对应的索引&#xff0c;索引用于访问对象。因为vector"容纳着”其他对象&#xff0c;所以它也常被称作容器(container).第 II部将对容器进行更为详细的介绍。 要想使用…

SpringBoot AOP切面实现

文章目录一、AOP简介二、AOP体系与概念三、AOP实例1、创建SpringBoot工程2、添加依赖3、AOP相关注解3.1、Aspect3.2、Pointcut3.2.1、execution()3.2.2、annotation()3.3、Around3.4、Before3.5、After3.6、AfterReturning3.7、AfterThrowing一、AOP简介 AOP&#xff08;Aspec…

英语口语-文章朗读Week8 Friday

文章 It is a phenomenon that people are losing trust in each other in today’s society. Some people become selfish,and for interest, they are likely to betray their colleagues,friends, and even their relatives. They tend to cater to those who can benefit …

C++primer 第 3 章 字符串、向量和数组 3 . 4 迭代器介绍

3.4迭代器介绍 我们已经知道可以使用下标运算符来访问string对象的字符或vector对象的元素&#xff0c;还有另外一种更通用的机制也可以实现同样的目的&#xff0c;这就是迭代器&#xff08;iterator&#xff09;。在第II部分中将要介绍&#xff0c;除了vector之外&#xff0c…

ClickHouse 函数

文章目录一、日期函数1、时间或日期截取函数&#xff08;返回非日期&#xff09;2、时间或日期截取函数&#xff08;返回日期&#xff09;3、日期或时间日期生成函数二、类型转化类函数1、精度保留&#xff08;非四舍五入&#xff09;2、字符串转化为整数&#xff08;非整数的字…

英语口语-文章朗读Week9 TuesDay

朗读文章 People living in ancient times had no alternative but to do housework manually. They fire the wood when they cook,they hand wash clothes with hands; they sweep the floor with brooms. Now, modern inventions come as a great relief to people. We co…

SpringBoot @Value注解

目录一、非配置文件注入1、注入普通字符串2、注入JAVA系统变量3、注入表达式4、注入其他Bean属性5、注入文件资源6、注入URL资源二、通过配置文件注入1、注入普通字符串2、注入基本类型3、注入数组类型4、注入List类型5、注入Map类型一、非配置文件注入 1、注入普通字符串 直…

C++primer 第 3 章 字符串、向量和数组 3 . 5 数组

3.5数组 数组是一种类似于标准库类型vector&#xff08;参见3.3节&#xff0c;第86页&#xff09;的数据结构&#xff0c;但是在性能和灵活性的权衡上又与vector有所不同。与vector相似的地方是&#xff0c;数组也是存放类型相同的对象的容器&#xff0c;这些对象本身没有名字…

codeforces 122A-C语言解题报告

122A题目网址 题目解析 1.输入数字(在1000以内),若能被4,7幸运数整除或只含4,7则输出YES,否则输出NO 举例: 输入: 107 输出: NO 2.解题关键: 1)使用列举法,把所有符合的幸运数列出来(int number[]) 1—2 2–224 3–22*28 24814个 2)若n是幸运数中的一个或n%幸运数0,则为YES…

SpringBoot @Value给静态变量注入值

文章目录一、简介二、Value给静态变量注入值方案一&#xff1a;set()方法设置方案二&#xff1a;PostConstruct注解修饰的方法中进行赋值三、总结一、简介 SpringBoot 中给普通变量注入值只需在变量上添加 Value 注解即可。 application.properties 配置文件有如下配置&#…

C++primer 第 4 章 表达式 4.1基础 4 . 2 算术运算符 4 .3 逻辑和关系运算符 4 . 4 赋值运算符 4 .5 递增和递减运算符 4.6成员访问运算符

表达式由一个或多个运算对象(operand)组成&#xff0c;对表达式求值将得到一个结果(result)字面值和变量是最简单的表达式(expression),其结果就是字面值和变量的值。把一个运算符(operator)和一个或多个运算对象组合起来可以生成较复杂的表达式 4.1基础 有几个基础概念对表达…

codeforces 266B-C语言解题报告

266B题目网址 题目解析 输入n,t,排队情况s,输出第t次循环后,排队情况 举例: 输入: 5 1 BGGBG 输出: GBGGB 2.输入的n代表排队的人数,t代表整个循环t次之后再输出结果 3.注意点: 使用while()大循环去控制t次的循环,使用for()内层循环去遍历整个字符串 如果if(s[j]‘B’&…

Nginx Location配置详解

目录一、语法二、匹配顺序三、root 与 alias 的区别四、server 和 location 中的 root一、语法 Location 是 Nginx 中一个非常核心的配置&#xff0c;关于Location&#xff0c;举个简单的配置例子&#xff1a; server {listen 80;server_name 10.0.7.115;location / {root /d…

英语口语-文章朗读Week9 Wednesday

英语文章 Birds of the same species flock together&#xff0c; People tend to look for someone like themselves to be friends. But having the same interests is not the only standard when we are seeking friends. In most cases, especially for adults, people l…

C++primer 第 4 章 表达式 4.7条件运算符 4.8位运算符 4.9 sizeof运算符 4.10逗号运算符 4.11类型转换 4 . 1 2 运算符优先级表

4.7条件运算符 条件运算符(?&#xff1a;)允许我们把简单的if else逻辑嵌入到单个表达式当中&#xff0c;条件运算符按照如下形式使用&#xff1a;cond ? expr1 : expr2;其中cond是判断条件的表达式&#xff0c;而expr1和expr2是两个类型相同或可能转换为某个公共类型的表达…

Git 之 git tag标签使用

目录一、简介二、本地tag操作1、创建tag标签&#xff08;1&#xff09;创建轻量标签&#xff08;2&#xff09;创建附注标签2、查看tag标签&#xff08;1&#xff09;查看标签列表&#xff08;2&#xff09;查看标签提交信息&#xff08;3&#xff09;在提交历史中查看标签3、删…

codeforces 110A-C语言解题报告

110A题目网址 题目解析 1.输入一个数字,如果数字中包含的4,7的数量是4或7的倍数,则输出YES,否则输出NO 举例: 输入: 40047 输出: NO 2.注意点: 1)由于数字很长,所以使用long long int类型,使用scanf("%lld",&n)接收输入 2)整型转字符串,使用sprintf(字符串,“…