sql
CREATE TABLE STUDENT (
SNO VARCHAR(3) NOT NULL ,
SNAME VARCHAR(9) NOT NULL ,
SSEX CHAR(2) NOT NULL
);
INSERT INTO STUDENT VALUES ('001', 'KangKang', 'M ');
INSERT INTO STUDENT VALUES ('002', 'Mike', 'M ');
INSERT INTO STUDENT VALUES ('003', 'Jane', 'F ');
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--數據庫-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.6</version>
</dependency>
application.yml
# Spring \u76F8\u5173
spring:
redis:
password: 123456
host: 127.0.0.1
port: 9001
timeout: 5000
# 數據源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
type: com.zaxxer.hikari.HikariDataSource
hikari:
connection-timeout: 80000
maximum-pool-size: 5
max-idle: 4
min-idle: 2
initial-size: 2
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
time-between-eviction-runs-millis: 18800
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
url: jdbc:mysql://127.0.0.1:3306/txlcn-demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
jackson.:
#日期格式化
date-format: yyyy-MM-dd HH:mm:ss
serialization:
#格式化輸出
indent_output: true
#忽略無法轉換的對象
fail_on_empty_beans: false
#設置空如何序列化
defaultPropertyInclusion: NON_EMPTY
deserialization:
#允許對象忽略json中不存在的屬性
fail_on_unknown_properties: false
parser:
#允許出現特殊字符和轉義符
allow_unquoted_control_chars: true
#允許出現單引號
allow_single_quotes: true
# mybaits-plus配置
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
global-config:
banner: false
db-config:
table-underline: true
logic-delete-value: 1 # 邏輯已刪除值(默認爲 1)
logic-not-delete-value: 0 # 邏輯未刪除值(默認爲 0)
id-type: auto
configuration:
map-underscore-to-camel-case: true
mapper:
package com.springboot.mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;
import com.springboot.bean.Student;
@Component
@Mapper
public interface StudentMapper {
@Insert("insert into student(sno,sname,ssex) values(#{sno},#{name},#{sex})")
int add(Student student);
@Update("update student set sname=#{name},ssex=#{sex} where sno=#{sno}")
int update(Student student);
@Delete("delete from student where sno=#{sno}")
int deleteBysno(String sno);
@Select("select * from student where sno=#{sno}")
@Results(id = "student",value= {
@Result(property = "sno", column = "sno", javaType = String.class),
@Result(property = "name", column = "sname", javaType = String.class),
@Result(property = "sex", column = "ssex", javaType = String.class)
})
Student queryStudentBySno(String sno);
}
service
package com.springboot.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.bean.Student;
import com.springboot.mapper.StudentMapper;
@Service("studentService")
public class StudentServiceImp{
@Autowired
private StudentMapper studentMapper;
public int add(Student student) {
return this.studentMapper.add(student);
}
public int update(Student student) {
return this.studentMapper.update(student);
}
public int deleteBysno(String sno) {
return this.studentMapper.deleteBysno(sno);
}
public Student queryStudentBySno(String sno) {
return this.studentMapper.queryStudentBySno(sno);
}
}
controller
package com.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.bean.Student;
import com.springboot.service.StudentService;
@RestController
public class TestController {
@Autowired
private StudentService studentService;
@RequestMapping( value = "/querystudent", method = RequestMethod.GET)
public Student queryStudentBySno(String sno) {
return this.studentService.queryStudentBySno(sno);
}
}
student
package com.springboot.bean;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = -339516038496531943L;
private String sno;
private String name;
private String sex;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
訪問
http://localhost:8080/web/druid/
輸入用戶名 密碼 druid druid123
進入首頁
輸入
http://localhost:8080/web/querystudent?sno=001
查看sql執行情況