Spring boot2 數據訪問之Druid數據源+Mybatis

Mybatis官方地址

 

1、查找Mybatis 相關starter 官方文檔 

這裏注意查找指定版本的Starter

 這裏以2.2.2爲例,查看官方的pom.xml如下:

<?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相關信息,如下:

       <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相關

#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類

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

<?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)、控制器請求方法
    @GetMapping(value = "/request/getmessage")
    public ImUserMessage GetImUserMessage(@RequestParam("id") String id)
    {
        return ImUserMessageMapper.Get(id);
    }

 

(5)、使用註解式的sql編寫方式非xml配置式

Mapper代碼如下:

    @Select("select * from imusermessage where Id = #{Id}")
    public ImUserMessage get(String id);

 

(6)、帶有自增int主鍵的插入

i、xml配置代碼如下:

<?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、註解式

     @Insert("insert into Test(Name) values(#{Name})")
     @Options(useGeneratedKeys = true,keyProperty = "Id")
     public void insert(Test test);

 

api測試代碼如下:

    @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;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章