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

手写简易- SpringBoot项目整合mybatis

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

手写简易- SpringBoot项目整合mybatis

1.创建spring boot项目

2.在pom.xml 文件中添加依赖


        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
        
            mysql
            mysql-connector-java
        

        
        
            com.alibaba
            druid
            1.1.14
        

        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
        
            org.projectlombok
            lombok
            1.16.20
            provided
        
3.配置项目jdk


4.application.yml
#端口号
server:
  port: 8099

spring:
  #服务名称
  application:
    name: springboot-01

  #数据库配置
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/pk?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver

#指定mybatis映射文件的路径
mybatis:
 #数据表映射配置文件路径
  mapper-locations: classpath:mapper/*.xml
  #数据表映射实体类路径
  type-aliases-package: com/example/springboot01/pojo
5.数据库表设计

表中添加数据

6.代码结构

7.启动器
package com.example.springboot01;

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

@SpringBootApplication
@MapperScan("com.example.springboot01.dao")
public class Springboot01Application {

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

}

8. pojo类
package com.example.springboot01.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Rank implements Serializable {

    private Integer id;
    private String name;
    private BigDecimal bonus;
    private Date created;
    private Date modified;
}

9. dao类
package com.example.springboot01.dao;

import com.example.springboot01.pojo.Rank;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface RankDao {

    List findAll();
}

10.RankMapper.xml



    
        select * from rank
    

11.service 类
package com.example.springboot01.service;

import com.example.springboot01.dao.RankDao;
import org.springframework.beans.factory.annotation.Autowired;

public class RankService {

    List findAll();

}

12.RankServiceImpl实现类
package com.example.springboot01.service.Impl;

import com.example.springboot01.dao.RankDao;
import com.example.springboot01.pojo.Rank;
import com.example.springboot01.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service
public class RankServiceImpl implements RankService {
    @Autowired
    private RankDao rankDao;

    @Override
    public List findAll() {
        return rankDao.findAll();
    }
}

13. controller
package com.example.springboot01.controller;

import com.example.springboot01.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RankController {

    @Autowired
    private RankService rankService;

    @ResponseBody
    @RequestMapping("/test")
    public String getRanks(){
        return rankService.findAll().toString();

    }


}

14.结果

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

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

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