Spring boot2 数据访问之Druid数据源+Mybatis

2023-10-16 19:17:00 浏览数 (2)

Mybatis官方地址

1、查找Mybatis 相关starter 官方文档

这里注意查找指定版本的Starter

 这里以2.2.2为例,查看官方的pom.xml如下:

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<!--
       Copyright 2015-2022 the original author or authors.
       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot</artifactId>
    <version>2.2.2</version>
  </parent>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <name>mybatis-spring-boot-starter</name>
  <properties>
    <module.name>org.mybatis.spring.boot.starter</module.name>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
    </dependency>
  </dependencies>
</project>

提取其中的starter相关信息,如下:

代码语言:javascript复制
       <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

添加至目标项目的pom.xml,reload maven,添加了如下依赖项

 查看自动配置类如下:

 i、SqlSessionFactory和SqlSessionFactoryBean类必须存在

ii、有且只有一个DataSource

vi、通过配置项前缀为mybatis和MybatisProperties实例绑定

vii、在DataSourceAutoConfiguration和MybatisLanguageDriverAutoConfiguration之后配置

2、相关依赖项

(1)、SqlSessionFactory 根据容器内部的数据源实例创建Sql会话工厂实例组件,并配置相关数据,并写入容器中

(2)、SqlSessionTemplate 

 SqlSessionTemplate 适配了SqlSession

 真正执行sql得也是SqlSession

(3)、AutoConfiguredMapperScannerRegistrar Mapper自动注入

 扫描所有的@Mapper注解,并将相关内容写入容器中.

3、实战 官方文档

(1)、修改application.yml 配置mybatis相关

代码语言:javascript复制
#mybatis配置
mybatis:
  #mybatis sql xml于接口映射关系 xml配置文件
  mapper-locations: classpath:mybatis/mappers/*.xml
  #全局配置文件地址 不建议使用这种方式建议使用下面的configuration替换取代全局配置文件
  # config-location: classpath:mybatis-config.xml
  configuration:
    map-underscore-to-camel-case: true

(2)、编写Mapper类

代码语言:javascript复制
package com.company.webtests.mappers;
import com.company.webtests.entities.ImUserMessage;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ImUserMessageMapper
{
    public ImUserMessage Get(String id);
}

(3)、编写Mapper对应xml

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.company.webtests.mappers.ImUserMessageMapper">
    <select id="Get" resultType="com.company.webtests.entities.ImUserMessage">
        select * from imusermessage where Id = #{Id}
    </select>
</mapper>

namespace-mapper类全路径

id-mapper类对应的方法

resultType-返回值类全路径

(4)、控制器请求方法

代码语言:javascript复制
    @GetMapping(value = "/request/getmessage")
    public ImUserMessage GetImUserMessage(@RequestParam("id") String id)
    {
        return ImUserMessageMapper.Get(id);
    }

(5)、使用注解式的sql编写方式非xml配置式

Mapper代码如下:

代码语言:javascript复制
    @Select("select * from imusermessage where Id = #{Id}")
    public ImUserMessage get(String id);

(6)、带有自增int主键的插入

i、xml配置代码如下:

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.company.webtests.mappers.TestMapper">
    <insert id="insert" useGeneratedKeys="true" keyProperty="Id">
        insert into Test(Name) values(#{Name})
    </insert>
</mapper>

ii、注解式

代码语言:javascript复制
     @Insert("insert into Test(Name) values(#{Name})")
     @Options(useGeneratedKeys = true,keyProperty = "Id")
     public void insert(Test test);

api测试代码如下:

代码语言:javascript复制
    @Autowired
    TestMapper TestMapper;

    @GetMapping(value = "/request/addtest")
    public Test addTest(@RequestParam("name") String name)
    {
         Test t=new Test().setName(name);
         TestMapper.insert(t);
         return t;
    }

0 人点赞