栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot整合Mybatis (简易)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot整合Mybatis (简易)

一、代码结构

二、 pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.4
         
    
    com.keafmd
    spring-boot-09-mybatis
    0.0.1-SNAPSHOT
    spring-boot-09-mybatis
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


三、配置文件

application.properties

server.port=80
# 配置数据源
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.username= root
spring.datasource.password= 18044229

# 整合mybatis
mybatis.mapper-locations=classpath*:com/neuedu/boot/mapper/*.xml

四、UserMapper.xml

在resources中新建mappers文件夹里面是 mapper.xml





    
        select * from user
    


UserMapper.java
package com.alibaba.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

public interface UserMapper {

    List list();
}

五、Main文件 在运行类上添加@MapperScan注解

package com.alibaba;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.alibaba.mapper")
public class SpringBoot09MybatisApplication {

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

}

 六、测试类

package com.fengqx.mapper;

import com.fengqx.SpringBoot09MybatisApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

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

@SpringBootTest(classes = SpringBoot09MybatisApplication.class)
class UserMapperTest {

    @Autowired
    UserMapper userMapper;

    @Test
    void list(){
        List list = userMapper.list();
        for (Object o : list) {
            System.out.println(o);
        }
    }

}

效果图

 完成连接数据库!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/332778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号