首先用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数据