Springboot整合SSM

Springboot整合SSM1.1创建SSM模块1.1.1系统架构图1.1.2项目结构1.1.3需求访问:http://localhost:8080/car/get返回:{“name”:”BMW”,”color”:”red”,”price”:9.9}1.1.4准备表,数据CREATETABLE`car`(`id`int(11)NOTNULLauto_increment,`name`varchar(10)defaultNULL,`color`varchar(10)d

大家好,又见面了,我是你们的朋友全栈君。

1.1 创建SSM模块

1.1.1 系统架构图

在这里插入图片描述

1.1.2 项目结构

在这里插入图片描述

1.1.3 需求

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

1.1.4 准备表,数据

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;

1.1.5 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>

1.1.6 Car.java

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;
    }

}

1.1.7 CarDao接口

package cn.tedu.dao;

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

public interface CarDao { 
   
    Car get();
}

1.1.8 CarMapper.xml

<?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>

1.1.9 CarService接口

package cn.tedu.service;

import cn.tedu.pojo.Car;

public interface CarService { 
   
    Car get() ;
}

1.1.10 CarServiceImpl.java

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();
    }
}

1.1.11 CarController.java

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();
    }
}

1.1.12 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:  
	#别名包
  type-aliases-package: cn.tedu.pojo
  #指定UserMapper.xml文件的位置
  mapper-locations: classpath:*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

1.1.13 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);
    }
}

1.1.15 测试

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

1.1.14 [新增网页]

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js"></script>
		<script src="axios.min.js"></script>
	</head>
	<body>
		<div id="app">
			<table style="font-size: 20px;" cellspacing="0" border="1px" bgcolor="lightgray">
				<h3 align="center">京淘电商平台</h3>
				<button>新增</button>
			   <head>
			      <tr>
			         <th style="text-align: center;" width="320">姓名</th>
			         <th style="text-align: center;" width="280">年龄</th>
			         <th style="text-align: center;">爱好</th>
			         <th style="text-align: center;">学历</th>
			         <th style="text-align: center;">入学时间</th>
			         <th style="text-align: center;">[操作]</th>
			      </tr>
			   </head>
			   <body>
			      <tr v-for="o in msg" align="center">
			         <td>{
  
  {o.name}}</td>
			         <td>{
  
  {o.age}}</td>
			         <td align="right"><span style="color: red;">{
  
  {o.hobby}}</span></td>
					 <td v-if="o.edu==3">本科</td>
					 <td>{
  
  {o.intime.substring(0,9)}}</td>
					 <td>
						 <button>修改</button>
						 <button>删除</button>
					 </td>
			      </tr>
			   </body>
			</table>	
		</div>
	</body>
	<script> new Vue({ 
      el:"#app", data:{ 
      msg: "暂无数据" }, mounted() { 
      axios({ 
      method: "post", url: "http://localhost:8080/car/get" }) .then( res => { 
      this.msg = res.data; } ) } }) </script>
</html>

注意:::::Controller层要加一个注解才行
在这里插入图片描述

新增需求:

访问:http://localhost:8080/car/getById?id=2
返回: { 
   "id":2,"name":"Audi","color":"black","price":0.3}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134124.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 什么是SOAP,有哪些应用

    什么是SOAP,有哪些应用SOAP是一种轻量级协议,用于在分散型、分布式环境中交换结构化信息。SOAP利用XML技术定义一种可扩展的消息处理框架,它提供了一种可通过多种底层协议进行交换的消息结构。这种框架的设计思想是要独立于任何一种特定的编程模型和其他特定实现的语义。SOAP规范还定义了HTTP消息是怎样传输SOAP消息的。MSMQ、SMTP、TCP/IP都可以做SOAP的传输协议。转载于:ht…

    2022年7月24日
    6
  • Java IO流自动关闭

    Java IO流自动关闭Filef=newFile();Filedes=newFile();try(FileInputStreaminput=newFileInputStream(f);FileOutputStreamout=newFileOutputStream(des);){}catch(FileNotFoundExceptione1){ }catch(IO…

    2022年5月27日
    33
  • kettle工具使用教程

    kettle工具使用教程kettle工具使用教程Kettle简介Kettle是一款国外开源的ETL工具,纯java编写,可以在Window、Linux、Unix上运行,数据抽取高效稳定。Kettle中文名称叫水壶,该项目的主程序员MATT希望把各种数据放到一个壶里,然后以一种指定的格式流出。Kettle这个ETL工具集,它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述你想做什么,而不是你想怎么…

    2022年10月16日
    4
  • oracle索引视图_位图联合索引

    oracle索引视图_位图联合索引一.什么是位图索引我们目前大量使用的索引一般主要是B*Tree索引,在索引结构中存储着键值和键值的RowID,并且是一一对应的.而位图索引主要针对大量相同值的列而创建(例如:类别,操作员,部门ID,库房ID等),索引块的一个索引行中存储键值和起止Rowid,以及这些键值的位置编码,位置编码中的每一位表示键值对应的数据行的有无.一个位图索引块可能指向的是几十甚至成百上千行数据的位置.这种方式存储数据…

    2025年7月17日
    3
  • 离线安装python第三方库_python安装whl文件失败

    离线安装python第三方库_python安装whl文件失败1、安装目标库1、首先,选择你要导入的库文件,如seaborn库下载网站:https://pypi.org/或https://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy2、在下载路径下空白处,按住Shift+鼠标右键,选择在此处打开命令窗口执行安装命令pipinstallseaborn-0.10.1-py3-none-any.whl(如有报错,详见…

    2022年8月27日
    6
  • Java8的其它 新特性

    Java8的其它 新特性

    2021年7月9日
    75

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号