【MyBatis】学生表格操作
创始人
2025-05-30 06:53:16
0次

目录

文件目录

Maven依赖

配置application.properties

StudentMapper.java

Student.java

stu-mapper.xml

HomeworkApplicationTests.java 

运行结果


学生表:字段包括,id、学生学号、姓名、班级、生日日期,其中id为主键

请按照要求完成以下任务。

1) 编写实体类;

2) 编写接口;

接口中包括如下相关方法:

public void insertStudent(Student s);

public void deleteStudent(int stuId);

public void updateStudent(Student s);

public Student selectStudentById(int stuId);

public List selectStudent();//返回所有的学生信息

public int getCount();//返回记录条数

public Integer selectStudentMaxId ();//获取最大的id

public List selectStudent2(String startDate,String endDate);//查询生日指定日期范围的学生记录

public List selectStudentByCondition(Student s);//判断传入的属性,编写查询条件,用if

public List queryByInList(List ids);//编写foreach查询条件的功能ids为id列表

public void updateStudentSet(Student s);//用set来确定更新部分字段

public void updateStudentTrim(Student s);//用trim来确定更新部分字段

public void updateStudentWhere(Student s);//用where来确定更新部分字段

3)编写测试代码。


文件目录

Maven依赖

org.springframework.bootspring-boot-starter-aoporg.springframework.bootspring-boot-starter-testtestorg.mybatis.spring.bootmybatis-spring-boot-starter3.0.1org.springframework.bootspring-boot-devtoolsruntimetruemysqlmysql-connector-java8.0.32junitjunit4.13.2test

 配置application.properties

# DataSourceProperties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/javaee?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
spring.datasource.username=root
spring.datasource.password=123456# MybatisProperties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.school.homework.entity
mybatis.configuration.useGeneratedKeys=true
mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.use-actual-param-name=true# logger
logging.level.com.school.homework=debug

StudentMapper.java

@Mapper
public interface StudentMapper {public void insertStudent(Student s);public void deleteStudent(int stuId);public void updateStudent(Student s);public Student selectStudentById(int stuId);public List selectStudent();//返回所有的学生信息public int getCount();//返回记录条数public Integer selectStudentMaxId ();//获取最大的idpublic List selectStudent2(String startDate,String endDate);//查询生日指定日期范围的学生记录public List selectStudentByCondition(Student s);//判断传入的属性,编写查询条件,用ifpublic List queryByInList(List ids);//编写foreach查询条件的功能ids为id列表public void updateStudentSet(Student s);//用set来确定更新部分字段public void updateStudentTrim(Student s);//用trim来确定更新部分字段public void updateStudentWhere(Student s);//用where来确定更新部分字段
}

Student.java

