数据库代码
/*
Navicat MySQL Data Transfer
Source Server : 本机
Source Server Version : 50546
Source Host : localhost:3306
Source Database : springboot
Target Server Type : MYSQL
Target Server Version : 50546
File Encoding : 65001
Date: 2023-10-22 16:15:57
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=865918979 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '张三', '22', '男');
INSERT INTO `student` VALUES ('2', '李四', '32', '男');
INSERT INTO `student` VALUES ('3', '王五', '15', '男');
INSERT INTO `student` VALUES ('4', '樱岛麻衣', '18', '女');
INSERT INTO `student` VALUES ('5', '初音未来', '19', '女');
INSERT INTO `student` VALUES ('865918978', '神里凌华', '17', '女');
0.POM坐标依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot_demo04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo04</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.创建entity实体层
package com.itheima.entity;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
}
2.创建Dao接口层接口,继承BaseMapper<entity>
package com.itheima.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentDao extends BaseMapper<Student> {
//自定义查询方法
@Select("SELECT * FROM `student` where age = #{age};")
List<Student> findByAge(int age);
}
3.创建service接口层,继承IService<entity>
package com.itheima.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.entity.Student;
public interface IStudentService extends IService<Student> {
}
4.创建service接口的实现层,继承ServiceImpl<BaseMapper, entity> 并实现IStudentService接口
package com.itheima.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.StudentDao;
import com.itheima.entity.Student;
import com.itheima.service.IStudentService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudnetServiceImpl extends ServiceImpl<StudentDao, Student> implements IStudentService {
public List<Student> findByAge(int age){
//调用studentDao中的方法
return this.baseMapper.findByAge(age);
}
}
5.编写测试类
package com.itheima;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.dao.StudentDao;
import com.itheima.entity.Student;
import com.itheima.service.IStudentService;
import com.itheima.service.impl.StudnetServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class SpringbootDemo04ApplicationTests {
@Autowired
StudentDao student;
@Autowired
StudnetServiceImpl studentService;
// @Autowired
// Student student1;
@Test
void contextLoads() {
List<Student> students = student.selectList(null);
System.out.println(students);
}
@Test
//查询年龄小于20岁的
void selectAgeLt20() {
LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<Student>();
lqw.lt(Student::getAge, 20);
List<Student> students = student.selectList(lqw);
System.out.println(students);
}
@Test
//查询年龄大于30岁的
void selectAgeGt30(){
LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<Student>();
lqw.gt(Student::getAge,30);
System.out.println(student.selectList(lqw));
}
@Test
//查询年龄在20-30岁的
void selectAge20_30(){
LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<Student>();
lqw.lt(Student::getAge,30).gt(Student::getAge,20);
System.out.println(student.selectList(lqw));
}
//查询名字中带三的人
@Test
void selectLike(){
LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
lqw.like(Student::getName,'三');
System.out.println(student.selectList(lqw));
}
@Test
void selectTest(){
List<Student> list = studentService.list();
System.out.println(list);
}
@Test
void saveTest(){
Student student = new Student();
student.setName("神里凌华");
student.setAge(17);
student.setSex("女");
studentService.saveOrUpdate(student);
}
@Test
void selectByAge(){
List<Student> list = studentService.findByAge(19);
System.out.println(list);
}
}