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

springBoot项目整合mybatis

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

springBoot项目整合mybatis

一、添加依赖

        org.springframework.boot
        spring-boot-parent
        2.1.2.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.2.8
        
        
        
            org.springframework.boot
            spring-boot-devtools
        
    
    
    
        
            
                src/main/java
                
                    **/*.xml
                
                false
            
        
    
二、创建Demo项目
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/getDept")
    public List getDeptInfo(){
        return deptService.getDeptInfo();
    }
}
public interface DeptService {
    List getDeptInfo();
}
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List getDeptInfo() {
        return deptMapper.getDeptInfo();
    }
}
@Mapper
public interface DeptMapper {
    List getDeptInfo();
}

    
        select * from dept
    

三、添加配置文件

配置文件

spring:
  datasource:
    username: root
    password: root
    url:  jdbc:mysql://localhost:3306/mybatis
    driver‐class‐name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  #mapper文件的映射
  mapper-locations: classpath:com/tedu/server/dao/mapper/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

其中数据库的用户名、密码、url可以先在idea中尝试一下,是否能连通

选择DataSource—>mysql

出现上图连接成功的字样,则表示连接没问题。(第一次连接时,会没有测试连接的按钮,需要在选择左侧红框,添加mysql的驱动)
driver‐class‐name驱动类的值,则可以在添加的驱动依赖包中进行查看

整个文件的目录结构如下:

启动类

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

启动main方法。
demo访问路径localhost:8080/getDept

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

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

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