public class Student {private int id;private int stuId;private String name;private String classname;private Date birthday;@Overridepublic String toString() {return "Student{" +"id=" + id +", stuId=" + stuId +", name='" + name + '\'' +", classname='" + classname + '\'' +", birthday=" + birthday +'}';}public Student(){}public Student(int id,int stuId,String name,String classname,Date birthday){super();this.id=id;this.stuId=stuId;this.name=name;this.birthday=birthday;this.classname=classname;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getStuId() {return stuId;}public void setStuId(int stuId) {this.stuId = stuId;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getClassname() {return classname;}public void setClassname(String classname) {this.classname = classname;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}
}

stu-mapper.xml



id,stu_id,name,classname,birthdaystu_id,name,classname,birthdayinsert into student ()values (#{stuId},#{name},#{classname},#{birthday})update student set stu_id=#{stuId},name=#{name},classname=#{classname},birthday=#{birthday} where id =#{id}update studentstu_id=#{stuId},name=#{name},classname=#{classname},birthday=#{birthday}where id =#{id}update studentstu_id=#{stuId},name=#{name},classname=#{classname},birthday=#{birthday}update studentname=#{name},classname=#{classname},and id=#{id}stu_id=#{stuId}and birthday=#{birthday}delete from student where stu_id=#{stuId}

HomeworkApplicationTests.java 

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = HomeworkApplication.class)
public class HomeworkApplicationTests {@Autowiredprivate StudentMapper studentMapper;@Testpublic void testInsertStudent(){Student s=new Student();s.setStuId(27);s.setBirthday(new Date());s.setClassname("计科201");s.setName("小李同学");studentMapper.insertStudent(s);System.out.println(s);}@Testpublic void testDeleteStudent(){int sStuId=21;studentMapper.deleteStudent(sStuId);System.out.println(studentMapper.selectStudentById(21));}@Testpublic void testUpdateStudent() throws ParseException {Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2002-01-05");Student s=new Student(1,19,"小李同学","计科201",date1);studentMapper.updateStudent(s);}@Testpublic void testSelectStudentById(){Student s=studentMapper.selectStudentById(19);System.out.println(s);}@Testpublic void testSelectStudent(){System.out.println(studentMapper.selectStudent());}@Testpublic void testGetCount(){System.out.println(studentMapper.getCount());}@Testpublic void testSelectStudentMaxId(){System.out.println(studentMapper.selectStudentMaxId());}@Testpublic void testSelectStudent2(){System.out.println(studentMapper.selectStudent2("2002-01-04","2023-03-28"));}@Testpublic void testSelectStudentByCondition() throws ParseException {Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2002-01-03");Student s=new Student(1,19,"","",date1);System.out.println(studentMapper.selectStudentByCondition(s));}@Testpublic void testQueryByInList(){List ids=new ArrayList<>();ids.add(19);//ids.add(23);ids.add(21);List stus=studentMapper.queryByInList(ids);for (Student stu:stus) {System.out.println(stu);}}@Testpublic void testUpdateStudentSet() throws ParseException {Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2002-01-08");Student s=new Student(4,0,"小h同学","医学117",date1);studentMapper.updateStudentSet(s);System.out.println(studentMapper.selectStudentById(s.getStuId()));}@Testpublic void testUpdateStudentTrim() throws ParseException {Student s=new Student(2,21,"王二虎","网络181",new SimpleDateFormat("yyyy-MM-dd").parse("2003-11-05"));studentMapper.updateStudentTrim(s);System.out.println(studentMapper.selectStudentById(s.getStuId()));}@Testpublic void testUpdateStudentWhere(){Student s=new Student(4,0,"王丽","建工138",null);studentMapper.updateStudentWhere(s);System.out.println(studentMapper.selectStudent());}}

运行结果

 

 

相关内容

热门资讯

总算了解开挂!众乐乐开挂AI攻... 总算了解开挂!众乐乐开挂AI攻略,众乐乐多功能都是存在有开挂攻略1、上手简单,内置详细流程视频教学,...
程序员教你辅助!英豪娱乐城辅助... 程序员教你辅助!英豪娱乐城辅助AI工具,英豪娱乐城多功能原来真的是有辅助工具暗藏猫腻,小编详细说明英...
盘点几款辅助!冰球突破辅助AI... 盘点几款辅助!冰球突破辅助AI插件,冰球突破多功能其实真的有辅助插件1、冰球突破脚本辅助下载、冰球突...
分享一款辅助器!建湖好运麻将辅... 分享一款辅助器!建湖好运麻将辅助器AI教程,建湖好运麻将多功能竟然真的是有辅助器教程1、在建湖好运麻...
每日必备透视挂!新老夫子透视挂... 每日必备透视挂!新老夫子透视挂AI技巧,新老夫子多功能原来真的有透视挂技巧1、新老夫子脚本辅助下载、...
分享认知作弊器!星际互联作弊器... 分享认知作弊器!星际互联作弊器AI方法,星际互联多功能都是存在有作弊器方法1、每一步都需要思考,不同...
玩家必备攻略作弊器!闲逸斗地主... 玩家必备攻略作弊器!闲逸斗地主作弊器AI方法,闲逸斗地主多功能果然是有作弊器方法1、闲逸斗地主透视辅...
分辨真假透视挂!宁波游戏大厅透... 分辨真假透视挂!宁波游戏大厅透视挂AI软件,宁波游戏大厅多功能一直是有透视挂软件1、让任何用户在无需...
每日必看教程透视挂!君悦来透视... 每日必看教程透视挂!君悦来透视挂AI工具,君悦来多功能本来是有透视挂工具1、上手简单,内置详细流程视...
发现一款外挂!半岛外挂AI工具... 发现一款外挂!半岛外挂AI工具,半岛多功能一贯真的有外挂工具1、起透看视 半岛辅助软件价格2、随意选...