各位看官大家好,最近一直在研究SpringBoot框架以及整合相关框架的使用,上次使用SpringBoot+Mybatis+echarts使用JSP模板引擎,实现数据可视化的小案例,这次给大家带来SpringBoot+Mybatis+Layui使用Thymeleaf模板引擎实现学生信息的增删改查,直接进入主题,案例目录结构如下:
pom文件,引入需要的依赖:
4.0.0
springboot
com.springboot-mybatis4
war
0.0.1-SNAPSHOT
com.springboot-mybatis4 Maven Webapp
http://maven.apache.org
3.0.9.RELEASE
2.2.2
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
junit
junit
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
true
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.oracle
ojdbc7
12.1.0.2
net.sf.json-lib
json-lib
2.4
jdk15
org.springframework.boot
spring-boot-starter-thymeleaf
com.springboot-mybatis4
新建java实体类:
package com.springboot.entity;
public class student {
private String name;
private String math;
private String chinese;
private String language;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMath() {
return math;
}
public void setMath(String math) {
this.math = math;
}
public String getChinese() {
return chinese;
}
public void setChinese(String chinese) {
this.chinese = chinese;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public String toString() {
return "student [name=" + name + ", math=" + math + ", chinese=" + chinese + ", language=" + language + "]";
}
}
数据库mapper映射接口:
package com.springboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.springboot.entity.student;
@Mapper
public interface studentMapper {
//根据姓名查询学生
public student getStudentByName(String name);
//查询所有
public List getStudentInfo();
//新增学生信息
public Integer addStudent(student stu);
//修改信息
public int changeInfoByName(student stu);
//删除学生信息
public int deleteInfoByName(String name);
}
新建Service接口:
package com.springboot.service;
import java.util.List;
import com.springboot.entity.student;
public interface studentService {
public List getStudentInfo();
//根据姓名查询学生
public student getStudentByName(String name);
//新增学生信息
public Integer addStudent(student stu);
//修改信息
public int changeInfoByName(student stu);
//删除学生信息
public int deleteInfoByName(String name);
}
新建实现Service接口的类:
package com.springboot.service.Impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.entity.student;
import com.springboot.mapper.studentMapper;
import com.springboot.service.studentService;
@Service
public class studentServiceImpl implements studentService {
@Autowired //自动装配
studentMapper stuMapper;
public List getStudentInfo() {
return stuMapper.getStudentInfo();
}
public student getStudentByName(String name) {
return stuMapper.getStudentByName(name);
}
public Integer addStudent(student stu) {
return stuMapper.addStudent(stu);
}
public int changeInfoByName(student stu) {
return stuMapper.changeInfoByName(stu);
}
public int deleteInfoByName(String name) {
return stuMapper.deleteInfoByName(name);
}
}
Controller层,使用restful风格的接口,简介明了:
package com.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springboot.entity.student;
import com.springboot.service.studentService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
/*
* 不使用@RestController,否则返回return后面的数据或者字符串,而不是html页面
* @RestController相当于, @Controller和@ResponseBody的组合注解
*/
public class studentController {
@Autowired
studentService stuService;
@GetMapping("/all")
public String getInfo(Model model) {
//将多个对象封装为一个json数组
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
List list = stuService.getStudentInfo();
for(int i=0;i
前面html文件实现简单增删改查功能:
学生后台管理系统
学生信息管理系统
选择框
序号
姓名
数学
语文
英语
姓名:
数学:
英语:
语文:
数据库Mapper映射文件:
insert into student(name,math,language,chinese) values (#{name},#{math},#{language},#{chinese})
update student set math=#{math},language=#{language},chinese=#{chinese} where name=#{name}
delete from student where name = #{name}
项目全局配置文件:application.yaml:
spring:
datasource:
url: jdbc:oracle:thin:@数据库服务器IP:1521:ylorcl
username: 账户名
password: 密码
driver-class-name: oracle.jdbc.OracleDriver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.springboot.entity.student
logging:
level:
com.springboot.mapper : debug
最后在Chrome浏览器输入:http://localhost:8080/all ,显示以下界面:
以上学生的增删改查功能都已实现,有兴趣的小伙伴可以亲自尝试,源码地址:https://github.com/evanZhangYiFeng/SpringBoot
欢迎大家留言一起探讨,感谢大家完整看完!