SpringBoot之SSM完整版整合

1.项目目录

在此目录下面注意StudentDAO.xml文件所在的包名
在这里插入图片描述

2.先看建立项目时自动生成的 DemoApplication

package com.westos.springboothello.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//声明该类是一个SpringBoot的引导类
@SpringBootApplication(scanBasePackages = {"com.westos.springboothello.demo"})
@RestController
public class DemoApplication {
    //main方法是java程序的入口
	public static void main(String[] args) {
		//run方法:表示要运行SpringBoot的引导类,参数就是引导类的字节码对象
		SpringApplication.run(DemoApplication.class, args);
	}
}

3.写domain包下Student实体类

package com.westos.springboothello.demo.domain;

public class Student {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

4.写 dao包下的 StudentDAO接口

package com.westos.springboothello.demo.dao;

import com.westos.springboothello.demo.domain.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
    public interface StudentDAO {
    List<Student> listStudent(Student student);
    Integer save(Student student);
}

5.写controller包下的HelloController类

package com.westos.springboothello.demo.controller;

import com.westos.springboothello.demo.dao.StudentDAO;
import com.westos.springboothello.demo.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/aaa")
public class HelloController {

    @Autowired
    private StudentDAO studentDAO;
   
    @RequestMapping("/a1")
    public String aaa(){
        return "hello";
    }

    @RequestMapping("/a2")
    public ResponseEntity<List<Student>> listStudent(Student student){
        List<Student> studentlist = studentDAO.listStudent(student);
        if(studentlist==null||studentlist.isEmpty()){
            return  new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(studentlist,HttpStatus.OK);
    }
}

6.写com.westos.springboothello.demo.domain.Student包下的StudentDAO.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="com.westos.springboothello.demo.dao.StudentDAO">
    <select id="listStudent" resultType="com.westos.springboothello.demo.domain.Student">
        select * from student
        <where>
            <if test="name!=null">
                name=#{name}
            </if>
        </where>
    </select>
    <insert id="save">
        insert into student(id,name) values(#{id},#{name})
    </insert>
</mapper>

7.写application.yml配置文件

server:
  port: 8888
  servlet:
    context-path: /westos
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://localhost:3306/student
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

8.写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">
	<modelVersion>4.0.0</modelVersion>
	<!-- 所有的SpringBoot工程都必须继承spring-boot-starter-parent -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.westos.springboothello</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- web功能的起步依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>
        <!--MySQL起步依赖-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.47</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--热部署配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

9.数据库建表

在这里插入图片描述

10.点击运行,浏览器输入地址:http://localhost:8888/westos/aaa/a2

在这里插入图片描述
以上为SpringBoot之SSM完整版整合

发布了56 篇原创文章 · 获赞 6 · 访问量 7757

猜你喜欢

转载自blog.csdn.net/ly823260355/article/details/96020645