标签:source 存在 result content control values img 推荐 row
废话少说,上代码
结构:
1application.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
#通过触发器,来控制什么时候进行加载热部署的文件
spring.devtools.restart.trigger-file=trigger.txt
#端口地址
server.port=8080
#文件上传路径
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的基础配置
#Freemarker是否开启thymeleaf缓存,本地为flase,生产改为true
spring.freemarker.cache=false
#Freemarker编码格式
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
#Freemarker类型
spring.freemarker.content-type=text/html; charset=utf-8
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
#Freemarker文件后缀
spring.freemarker.suffix=.ftl
#Freemarker路径
spring.freemarker.template-loader-path=classpath:/templates/
#整合thymeleaf相关配置
#开发时关闭缓存,不然没法看见实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#thymeleaf路径
spring.thymeleaf.prefix=classpath:/templates/tl/
#thymeleaf编码格式
spring.thymeleaf.encoding=UTF-8
#thymeleaf类型
spring.thymeleaf.servlet.content-type=text/html; charset=utf-8
#thymeleaf名称后缀
spring.thymeleaf.suffix=.html
#整合mysql的配置文件
#mysql加载驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#jdbc数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#mysql账号
spring.datasource.username=root
#mysql密码
spring.datasource.password=456789
#数据连接源,如果注释掉,数据源使用默认的(com.zaxxer.hikari.HikariDataSource)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#控制台打印mybtics的sql语句
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2SpringbootMysqlApplication
package springboot_mysql;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("springboot_mysql.mapper")
public class SpringbootMysqlApplication {
public static void main(String[] args)throws Exception {
SpringApplication.run(SpringbootMysqlApplication.class, args); }
}
3Student
package springboot_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();
}
}
4JsonData
package springboot_mysql.bean;
import java.io.Serializable;
import java.util.List;
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 buildSuccess(int id) {
return id;
}
public static Object buildSuccess(List<Student> all) {
return all;
}
public static Object buildSuccess(Student findById) {
return findById;
}
public static Object buildSuccess() {
return null;
}
}
5StudentMapper
package springboot_mysql.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import springboot_mysql.bean.Student;
/**
* 访问数据库的接口
* @author Administrator
*
*/
public interface StudentMapper {
/**
* sql语句中推荐使用#{},而不是${},因为存在sql注入的危险,#{}返回的值是?
* @Insert("insert into student(name,age,sex,phone,money)values(#{name},#{age},#{sex},#{phone},#{money})")是新增的sql的方法
* @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")是获取自增的主键id
* useGeneratedKeys是是否返回值,true返回,flase不返回
* keyColumn对应的是Student类中的属性id
* keyProperty对应的是数据库mysql中的表Student中的字段id
* @param student
* @return
*/
@Insert("insert into student(name,age,sex,phone,money)values(#{name},#{age},#{sex},#{phone},#{money})")
@Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
int insert(Student student);
/**
* 查询student所有的数据
* @return
* @Results({
@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")
})
中column对应的是数据库中的字段,property对应的是Student类中的属性,主要是用来对付属性名称不一致使用,这里可以注释掉
*/
@Select("select * from student")
@Results({
@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")
})
List<Student> findAll();
/**
* 根据id查询student
* @param id
* @return
*/
@Select("select * from student where id=#{id}")
Student findById(int id);
/**
* 根据id修改Student
* @param student
*/
@Update("update student set name=#{name} where id=#{id} ")
void update(Student student);
/**
* 根据id删除Student
* @param id
* @return
*/
@Delete("delete from student where id =#{id} ")
void deleteById(int id);
}
6StudentService
package springboot_mysql.service;
import java.util.List;
import springboot_mysql.bean.Student;
public interface StudentService {
/**
* 新增student的数据接口
*
* @param student
* @return
*/
int add(Student student);
/**
* 查询student的所有数据接口
*
* @return
*/
List<Student> findAll();
/**
* 根据id查询student的数据接口
*
* @param id
* @return
*/
Student findById(int id);
/**
* 根据id删除student的数据接口
*
* @param id
* @return
*/
void deleteById(int id);
/**
* 根据student对象数据进行修改的接口
*
* @param student
*/
void update(Student student);
}
7StudentServiceImpl
package springboot_mysql.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot_mysql.bean.Student;
import springboot_mysql.mapper.StudentMapper;
import springboot_mysql.service.StudentService;
/**
* @Service 这个注解能够让controller扫描StudentServiceImpl
* @author Administrator
*
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentmapper;
/**
* 新增student的接口实现
*/
@Override
public int add(Student student) {
return studentmapper.insert(student);
}
@Override
public List<Student> findAll() {
return studentmapper.findAll();
}
@Override
public Student findById(int id) {
return studentmapper.findById(id);
}
@Override
public void deleteById(int id) {
studentmapper.deleteById(id);
}
@Override
public void update(Student student) {
studentmapper.update(student);
}
}
8StudentController
package springboot_mysql.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot_mysql.bean.JsonData;
import springboot_mysql.bean.Student;
import springboot_mysql.mapper.StudentMapper;
import springboot_mysql.service.StudentService;
@RestController
@RequestMapping("/api/v1/student")
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private StudentMapper studentMapper;
@GetMapping("add")
public Object add() {
Student student = new Student();
student.setName("李明");
student.setAge(20);
student.setSex("男");
student.setPhone("13662626356");
student.setMoney(1000);
int id = studentService.add(student);
return JsonData.buildSuccess(id);
}
@GetMapping("findAll")
public Object findAll() {
return JsonData.buildSuccess(studentMapper.findAll());
}
@GetMapping("findById")
public Object findById(int id) {
return JsonData.buildSuccess(studentMapper.findById(id));
}
@GetMapping("deleteById")
public Object deleteById(int id) {
studentMapper.deleteById(id);
return JsonData.buildSuccess();
}
@GetMapping("update")
public Object update(String name, int id) {
Student student = new Student();
student.setName(name);
student.setId(id);
studentMapper.update(student);
return JsonData.buildSuccess();
}
}
9run application 通过接口访问Controller中的接口,可以查看数据的增删改查
整合springboot+mybatis+mysql之增删改查(三)
标签:source 存在 result content control values img 推荐 row
原文地址:https://www.cnblogs.com/zhushilai/p/13579462.html