完整整合Spring、Mybatis后端框架,通过controller层手动接收调用service层调用dao层再执行sql返回结果

1.新建一个model(启动类写dao层包扫描)

在这里插入图片描述

2.配置pom文件,加入依赖

<dependencies>
        <!--mybatis依赖包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!--jdbc依赖包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>

        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>

        </dependency>
    </dependencies>

3.在java包和dao、service同级下创建启动类(启动类写dao层包扫描)

package cn.tedu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("cn.tedu.dao")
public class RunApp {
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class,args);
    }
}

4.在resources中配置application.yml文件,才能正常启动启动类

#SpringBoot配置mysql信息
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///mybatisdb?useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: root
#SpringBoot整合Mybatis配置
mybatis:
  #指定UserMapper.xml文件的位置
  mapper-locations: classpath:*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

5.开始写pojo实体类

package cn.tedu.pojo;

import lombok.Data;

@Data
public class Car {
    private Integer id;
    private String name;
    private String color;
    private double price;
}

6.写dao层接口

package cn.tedu.dao;

import cn.tedu.pojo.Car;

import java.util.List;

public interface CarDao {
    /**
     * 查询所有车辆信息
     * @return  返回查出的list集合
     */
    List<Car> findAll();

    Car findById(Integer id);
}

7.根据dao层在resources中写CarMapper.xml (namespace是dao层接口全路径,sql语句的唯一标识id是接口中对应的方法名)

<?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="cn.tedu.dao.CarDao">
    <sql id="sql1">id,name,color,price</sql>
    <select id="findAll" resultType="cn.tedu.pojo.Car">
        select <include refid="sql1"></include> from car
    </select>
    <select id="findById" resultType="cn.tedu.pojo.Car">
        select <include refid="sql1"></include> from car
        where id = #{id}
    </select>
</mapper>

8.写service接口

package cn.tedu.service;

import cn.tedu.pojo.Car;

import java.util.List;

public interface CarService {
    List<Car> selectAll();
    Car selectById(Integer id);
}

9.写service实现类

package cn.tedu.service;

import cn.tedu.dao.CarDao;
import cn.tedu.pojo.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarDao carDao;
    @Override
    public List<Car> selectAll() {
        return carDao.findAll();
    }

    @Override
    public Car selectById(Integer id) {
        return carDao.findById(id);
    }
}

10.写controller类(restful风格需要在属性前加@PathVariable注解)

package cn.tedu.controller;

import cn.tedu.pojo.Car;
import cn.tedu.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Service;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarService carService;

    /**
     * http://localhost:8080/car/selectAll
     * @return
     */
    @RequestMapping("/selectAll")
    public List<Car> selectAll(){
        return carService.selectAll();
    }

    /**
     * http://localhost:8080/car/selectById/1
     * @param id
     * @return
     */
    @RequestMapping("/selectById/{id}")
    public Car selectById(@PathVariable Integer id){
        return carService.selectById(id);
    }
}

成功

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值