SSM入门案例

系统架构图

流程图

项目结构

在这里插入图片描述

需求

访问:http://localhost:8080/car/get
返回:{“name”:“BMW”,“color”:“red”,“price”:9.9}

1.准备表,数据

CREATE TABLE `car` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(10) default NULL,
  `color` varchar(10) default NULL,
  `price` double default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.配置pom.xml核心文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springbootcgb2103</artifactId>
        <groupId>cn.tedu</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>testSSM</artifactId>
    
    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

3.car.java

存在Moudle层,用来封装Car的数据

package cn.tedu.pojo;

import org.springframework.stereotype.Component;

@Component
public class Car {
    private int id;
    private String name;
    private String color;
    private double price;
    
    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}

4.CarDao接口

存在Dao层,Mybatis框架

  • 可以在尚未实现具体DAO的时候编写上层代码,如Service里对DAO的调用
  • 可以为DAO进行多实现,例如有JDBCDAO实现,MyBatisDAO实现,而不需要更改上层代码,只需要简单的在Spring的IoC配置里修改一下注入的DAO实现
package cn.tedu.dao;

import cn.tedu.pojo.Car;
import org.springframework.stereotype.Repository;

public interface CarDao {
    Car get();
}

5.CarMapper.xml

Mybatis模式,实现Dao层接口,用来编写Sql语句实现对数据库的操作

<?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">
    <select id="get" resultType="cn.tedu.pojo.Car">
        select * from car where price=9.9
    </select>
</mapper>

6.CarService接口

Spring框架
Service接口,可以在尚未实现具体Service情况下编写上层改代码,如Controller对Service的调用,可以对Service进行多实现

package cn.tedu.service;

import cn.tedu.pojo.Car;

public interface CarService {
    Car get() ;
}

7.CarServiceImpl.java

Spring框架
Service接口的实现类
通过注入依赖,与Dao层同步

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;

@Service
public class CarServiceImpl implements CarService{
    @Autowired
    private CarDao carDao;
    @Override
    public Car get() {
        return carDao.get();
    }
}

8.CarController.java

SpringMVC框架
通过注入依赖和Servioce同步,与前端进行交互

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/car/")
public class CarController {
    @Autowired
    private CarService carService;
    @RequestMapping("get")
    public Car get(){
        return carService.get();
    }
}

9.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

10.RunApp.java

程序入口,固定格式

package cn.tedu; 

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

@SpringBootApplication
@MapperScan("cn.tedu.dao")//扫描DAO接口文件所在的包
public class Runapp {
    public static void main(String[] args) {
        SpringApplication.run(Runapp.class);
    }
}

11.测试

访问:http://localhost:8080/car/get
返回:{"name":"BMW","color":"red","price":9.9}

新增需求:
访问:http://localhost:8080/car/getById?id=2
返回: {"id":2,"name":"Audi","color":"black","price":0.3}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶尔也吹晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值