博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot--整合MyBatis(XML方式)
阅读量:2442 次
发布时间:2019-05-10

本文共 3462 字,大约阅读时间需要 11 分钟。

整合MyBatis(XML方式)

1. 添加MySQL 连接驱动依赖、SpringBoot Mybatis 依赖

完整pom.xml文件如下:

4.0.0
com.example
demo
1.0.0
jar
hello
Demo project for Spring Boot
1.3.0
5.1.39
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
${mysql-connector}
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot}
org.projectlombok
lombok
ch.qos.logback
logback-classic
org.slf4j
jcl-over-slf4j

整合MyBatis的核心依赖MyBatis-Spring-Boot-Starter提供:

  • 自动检测现有的DataSource
  • 将创建并注册SqlSessionFactory的实例,该实例使用SqlSessionFactoryBean将该DataSource作为输入进行传递
  • 将创建并注册从SqlSessionFactory中获取的SqlSessionTemplate的实例。
  • 自动扫描您的mappers,将它们链接到SqlSessionTemplate并将其注册到Spring上下文,以便将它们注入到您的bean中。

因此,引入该Starter之后,只需要定义一个DataSource即可(application.properties中可配置),它会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,并注册到Spring上下文中。

配置application.yml文件

  • 配置数据库连接信息(数据源)
spring:    #数据源  datasource:    url: jdbc:mysql://localhost:3306/springbootdb??useUnicode=true&characterEncoding=UTF-8    username: root    password: 123    driver-class-name: com.mysql.jdbc.Driver
  • 配置mybatis
#mybatis配置mybatis:  typeAliasesPackage: com.example.mybaitsxml.dao.entity  mapperLocations: classpath:mapper/*.xml  #configLocation: classpath:/mybatis-config.xml

注:通常,若mybatis配置信息较少,只是针对基本配置无需复杂配置,则只需在application.yml文件中配置即可,否则最好配置在 mybatis-config.xml中。

代码实现

工程结构:

在这里插入图片描述

  • controller层
@Slf4j@RestController@RequestMapping("/web")public class UserController {
@Autowired private UserService userService; @GetMapping("/queryAllUsers") public List
queryAllUsers(){
return userService.queryAllUsers(); }}
  • service层(实现)
@Slf4j@Servicepublic class UserServiceImpl implements UserService {
@Autowired private UserMapper userMapper; @Override public List
queryAllUsers() {
log.info("/queryAllUsers start..."); return userMapper.queryAllUsers(); }}
  • dao层

dao层分为数据库实体类(entity)和数据库操作mapper接口(mapper)

entity:

@Datapublic class User {
private String name ; private String sex; private Integer age; private Integer classNo;}

mapper:

public interface UserMapper {
List
queryAllUsers();}
  • mapper.xml实现

测试:

在这里插入图片描述

转载地址:http://wzpqb.baihongyu.com/

你可能感兴趣的文章
富文本中添加字体选项功能_如何将开发人员选项卡添加到Microsoft Office功能区...
查看>>
如何将音乐添加到PowerPoint演示文稿
查看>>
mozilla.pdf_Mozilla说它没有从Booking.com赚钱
查看>>
fitbit手表中文说明书_Fitbit OS达到3.0版,这是新功能
查看>>
ublock origin_Chrome可能会在打破uBlock起源的同时更快地阻止广告
查看>>
电邮地址_我如何找出电子邮件的真正来源?
查看>>
windows虚拟桌面_在Windows中使用虚拟桌面的最佳免费程序
查看>>
ipad iphone开发_如何在iPhone或iPad上的消息中快速选择表情符号
查看>>
在windows使用gpu_如何选择Windows 10上游戏使用的GPU
查看>>
minecraft启动器_如何使用外部编辑器编辑Minecraft地图
查看>>
什么是适用于iPhone和iPad的iOS最新版本?
查看>>
成为产品不一定是坏事
查看>>
Ubuntu 18.04 LTS现在在Microsoft Store中
查看>>
如何检查已安装的Ubuntu版本
查看>>
如何在Windows 10上禁用附近共享
查看>>
gmail_Gmail将提供自毁电子邮件
查看>>
google 禁止广告_是否应禁止针对个人的广告?
查看>>
Plover.io在本地设备之间快速共享文件
查看>>
如何在OS X照片中禁用iCloud照片同步
查看>>
Minecraft的官方网站分发了受恶意软件感染的皮肤
查看>>