1.创建学生信息表
create table stu(id int unsigned primary key auto_increment comment 'ID',name varchar(100) comment '姓名',age tinyint unsigned comment '年龄',gender tinyint unsigned comment '性别, 1:男, 2:女',score double(5,2) comment '成绩',phone varchar(11) comment '手机号'
)comment '学生表';
ALTER TABLE stu CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
insert into stu(id, name, age, gender,score, phone) VALUES (null,'张三',18,'1',560,'18800000000');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'李四',19,'1',572,'18800000001');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'王五',20,'1',480,'18800000002');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'赵六',18,'2',621,'18800000003');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'黄七',17,'2',211,'18800000004');
insert into stu(id, name, age, gender,score, phone) VALUES (null,'孙八',18,'1',492,'18800000005');
2.创建项目工程
2.appliction.yml
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisusername: rootpassword: 1234
3.StuController
package com.itdanb.springboottest.controller;import com.itdanb.springboottest.pojo.Stu;
import com.itdanb.springboottest.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class StuController {@Autowiredprivate StuService stuService;@RequestMapping("/findById")public Stu findById(Integer id){return stuService.findById(id);}
}
Stumapper
package com.itdanb.springboottest.mapper;import com.itdanb.springboottest.pojo.Stu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;@Mapper
public interface StuMapper {@Select("select * from stu where id = #{id}")public Stu findById(Integer id);}
Stu
package com.itdanb.springboottest.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Stu {private Integer id;private String name;private Integer age;private Short gender;private String score;private String phone;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Short getGender() {return gender;}public void setGender(Short gender) {this.gender = gender;}public String getScore() {return score;}public void setScore(String score) {this.score = score;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "Stu{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", score='" + score + '\'' +", phone='" + phone + '\'' +'}';}
}
StuService
package com.itdanb.springboottest.service;import com.itdanb.springboottest.pojo.Stu;public interface StuService {public Stu findById(Integer id);
}
StuServiceImpl
package com.itdanb.springboottest.service.impl;import com.itdanb.springboottest.pojo.Stu;
import com.itdanb.springboottest.mapper.StuMapper;
import com.itdanb.springboottest.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class StuServiceImpl implements StuService {@Autowiredprivate StuMapper stuMapper;@Overridepublic Stu findById(Integer id) {return stuMapper.findById(id);}
}