SpringBoot整合ssm快速搭建开发环境

首先用idea创建一个springboot项目

下一步 选择项目名和一些配置

勾选web  Mysql  mybatis

创建对应的包

完事之后我来贴上示例代码

实体类

package com.hw.entity;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-04 20:17
 **/
public class Student {
    private Integer sid;
    private String sname;
    private Integer tid;

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", tid=" + tid +
                '}';
    }

    public Student(Integer sid, String sname, Integer tid) {
        this.sid = sid;
        this.sname = sname;
        this.tid = tid;
    }

    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Student(Integer sid, String sname) {
        this.sid = sid;
        this.sname = sname;
    }

    public Student() {
    }

}

server包下的接口

package com.hw.services;

import com.hw.entity.Student;

import java.util.List;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-04 20:19
 **/
public interface StudentsServices {
    public List<Student> getAllStudents();
}

server包下的impl包下的实现类,记得类上面加上@Server的注解

package com.hw.services.impl;

import com.hw.entity.Student;
import com.hw.mapper.StudentsMapper;
import com.hw.services.StudentsServices;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-04 20:20
 **/
@Service
public class StudentsServicesImpl implements StudentsServices {
    @Autowired
    private StudentsMapper studentsMapper;


    @Override
    public List<Student> getAllStudents() {
        return studentsMapper.getAllStudents();
    }
}

mapper包下和数据库打交道的类

package com.hw.mapper;

import com.hw.entity.Student;

import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-04 20:21
 **/
public interface  StudentsMapper {
    @Select("select * from student")
    public List<Student> getAllStudents();
}

controller层的控制类

package com.hw.controller;

import com.hw.entity.Student;
import com.hw.services.StudentsServices;

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;

import java.util.List;

/**
 * @program: Maven
 * @description:
 * @author: hw
 * @create: 2019-01-04 21:19
 **/
@RestController
public class selectStudentController {
    @Autowired  //注入    完成自动装配
    private StudentsServices studentsServices;

    @RequestMapping("/getStudents")
    public List<Student> getAllStudents(){
        System.out.println("集合长度:"+studentsServices.getAllStudents().size());
        return studentsServices.getAllStudents();
    }

}

然后是整个项目的application.properties

#设置端口
server.port=8091
#设置访问的链接
server.servlet.context-path=/hw

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8


#配置数据库信息
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#设置别名
mybatis.type-aliases-package=com.hw.entity

最后是启动类,也就是项目自动帮我们建好的类,我们需要加上一个扫码注解

package com.hw;

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

@MapperScan("com.hw.mapper")    //设置指定要扫描的包
@SpringBootApplication
public class SpringbootSsmApplication {

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

}

最后启动我们的项目后访问

直接返回的是json数据

 

点赞或者评论是我最大的动力,有问题欢迎留言或者联系q:1559810637

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值