一、创建一个SpringBoot项目

 
 
 
 
 
 
 
 

二、引入相关依赖

        <!--web核心依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

三、创建如下结构文件

 
 
  • 编写实体类com.zhg.demo.mybatis.entity.User
public class User implements Serializable {
private Long id;//编号
private String username;//用户名
private String password;//密码
//省略get set方法
}
  • 编写接口com.zhg.demo.mybatis.mapper.UserMapper

    注意:需要使用@Mapper注解,不然SpringBoot无法扫描
@Mapper//指定这是一个操作数据库的mapper
public interface UserMapper {
List<User> findAll();
}
  • 编写在resources文件中创建 mapper/UserMapper.xml文件

    注意

    1.namespace中需要与使用@Mapper的接口对应

    2.UserMapper.xml文件名称必须与使用@Mapper的接口一致

    3.标签中的id必须与@Mapper的接口中的方法名一致,且参数一致
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhg.demo.mybatis.mapper.UserMapper">
<select id="findAll" resultType="User">
SELECT * FROM tb_user
</select>
</mapper>

-----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="findAll" resultType="com.example.demo.entity.User">
SELECT * FROM tb_user
</select>
</mapper>
 
  • 编写接口com.zhg.demo.mybatis.service
public interface UserService {
List<User> findAll();
}
  • 编写实现类com.zhg.demo.mybatis.service.impl.UserServiceimpl

    注意:需要在接口实现类中使用@Service注解,才能被SpringBoot扫描,在Controller中使用@Authwired注入
@Service("userService")
public class UserServiceimpl implements UserService { @Autowired
private UserMapper userMapper; @Override
public List<User> findAll() {
return userMapper.findAll();
}
}
  • 编写api接口com.zhg.demo.mybatis.controller.UserController
@RestController
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; @RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
} }
  • 在启动类中添加对@MapperScan的扫描
@SpringBootApplication
@MapperScan("com.zhg.demo.mybatis.mapper")//使用MapperScan批量扫描所有的Mapper接口;
public class MybatisApplication { public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
} }

四、配置文件

注意

1.mybatis中的mapper-locations是mapper的xml文件位置

2.mybatis中的type-aliases-package是为了配置xml文件中resultType返回值的包位置,如果未配置请使用全包名如下:

<select id="findAll" resultType="com.zhg.demo.mybatis.entity.User">
SELECT * FROM tb_user
</select>
  • 在resources中创建application.yml文件,并编写配置
server:
port: 8081
spring:
#数据库连接配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://47.107.105.158:3306/test?characterEncoding=utf-8&useSSL=false
username: root
password: 123456 #mybatis的相关配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.zhg.demo.mybatis.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true

--------------------------------------------------------------------------
server:
port: 8081
spring:
#数据库连接配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false
username: root
password: root

#mybatis的相关配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
 

五、创建数据库和数据表

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'laowang', '112233');
INSERT INTO `tb_user` VALUES ('2', 'laoli', '123456');

六、启动并测试

作者:蜻蜓队长家长
链接:https://www.jianshu.com/p/541874714907
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

SpringBoot整合mybatis快速入门的更多相关文章

  1. SpringBoot整合ActiveMQ快速入门

    Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...

  2. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  3. SpringBoot+SpringMVC+MyBatis快速整合搭建

    作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...

  4. springboot 整合 mybatis 入门

    springboot整合mybatis 0.yml 配置文件 1.创建数据库表. 2.创建实体类. 3.创建 Mapper 接口 ,添加 @Mapper 注解. 4.创建 Mapper 映射文件. & ...

  5. 001 SringBoot基础知识及SpringBoot整合Mybatis

    1.原有Spring优缺点分析 (1)优点 Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品.无需开发重量级的Enterprise J ...

  6. Springboot 完整搭建快速入门,必看!

    前言 手把手教你Springboot微服务项目搭建快速入门,通过本文学习Springboot的搭建快速入门,掌握微服务大致的配置服务,后续将会继续将核心组件引入到项目中,欢迎关注,点赞,转发. Spr ...

  7. SpringBoot数据访问(一) SpringBoot整合Mybatis

    前言 SpringData是Spring提供的一个用于简化数据库访问.支持云服务的开源框架.它是一个伞形项目,包含了大量关系型数据库及非关系型数据库的数据访问解决方案,其设计目的是为了使我们可以快速且 ...

  8. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  9. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

随机推荐

  1. 小哈学Python ----XML

    XML XML是实现不同语言或程序之间进行数据交换的协议,XML文件格式如下: <data> <country name="Liechtenstein"> ...

  2. java+selenium UI自动化001

    selenium是一个用于Web应用程序测试的工具,可以用来模拟用户在浏览器上的操作. 支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Googl ...

  3. python 列表指导式

    >>> a=[page for page in range(10)]>>> print (a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>& ...

  4. box-shadow详解

    今天课堂上有学生问到box-shadow这个属性,那么下面我们就来详细的解说下这个属性它的用法,box-shadow是css3中的一个属性,它可以向框添加一个或多个阴影. 首先我们来看它的语法: bo ...

  5. MySQL Orchestrator自动导换+VIP切换

    目录 Orchestrator总体结构...  测试环境信息...  Orchestrator详细配置...  SSH免密配置...  /etc/hosts配置...  visudo配置...  /e ...

  6. sync/fsync/fdatasync的简单比较

    此文主要转载自 http://blog.csdn.net/zbszhangbosen/article/details/7956558 官网上有关于MySQL的flush method的设置参数说明,但 ...

  7. 【贪心】数列分段Section I luogu-1181

    题目描述 对于给定的一个长度为\(N\)的正整数数列\(A_i\),现要将其分成连续的若干段,并且每段和不超过\(M\)(可以等于\(M\)),问最少能将其分成多少段使得满足要求. 分析 简单思考一下 ...

  8. 将py文件打包成exe文件

    PyInstaller工具是跨平台的,它既可以在 Windows平台上使用,也可以在 Mac OS X 平台上运行.在不同的平台上使用 PyInstaller 工具的方法是一样的,它们支持的选项也是一 ...

  9. Ubuntu 19.10安装Wine软件

    ======================================== 我使用的操作系统版本为Ubuntu 19.10 64位,如果是32位Ubuntu19.10则可以跳过步骤一 1.添加 ...

  10. 微信小程序 -- scroll view

    效果图:横向滚动和纵向滚动 scroll view使用方法文档,前面已经介绍查找文档方法,此处不再赘述 一.横向滚动 创建一个页面scroll-nav 然后,在.wxml文件中排版 <!--水平 ...