廢話少說,上代碼
結構:
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中的接口,可以查看數據的增刪改查