前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >04 Spring Boot 整合MyBatis

04 Spring Boot 整合MyBatis

作者头像
shimeath
发布2020-07-31 15:48:49
3020
发布2020-07-31 15:48:49
举报

整合MyBatis

注意点

  1. 在相应Mapper接口上通过@Mapper注解进行注入;或在程序入口添加@MapperScan(com.hxh.Mapper),这其中的所有接口都会被扫描
  2. XXXXMapper.xml存放在resources/MyBatis/mapper目录下

整合方法

  1. 配置整合依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
  2. 修改yaml配置文件,进行数据库连接配置 spring: datasource: username: root password: 123456 #?serverTimezone=UTC解决时区的报错 url: jdbc:mysql://localhost:3306/db_test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource mybatis: type-aliases-package: com.hxh.pojo
  3. 创建mapper目录以及相应Mapper接口 //@Mapper : 表示本类是一个 MyBatis 的 Mapper @Mapper @Repository public interface DepartmentMapper { // 获取所有部门信息 List<Department> getDepartments(); // 通过id获得部门 Department getDepartment(Integer id); }
  4. 建立对应Mapper映射文件(与接口同名) <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hxh.springboot05mybatis.mapper.BlogTypeMapper"> <select id="blogTypeList" resultType="BlogType"> Select * from t_blogtype </select> <insert id="addBlogType"> insert into t_blogtype values(null, '测试',6); </insert> <update id="updateBlogType" parameterType="BlogType"> update t_blogtype set typeName=#{typeName},orderNo=#{orderNo} where id=#{id} </update> <delete id="delBlogType" parameterType="Integer"> delete from t_blogtype where id=#{id} </delete> </mapper>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 整合MyBatis
    • 注意点
      • 整合方法
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档