简单的Java Spring Boot:基于Spring Boot,mybatis 实现学生的增删改查,数据库使用mysql,创建student表,字段包括id,name,birthday,编写增删改查接口,测试使用postman,要求学生名可以模糊查询。
1.新建项目
2. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
3. application.yml 配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
username: root
password: 7477
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
4. 数据库建表语句
create table student(
id int primary key not null,
name varchar(32),
birthday varchar(32))
5. 新建对应实体类
使用@Data 注解,来自 lombok。 可以省去 写get、set方法
6. 建立mapper接口
注意 @Mapper 注释
// 查询所有学生
@Select("select * from student")
List<Student> getStudents();
// 通过姓名模糊查询学生
@Select ("select * from student where name like concat('%',#{name},'%')")
List<Student> findStudentByName(String name);
// 删除
@Delete("delete from student where id = #{id}")
void delStudent(int id);
// 新增
@Insert("insert into student(id,name,birthday) values(#{id},#{name},#{birthday})")
void addStudent(Student student);
// 修改(更新)
@Update("update student set name = #{name},birthday = #{birthday} where id = #{id} ")
void updateStudent(Student student);
7. Service 业务层
由于简单所以不写实现类了,直接调用Mapper
注意@Service注解
@Resource
private StudentMapper studentMapper;
// 查询
public List<Student> getStudents(){
return studentMapper.getStudents();
}
// 删除
public void delStudent(int id){
studentMapper.delStudent(id);
}
// 修改
public void updateStudent(Student student){
studentMapper.updateStudent(student);
}
// 新增
public void addStudent(Student student){
studentMapper.addStudent(student);
}
// 模糊查询
public List<Student> findStudentByName(String name){
return studentMapper.findStudentByName(name);
}
8. Controller 控制层
@RestController 注解
@RequestMapping(value = "/student") 加不加都可以,postman测试时,地址要对
@Resource
private StudentService studentService;
@RequestMapping(value = "/addStudent")
public String addStudent(@RequestBody Student student){
try {
studentService.addStudent(student);
return "添加成功";
} catch (Exception e) {
e.printStackTrace();
return "添加失败";
}
}
@RequestMapping(value = "/updateStudent")
@ResponseBody
public String updateStudent(@RequestBody Student student){
try {
studentService.updateStudent(student);
return "更新成功";
} catch (Exception e) {
e.printStackTrace();
return "更新失败";
}
}
@RequestMapping(value = "/delStudent")
@ResponseBody
public String delStudent(int id){
try {
studentService.delStudent(id);
return "删除成功";
} catch (Exception e) {
e.printStackTrace();
return "删除失败";
}
}
@RequestMapping(value = "/getStudents")
@ResponseBody
public List<Student> getStudents(){
try {
List<Student> students = studentService.getStudents();
return students;
} catch (Exception e) {
e.printStackTrace();
return Collections.emptyList();
}
}
@RequestMapping(value = "/findStudentByName")
@ResponseBody
public List<Student> findStudentByName(String name){
try {
List<Student> students = studentService.findStudentByName(name);
return students;
} catch (Exception e) {
e.printStackTrace();
return Collections.emptyList();
}
}
9. 启动类
添加注解 一个用于数据库,一个用与Mapper 我启动时报错,所以添加了这两个,忘记为什么了,有需求可以继续百度一下,
10. postman测试
更新
新增
查询所有
删除
模糊查询