1
链接:https://pan.baidu.com/s/127D5bofzDcaOC9LZXWucog
提取码:kedc
1数据结构
2Student
package springboot_mybatis_mysql.bean;
public class Student {
/**
* 自增id
*/
private int id;
/**
* 学生姓名
*/
private String name;
/**
* 学生年龄
*/
private int age;
/**
* 学生性别
*/
private String sex;
/**
* 学生电话
*/
private String phone;
/**
* 学生存款
*/
private int money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Student(int id, String name, int age, String sex, String phone, int money) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.phone = phone;
this.money = money;
}
public Student() {
super();
}
}
3JsonData
package springboot_mybatis_mysql.bean;
import java.io.Serializable;
import springboot_mybatis_mysql.util.ResultUtils;
public class JsonData implements Serializable {
private static final long serialVersionUID = 1L;
// 状态码,0表示成功,-1表示失败
private int code;
// 结果
private Object data;
// 返回错误消息
private String msg;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public JsonData(int code, Object data, String msg) {
super();
this.code = code;
this.data = data;
this.msg = msg;
}
public static Object toJSON(ResultUtils resultUtils) {
return resultUtils;
}
}
4ResultUtils
package springboot_mybatis_mysql.util;
import springboot_mybatis_mysql.bean.JsonData;
public class ResultUtils {
/**
* 返回结果true:成功,false:失败
*/
private boolean success = true;
/**
* 返回信息
*/
private String msg;
/**
* 返回结果集
*/
private Object result;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
/*
* 返回成功结果
* */
public Object successResult(){
ResultUtils resultUtils = new ResultUtils();
resultUtils.setSuccess(true);
resultUtils.setMsg("success");
resultUtils.setResult(null);
return JsonData.toJSON(resultUtils);
}
/*
* 返回成功结果
* */
public Object successResult(Object obj){
//判断参数obj是否是String类型
if (obj instanceof String){
return successResult(null, (String) obj);
}else{
return successResult(obj, "error");
}
}
/*
* 返回成功结果
* */
public Object successResult(Object obj, String msg){
ResultUtils resultUtils = new ResultUtils();
resultUtils.setSuccess(true);
resultUtils.setMsg(msg);
resultUtils.setResult(obj);
return JsonData.toJSON(resultUtils);
}
/*
* 返回错误结果
* */
public Object errorResult(){
ResultUtils resultUtils = new ResultUtils();
resultUtils.setSuccess(false);
resultUtils.setMsg("error");
resultUtils.setResult(null);
return JsonData.toJSON(resultUtils);
}
/*
* 返回错误结果
* */
public Object errorResult(Object obj){
if (obj instanceof String){
return renderJsonError(null, (String) obj);
}else{
return renderJsonError(obj, "error");
}
}
/*
* 返回错误结果
* */
public Object renderJsonError(Object obj, String msg){
ResultUtils resultUtils = new ResultUtils();
resultUtils.setSuccess(false);
resultUtils.setMsg(msg);
resultUtils.setResult(obj);
return JsonData.toJSON(resultUtils);
}
}
5StudentDao
package springboot_mybatis_mysql.dao;
import java.util.List;
import springboot_mybatis_mysql.bean.Student;
/**
* studentDao
* @author Administrator
*
*/
public interface StudentDao {
/**
* 查询所有学生
* @return
*/
List<Student> findAll();
/**
* 新增学生
* @param student
*/
void insertStudent(Student student);
/**
* 修改学生
* @param student
*/
void updateStudent(Student student);
/**
* 根据id查询学生
* @param id
* @return
*/
Student findStudentById(int id);
/**
* 根据id删除学生
* @param id
*/
void deleteStudentById(int id);
}
6StudentService
package springboot_mybatis_mysql.service;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import springboot_mybatis_mysql.bean.Student;
/**
* 学生Service接口
* @author Administrator
*
*/
public interface StudentService {
/**
* 查询所有学生信息
* @return
*/
List<Student> findAll();
/**
* 新增学生信息
* @param student
*/
void insertStudent(Student student);
/**
* 修改学生信息
* @param student
*/
void updateStudent(Student student);
/**
* 根据id查询学生信息
* @param id
* @return
*/
Student findStudentById(int id);
/**
* 根据id删除学生信息
* @param id
*/
void deleteStudentById(int id);
}
7StudentServiceImpl
package springboot_mybatis_mysql.service.serviceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot_mybatis_mysql.bean.Student;
import springboot_mybatis_mysql.dao.StudentDao;
import springboot_mybatis_mysql.service.StudentService;
/**
* 学生Service的实现类
* @author Administrator
*
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
/**
* 查询所有学生
*/
@Override
public List<Student> findAll() {
// TODO Auto-generated method stub
return studentDao.findAll();
}
/**
* 新增学生
*/
@Override
public void insertStudent(Student student) {
studentDao.insertStudent(student);
}
/**
* 修改学生
*/
@Override
public void updateStudent(Student student) {
// TODO Auto-generated method stub
studentDao.updateStudent(student);
}
/**
* 根据id查询学生
*/
@Override
public Student findStudentById(int id) {
return studentDao.findStudentById(id);
}
/**
* 根据id删除学生
*/
@Override
public void deleteStudentById(int id) {
// TODO Auto-generated method stub
studentDao.deleteStudentById(id);
}
}
8StudentController
package springboot_mybatis_mysql.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springboot_mybatis_mysql.bean.Student;
import springboot_mybatis_mysql.service.StudentService;
import springboot_mybatis_mysql.util.ResultUtils;
@Api(description = "学生信息接口")
@Controller
@RequestMapping("/api/v1/student")
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 获取学生
* @ApiOperation 描述接口
* @return
*/
@ApiOperation(value="查询所有学生",notes="查询所有学生")
@RequestMapping(value="findAll",method=RequestMethod.GET)
public ModelAndView findAll() {
ModelAndView model=new ModelAndView("student/student_list");
model.addObject("student",studentService.findAll());
return model;
}
/**
* 根据id来获取学生
* @param id
* @return
*/
@ApiOperation(value="根据id查询所有学生",notes="根据id查询所有学生")
@ApiImplicitParams({
//@ApiImplicitParam:一个请求参数
@ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query", dataType = "int")
})
@RequestMapping(value="findStudentById",method=RequestMethod.GET)
public ModelAndView findStudentById(int id) {
ModelAndView model=new ModelAndView("student/edit");
if(id!=0) {
model.addObject("student",studentService.findStudentById(id));
}
return model;
}
/**
* 根据id来删除学生
* @param id
* @return
*/
@ApiOperation(value="根据id删除学生",notes="根据id删除学生")
@ApiImplicitParams({
//@ApiImplicitParam:一个请求参数
@ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query", dataType = "int" )
})
@RequestMapping(value="deleteStudentById",method=RequestMethod.GET)
@ResponseBody
public Object deleteStudentById(int id) {
ResultUtils res=new ResultUtils();
try {
studentService.deleteStudentById(id);
} catch (Exception e) {
res.errorResult();
}
return res.successResult();
}
@ApiOperation(value = "修改学生" , notes="修改学生")
@ApiImplicitParams({
@ApiImplicitParam(name = "student", value = "实体对象", required = true, paramType = "body", dataType = "Student")
})
@RequestMapping(value = "updateStudent",method=RequestMethod.POST)
@ResponseBody
public Object updateStudent(Student student) {
ResultUtils res = new ResultUtils();
try {
if(student.getId()!=0) {
studentService.updateStudent(student);
}
} catch (Exception e) {
return res.errorResult();
}
return res.successResult();
}
@ApiOperation(value = "新增学生" , notes="新增修改学生")
@ApiImplicitParams({
@ApiImplicitParam(name = "student", value = "实体对象", required = true, paramType = "body", dataType = "Student")
})
@RequestMapping(value = "updateStudent",method=RequestMethod.POST)
@ResponseBody
public Object insertStudent(Student student) {
ResultUtils res = new ResultUtils();
try {
if(student.getId()!=0) {
studentService.insertStudent(student);
}
} catch (Exception e) {
return res.errorResult();
}
return res.successResult();
}
}
9StudentMapper.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="springboot_mybatis_mysql.dao.StudentDao">
<resultMap id="BaseResultMap" type="springboot_mybatis_mysql.bean.Student">
<result column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
<result column="sex" property="sex" />
<result column="phone" property="phone" />
<result column="money" property="money" />
</resultMap>
<parameterMap id="Student" type="springboot_mybatis_mysql.bean.Student"/>
<sql id="Base_Column_List">
id, name, age, sex, phone, money
</sql>
<!--获取全部学生-->
<select id="findAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
</select>
<!--新增学生-->
<insert id="insertStudent" parameterType="Student">
insert into student (name, age, sex, phone, money)
values (#{name}, #{age}, #{sex}, #{phone}, #{money})
</insert>
<!--修改学生-->
<update id="updateStudent" parameterType="Student">
update student set
name = #{name}, age = #{age}, sex = #{sex}, phone = #{phone}, money = #{money}
where id = #{id}
</update>
<!--根据id获取学生-->
<select id="findStudentById" resultMap="BaseResultMap" parameterType="int">
select
<include refid="Base_Column_List" />
from student where id = #{id}
</select>
<!--根据id删除学生-->
<delete id="deleteStudentById" parameterType="int">
delete from student where id = #{id}
</delete>
</mapper>
10application.properties
web.upload-path=G:study_toolmaven_workspaceimages
#u9759u6001u8D44u6E90u6587u4EF6
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path},classpath:/thymeleaf/
#u6307u5B9Au67D0u4E9Bu6587u4EF6u4E0Du8FDBu884Cu76D1u542CuFF0Cu5373u4E0Du8FDBu884Cu70EDu52A0u8F7D
#spring.devtools.restart.exclude=application.properties
#u901Au8FC7u89E6u53D1u5668uFF0Cu6765u63A7u5236u4EC0u4E48u65F6u5019u8FDBu884Cu52A0u8F7Du70EDu90E8u7F72u7684u6587u4EF6
spring.devtools.restart.trigger-file=trigger.txt
#u7AEFu53E3u5730u5740
server.port=8080
#u6587u4EF6u4E0Au4F20u8DEFu5F84
web.file.path=G:\study_tool\maven_workspace\demo\src\main\resources\static\image\
#u6D4Bu8BD5u670Du52A1u5668u7684u8BBFu95EEu540Du79F0
test.name=jimao
#u6D4Bu8BD5u670Du52A1u5668u7684u8BBFu95EEu5730u5740
test.domain=liming
#Freemarkeru7684u57FAu7840u914Du7F6E
#Freemarkeru662Fu5426u5F00u542Fthymeleafu7F13u5B58uFF0Cu672Cu5730u4E3Aflase,u751Fu4EA7u6539u4E3Atrue
spring.freemarker.cache=false
#Freemarkeru7F16u7801u683Cu5F0F
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
#Freemarkeru7C7Bu578B
spring.freemarker.content-type=text/html; charset=utf-8
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
#Freemarkeru6587u4EF6u540Eu7F00
spring.freemarker.suffix=.ftl
#Freemarkeru8DEFu5F84
spring.freemarker.template-loader-path=classpath:/templates/
#u6574u5408thymeleafu76F8u5173u914Du7F6E
#u5F00u53D1u65F6u5173u95EDu7F13u5B58uFF0Cu4E0Du7136u6CA1u6CD5u770Bu89C1u5B9Eu65F6u9875u9762
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#thymeleafu8DEFu5F84
spring.thymeleaf.prefix=classpath:/templates/tl/
#thymeleafu7F16u7801u683Cu5F0F
spring.thymeleaf.encoding=UTF-8
#thymeleafu7C7Bu578B
spring.thymeleaf.servlet.content-type=text/html; charset=utf-8
#thymeleafu540Du79F0u540Eu7F00
spring.thymeleaf.suffix=.html
#u6574u5408mysqlu7684u914Du7F6Eu6587u4EF6
#mysqlu52A0u8F7Du9A71u52A8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#jdbcu6570u636Eu5E93u8FDEu63A5
spring.datasource.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#mysqlu8D26u53F7
spring.datasource.username=root
#mysqlu5BC6u7801
spring.datasource.password=456789
#u6570u636Eu8FDEu63A5u6E90,u5982u679Cu6CE8u91CAu6389uFF0Cu6570u636Eu6E90u4F7Fu7528u9ED8u8BA4u7684uFF08com.zaxxer.hikari.HikariDataSourceuFF09
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#u63A7u5236u53F0u6253u5370mybticsu7684sqlu8BEDu53E5
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
11SwaggerConfig
package springboot_mybatis_mysql;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//加载该类的配置
@Configuration
//启动Swagger2
@EnableSwagger2
public class SwaggerConfig {
/**
* 创建api应用
* @return
*/
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2).select().
apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).
build();
}
}
13SpringbootMybaticsMysqlApplication
14pom.xml
<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>
<groupId>springboot_mybatis_mysql</groupId>
<artifactId>springboot_mybatis_mysql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- web依赖的jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署依赖的jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- springdata jpa -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> -->
<!-- 引入mybatis的starter的包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
<!-- <scope>runtime</scope> -->
</dependency>
<!-- mysql的驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <scope>runtime</scope> -->
</dependency>
<!-- 引入第三方驱动源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!-- 引入模板引擎thymeleaf,作为页面渲染 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 引入freemark的模板引擎的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- poi导入和导出的jar -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- swagger ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<!-- junit测试jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>