程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

SpringBoot通过xml配置文件方式整合mybatis

发布于2021-03-13 13:47     阅读(809)     评论(0)     点赞(5)     收藏(0)


SpringBoot通过xml整合mybatis

1.导入相关依赖

SprintBoot使用mybatis连接Mysql数据库,因此需要导入mybatisMySQL驱动这两个依赖。其次需要导入一个druid的连接池依赖。
mybatis依赖和druid依赖我们可以去maven仓库进行查找。maven仓库(注:要用spring-boot-stater-mybatis和spring-boot-stater-druid),并将其xml代码复制到项目的pom.xml中。

 <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
    

2.创建实体类

创建你想要获取的表对应的Java实体类。可以通过IDEA创建也可以手工创建。

package com.hky.springboothelloworld.entity;

import lombok.Data;

@Data
public class UserTest {

    private String name;

    private String password;
}

3.创建mapper接口

由于mybatis是用mapper接口访问数据库,因此在实体类父目录下创建mapper文件夹,并在mapper下创建UserMapper接口。

package com.hky.springboothelloworld.mapper;


import com.hky.springboothelloworld.entity.User;
import com.hky.springboothelloworld.entity.UserTest;

import java.util.List;

public interface UserMapper {

    List<UserTest> findAll();

}

4.扫描mapper接口

mapper接口已经创建,但是项目并不知道mapper在哪里,因此要在启动类中添加@MapperScan(basepackage= “mapper文件夹的路径”)。
这样启动类就知道mapper接口的位置。

@SpringBootApplication
@MapperScan(basePackages = "com.hky.springboothelloworld.mapper")
public class SpringbootHelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootHelloworldApplication.class, args);
    }

}

5.实现mapper接口

虽然创建了UserMapper接口,但是其中只有一个空方法findAll。因此要对其进行配置。
在resource目录下创建mapper目录,在该目录下创建对应mapper接口的xml文件。
创建后可以去网上搜索mybatis xml配置文件相关约束。

<?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.hky.springboothelloworld.mapper.UserMapper"> #namespace的值应该是mapper接口包名
    <select id="findAll" resultType="UserTest"> #实现sql操作。resultType=“实体类名字” 。
        select * from usertest;
    </select>
</mapper>

6.配置yml文件

虽然创建了实体类、mapper接口、mapper接口的xml配置文件,但是该配置文件有三个问题:
1.项目工程不知道该配置文件在哪里。
2.没有开启驼峰映射。
3.上述代码中resultType=“实体类名字”,该配置文件并不知道这个别名代指的是谁。
因此,我们需要在yml配置文件中写入一下内容:

mybatis:
  mapper-locations: classpath:mapper/*.xml  #指定mapper接口的xml配置文件的路径
  type-aliases-package: com.hky.springboothelloworld.entity #指定别名对应的实体在哪里
  configuration:
    map-underscore-to-camel-case: true	#驼峰映射

在上述内容都实现后,就是在yml文件中写入数据库连接所需要的东西,包括:驱动,账号,密码,url,连接池。

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver #由于已经导入MySQL依赖,这里选择带CJ的驱动
    url: jdbc:mysql://192.168.1.105:3306/test?serverTimezone=UTC #这里由于驱动版本高,因此需要指定时区为东八区
    type: com.alibaba.druid.pool.DruidDataSource
    username: dev
    password: "XXX"

7.测试

上面的工作都完成后就可以进行测试了。
我们直接对我们创建的mapper接口右键,点击goto->test,并勾选findAll函数。IDEA会自动帮我们创建测试类。

package com.hky.springboothelloworld.mapper;

import com.hky.springboothelloworld.SpringbootHelloworldApplicationTests;
import com.hky.springboothelloworld.entity.User;
import com.hky.springboothelloworld.entity.UserTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class UserMapperTest extends SpringbootHelloworldApplicationTests { #这里继承主测试类

    @Autowired 
    private UserMapper usermapper; 

    @Test
    void findAll() {
    List<UserTest> result = usermapper.findAll();
        for (UserTest user : result) {
            System.out.println(user);   #输出读取的数据库内容
        }
    }
}

原文链接:https://blog.csdn.net/weixin_44275712/article/details/114656430



所属网站分类: 技术文章 > 博客

作者:Hdhhd

链接:http://www.javaheidong.com/blog/article/114344/d8b8df716cc0ee8aa3a1/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)