整合springboot+mybatis+mysql之增删改查(四)

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_tool\maven_workspace\images
#\u9759\u6001\u8D44\u6E90\u6587\u4EF6
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path},classpath:/thymeleaf/
#\u6307\u5B9A\u67D0\u4E9B\u6587\u4EF6\u4E0D\u8FDB\u884C\u76D1\u542C\uFF0C\u5373\u4E0D\u8FDB\u884C\u70ED\u52A0\u8F7D
#spring.devtools.restart.exclude=application.properties
#\u901A\u8FC7\u89E6\u53D1\u5668\uFF0C\u6765\u63A7\u5236\u4EC0\u4E48\u65F6\u5019\u8FDB\u884C\u52A0\u8F7D\u70ED\u90E8\u7F72\u7684\u6587\u4EF6
spring.devtools.restart.trigger-file=trigger.txt
#\u7AEF\u53E3\u5730\u5740
server.port=8080
#\u6587\u4EF6\u4E0A\u4F20\u8DEF\u5F84
web.file.path=G:\\study_tool\\maven_workspace\\demo\\src\\main\\resources\\static\\image\\
#\u6D4B\u8BD5\u670D\u52A1\u5668\u7684\u8BBF\u95EE\u540D\u79F0
test.name=jimao
#\u6D4B\u8BD5\u670D\u52A1\u5668\u7684\u8BBF\u95EE\u5730\u5740
test.domain=liming

 

#Freemarker\u7684\u57FA\u7840\u914D\u7F6E
#Freemarker\u662F\u5426\u5F00\u542Fthymeleaf\u7F13\u5B58\uFF0C\u672C\u5730\u4E3Aflase,\u751F\u4EA7\u6539\u4E3Atrue
spring.freemarker.cache=false
#Freemarker\u7F16\u7801\u683C\u5F0F
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true

 

#Freemarker\u7C7B\u578B
spring.freemarker.content-type=text/html; charset=utf-8
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true

 

#Freemarker\u6587\u4EF6\u540E\u7F00
spring.freemarker.suffix=.ftl
#Freemarker\u8DEF\u5F84
spring.freemarker.template-loader-path=classpath:/templates/

 


#\u6574\u5408thymeleaf\u76F8\u5173\u914D\u7F6E
#\u5F00\u53D1\u65F6\u5173\u95ED\u7F13\u5B58\uFF0C\u4E0D\u7136\u6CA1\u6CD5\u770B\u89C1\u5B9E\u65F6\u9875\u9762
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#thymeleaf\u8DEF\u5F84
spring.thymeleaf.prefix=classpath:/templates/tl/
#thymeleaf\u7F16\u7801\u683C\u5F0F
spring.thymeleaf.encoding=UTF-8
#thymeleaf\u7C7B\u578B
spring.thymeleaf.servlet.content-type=text/html; charset=utf-8
#thymeleaf\u540D\u79F0\u540E\u7F00
spring.thymeleaf.suffix=.html

 


#\u6574\u5408mysql\u7684\u914D\u7F6E\u6587\u4EF6
#mysql\u52A0\u8F7D\u9A71\u52A8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#jdbc\u6570\u636E\u5E93\u8FDE\u63A5
spring.datasource.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#mysql\u8D26\u53F7
spring.datasource.username=root
#mysql\u5BC6\u7801
spring.datasource.password=456789
#\u6570\u636E\u8FDE\u63A5\u6E90,\u5982\u679C\u6CE8\u91CA\u6389\uFF0C\u6570\u636E\u6E90\u4F7F\u7528\u9ED8\u8BA4\u7684\uFF08com.zaxxer.hikari.HikariDataSource\uFF09
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

 

#\u63A7\u5236\u53F0\u6253\u5370mybtics\u7684sql\u8BED\u53E5
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>

 

posted @ 2020-09-07 18:06  我是一只老白兔  阅读(468)  评论(0编辑  收藏  举报