当前位置: 首页 > news >正文

JavaEE实验三:3.5学生信息查询系统(动态Sql)

题目要求:

使用动态SQL进行条件查询、更新以及复杂查询操作。本实验要求利用本章所学知识完成一个学生信息系统,该系统要求实现3个以下功能:
1、多条件查询: 当用户输入的学生姓名不为空,则根据学生姓名进行学生信息的查询; 当用户输入的学生姓名为空而学生专业不为空,则只根据学生专业进行学生的查询;当学生姓名和专业都为空,则查询所有学生信息
2、单条件查询:查询出所有id值小于5的学生的信息;

实验步骤:

先创建一个数据库 user 表:

CREATE TABLE user(id int(32) PRIMARY KEY AUTO_INCREMENT,name varchar(50),major varchar(50),userId varchar(16)
);

再插入数据:

# 插入7条数据
INSERT INTO user VALUES ('1', '张三', 'spring', '202101');
INSERT INTO user VALUES ('2', '李四', 'mybatis', '202102');
INSERT INTO user VALUES ('3', '王二', 'reids', '202103');
INSERT INTO user VALUES ('4', '小张', 'springMVC', '202104');
INSERT INTO user VALUES ('5', '小红', 'springBoot', '202105');
INSERT INTO user VALUES ('6', '小王', 'springcloud', '202106');
INSERT INTO user VALUES ('7', '小芬', 'vue', '202107');

1.创建maven项目,在pom.xml文件中配置以依赖

2.创建实体类StudentEntity

3.创建jdbc.properties和mybatis-config.xml配置文件

4.创建StudentMapper接口

5.在mybatis-config.xml文件中注册StudentMapper.xml

6.创建测试类

7.工具类

8.测试结果

项目结构:

1.创建maven项目,在pom.xml文件中配置以依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Example</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource></resources></build>
</project>

2.创建实体类StudentEntity

package com.gcy.entity;public class StudentEntity {private Integer id;private String name;private String major;private String sno;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 String getMajor() {return major;}public void setMajor(String major) {this.major = major;}public String getSno() {return sno;}public void setSno(String sno) {this.sno = sno;}@Overridepublic String toString() {return "StudentEntity{" +"id=" + id +", name='" + name + '\'' +", major='" + major + '\'' +", sno='" + sno + '\'' +'}';}
}

3.创建jdbc.properties和mybatis-config.xml配置文件

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
jdbc.username=root
jdbc.password=200381

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 环境配置 --><!-- 加载类路径下的属性文件 --><properties resource="jdbc.properties"/><environments default="development"><environment id="development"><transactionManager type="JDBC"/><!-- 数据库连接相关配置 ,db.properties文件中的内容--><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></dataSource></environment></environments><mappers><mapper resource="com/gcy/mapper/StudentMaper.xml"/></mappers>
</configuration>

4.创建StudentMapper接口

package com.gcy.mapper;
import com.gcy.entity.StudentEntity;
import java.util.List;
public interface StudentMapper {List<StudentEntity> findStudentByName(StudentEntity  student);List<StudentEntity> findStudentById(Integer[] array);List<StudentEntity> findAllStudent(StudentEntity  student);List<StudentEntity> findStudentByNameOrMajor(StudentEntity  student);
}

5.在mybatis-config.xml文件中注册StudentMapper.xml

<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gcy.mapper.StudentMapper"><select id="findStudentByName" parameterType="com.gcy.entity.StudentEntity"resultType="com.gcy.entity.StudentEntity">select * from user where 1=1<if test="name!=null and name!=''">and name like concat('%',#{name},'%')</if></select><select id="findStudentByNameOrMajor" parameterType="com.gcy.entity.StudentEntity"resultType="com.gcy.entity.StudentEntity">select * from user<where><choose><when test="name !=null and name !=''">and name like concat('%',#{name}, '%')</when><when test="major !=null and major !=''">and major= #{major}</when></choose></where></select><select id="findAllStudent" parameterType="com.gcy.entity.StudentEntity"resultType="com.gcy.entity.StudentEntity">select * from user<where><choose><when test="name !=null and name !=''">and name like concat('%',#{name}, '%')</when><when test="major !=null and major !=''">and major= #{major}</when><otherwise>and id is not null</otherwise></choose></where></select><select id="findStudentById" parameterType="java.util.Arrays"resultType="com.gcy.entity.StudentEntity">select * from user<where><foreach item="id" index="index" collection="array"open="id in(" separator="," close=")">#{id}</foreach></where></select>
</mapper>

6.工具类

package com.gcy.utils;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/*** 工具类*/
public class MyBatisUtils {private static SqlSessionFactory sqlSessionFactory;// 初始化SqlSessionFactory对象static {try {// 使用MyBatis提供的Resources类加载MyBatis的配置文件Reader reader =Resources.getResourceAsReader("mybatis-config.xml");// 构建SqlSessionFactory工厂sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);} catch (Exception e) {e.printStackTrace();}}// 获取SqlSession对象的静态方法public static SqlSession getSession() {return sqlSessionFactory.openSession();}
}

7.创建测试类

import com.gcy.entity.StudentEntity;
import com.gcy.mapper.StudentMapper;
import com.gcy.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;import java.util.List;public class Test {@org.junit.Testpublic void  Test01(){SqlSession session = MyBatisUtils.getSession();StudentMapper mapper = session.getMapper(StudentMapper.class);StudentEntity student = new StudentEntity();student.setName("张三");List<StudentEntity> findStudentByName = mapper.findStudentByName(student);System.out.println("*************************  姓名不为空  *******************");for (StudentEntity s : findStudentByName) {System.out.println(s);}session.close();}@org.junit.Testpublic void Test02(){SqlSession session = MyBatisUtils.getSession();StudentMapper mapper = session.getMapper(StudentMapper.class);StudentEntity student = new StudentEntity();student.setMajor("spring");List<StudentEntity> studentByNameOrMajor = mapper.findStudentByNameOrMajor(student);System.out.println("*************************  专业不为空  *******************");for (StudentEntity s : studentByNameOrMajor) {System.out.println(s);}session.close();}@org.junit.Testpublic void Test03(){SqlSession session = MyBatisUtils.getSession();StudentMapper mapper = session.getMapper(StudentMapper.class);StudentEntity student = new StudentEntity();List<StudentEntity> allStudent = mapper.findAllStudent(student);System.out.println("*************************  学号不为空  *******************");for (StudentEntity s : allStudent) {System.out.println(s);}session.close();}@org.junit.Testpublic void Test04(){SqlSession session = MyBatisUtils.getSession();StudentMapper mapper = session.getMapper(StudentMapper.class);Integer[] strId = {1,2,3,4};List<StudentEntity> studentById = mapper.findStudentById(strId);System.out.println("*************************  前面4位  *******************");for (StudentEntity s : studentById) {System.out.println(s);}}
}

8.测试结果

相关文章:

JavaEE实验三:3.5学生信息查询系统(动态Sql)

题目要求: 使用动态SQL进行条件查询、更新以及复杂查询操作。本实验要求利用本章所学知识完成一个学生信息系统&#xff0c;该系统要求实现3个以下功能: 1、多条件查询&#xff1a; 当用户输入的学生姓名不为空&#xff0c;则根据学生姓名进行学生信息的查询&#xff1b; 当用户…...

【爬虫开发】爬虫从0到1全知识md笔记第5篇:Selenium课程概要,selenium的其它使用方法【附代码文档】

爬虫开发从0到1全知识教程完整教程&#xff08;附代码资料&#xff09;主要内容讲述&#xff1a;爬虫课程概要&#xff0c;爬虫基础爬虫概述,,http协议复习。requests模块&#xff0c;requests模块1. requests模块介绍,2. response响应对象,3. requests模块发送请求,4. request…...

【我的代码生成器】React的FrmUser类源码

FrmUser 类的源码中&#xff1a;FrmUser btnSaveClick 等命名方式都是参考VB.Net的写法。 import React, { forwardRef, useImperativeHandle, useState, useEffect, } from "react"; import { makeStyles, TextField, Grid, Paper, Button, ButtonGroup, } from &q…...

Flutter 单例模式的多种实现方法与使用场景分析

单例模式是一种常用的设计模式&#xff0c;用于确保一个类只有一个实例&#xff0c;并提供一个全局访问点。在Flutter应用程序中&#xff0c;单例模式可以有效地管理全局状态、资源共享和对象的生命周期。本文将介绍Flutter中实现单例模式的多种方法&#xff0c;并分析它们的使…...

C语言洛谷题目分享(9)奇怪的电梯

目录 1.前言 2.题目&#xff1a;奇怪的电梯 1.题目描述 2.输入格式 3.输出格式 4.输入输出样例 5.说明 6.题解 3.小结 1.前言 哈喽大家好啊&#xff0c;前一段时间小编去备战蓝桥杯所以博客的更新就暂停了几天&#xff0c;今天继续为大家带来题解分享&#xff0c;希望大…...

vue 中使 date/time/datetime 类型的 input 支持 placeholder 方法

一般在开发时&#xff0c;设置了 date/time/datetime 等类型的 input 属性 placeholder 提示文本时&#xff0c; 发现实际展示中却并不生效&#xff0c;如图&#xff1a; 处理后效果如图&#xff1a; 处理逻辑 判断表单项未设置值时&#xff0c;则设置其伪类样式&#xff0c;文…...

书生·浦语大模型全链路开源体系-第3课

书生浦语大模型全链路开源体系-第3课 书生浦语大模型全链路开源体系-第3课相关资源RAG 概述在 InternLM Studio 上部署茴香豆技术助手环境配置配置基础环境下载基础文件下载安装茴香豆 使用茴香豆搭建 RAG 助手修改配置文件 创建知识库运行茴香豆知识助手 在茴香豆 Web 版中创建…...

Weblogic任意文件上传漏洞(CVE-2018-2894)漏洞复现(基于vulhub)

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【Java、PHP】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收…...

链表基础3——单链表的逆置

链表的定义 #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; Node* createNode(int data) { Node* newNode (Node*)malloc(sizeof(Node)); if (!newNode) { return NULL; } newNode->data …...

Fiddler:网络调试利器

目录 第1章:Fiddler简介和安装 1.1 Fiddler概述 1.2 Fiddler的安装步骤 步骤1:下载Fiddler 步骤2:运行安装程序 步骤3:启动Fiddler 1.3 配置Fiddler代理 配置操作系统代理 配置浏览器代理 Google Chrome Mozilla Firefox 第2章:Fiddler界面和基本操作 2.1 Fi…...

【笔记】mysql版本6以上时区问题

前言 最近在项目中发现数据库某个表的createTime字段的时间比中国时间少了13个小时&#xff0c;只是在数据库中查看显示时间不对&#xff0c;但是在页面&#xff0c;又是正常显示中国时区的时间。 排查 项目中数据库的驱动使用的是8.0.19&#xff0c;驱动类使用的是com.mysq…...

Scala实战:打印九九表

本次实战的目标是使用不同的方法实现打印九九表的功能。我们将通过四种不同的方法来实现这个目标&#xff0c;并在day02子包中创建相应的对象。 方法一&#xff1a;双重循环 我们将使用双重循环来实现九九表的打印。在NineNineTable01对象中&#xff0c;我们使用两个嵌套的fo…...

Excel文件解析

在此模块的学习中&#xff0c;我们需要一个新的开源类库---Apahche POI开源类库。这个类库的用途是&#xff1a;解析并生成Excel文件(Word、ppt)。Apahche POI基于DOM方式进行解析&#xff0c;将文件直接加载到内存&#xff0c;所以速度比较快&#xff0c;适合Excel文件数据量不…...

纯css实现switch开关

代码比较简单&#xff0c;有需要直接在下边粘贴使用吧~ html: <div class"switch-box"><input id"switch" type"checkbox"><label></label></div> css&#xff1a; .switch-box {position: relative;height: 25px…...

Unity3d 微信小游戏 AB资源问题

简介 最近在做微信小游戏&#xff0c;因为对unity比较熟悉&#xff0c;而且微信也支持了用unity3d直接导出到小游戏的工具&#xff0c;所以就记录下这期间遇到的问题 微信小游戏启动时间主要受以下三点影响&#xff1a; 下载小游戏首包数据文件下载和编译wasm代码引擎初始化…...

Leetcode二叉树刷题

给你一个二叉树的根节点 root &#xff0c; 检查它是否轴对称。 示例 1&#xff1a; 输入&#xff1a;root [1,2,2,3,4,4,3] 输出&#xff1a;true public boolean isSymmetric(TreeNode root) {if(rootnull)return true;return compare(root.left,root.right);}public boole…...

如何给自己的网站添加 https ssl 证书

文章目录 一、简介二、申请 ssl 证书三、下载 ssl 证书四、配置 nginx五、开放 443 端口六、常见问题解决(一)、配置后&#xff0c;访问 https 无法连接成功(二) 证书配置成功&#xff0c;但是访问 https 还是报不安全 总结参考资料 一、简介 相信大家都知道 https 是更加安全…...

Vue路由跳转及路由传参

跳转 跳转使用 router vue 的路由跳转有 3 个方法&#xff1a; go 、 push 、 replace go &#xff1a;接收数字&#xff0c; 0 刷新&#xff0c;正数前进&#xff0c;负数后退 push &#xff1a;添加&#xff0c;向页面栈中添加一条记录&#xff0c;可以后退 replace &#…...

计算机网络常见面试总结

文章目录 1. 计算机网络基础1.1 网络分层模型1. OSI 七层模型是什么&#xff1f;每一层的作用是什么&#xff1f;2.TCP/IP 四层模型是什么&#xff1f;每一层的作用是什么&#xff1f;3. 为什么网络要分层&#xff1f; 1.2 常见网络协议1. 应用层有哪些常见的协议&#xff1f;2…...

时隔一年,再次讨论下AutoGPT-安装篇

AutoGPT是23年3月份推出的&#xff0c;距今已经1年多的时间了。刚推出时&#xff0c;我们还只能通过命令行使用AutoGPT的能力&#xff0c;但现在&#xff0c;我们不仅可以基于AutoGPT创建自己的Agent&#xff0c;我们还可以通过Web页面与我们创建的Agent进行聊天。这次的AutoGP…...

无人机协议

1. MAVLink协议 概述&#xff1a;MAVLink是一种轻量级、低带宽的无人机通信协议&#xff0c;它支持点对点、广播和多播通信&#xff0c;并且可以在不同的平台上使用。应用&#xff1a;MAVLink协议广泛应用于PX4、ArduPilot等开源飞控系统中&#xff0c;用于地面站和无人机之间…...

NoFences终极指南:免费开源桌面分区工具彻底解决Windows桌面混乱问题

NoFences终极指南&#xff1a;免费开源桌面分区工具彻底解决Windows桌面混乱问题 【免费下载链接】NoFences &#x1f6a7; Open Source Stardock Fences alternative 项目地址: https://gitcode.com/gh_mirrors/no/NoFences 还在为杂乱的Windows桌面而烦恼吗&#xff1…...

如何用FanControl终极解决Windows风扇噪音与散热难题

如何用FanControl终极解决Windows风扇噪音与散热难题 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_Trending/fa/FanControl.…...

ArcGIS标注进阶:手把手教你搞定分式标注和河流左斜体(附完整表达式)

ArcGIS标注进阶&#xff1a;分式标注与河流左斜体实战指南 在地图制图领域&#xff0c;专业标注是提升可视化效果的关键环节。许多GIS工程师在进行水文地质制图时&#xff0c;常遇到分式标注格式混乱、河流名称无法实现标准左斜体等痛点问题。本文将彻底解决这些标注难题&#…...

AI Agent变现难题与破局之道:小白程序员必备收藏,2026年蓝海掘金指南!

文章深入分析了当前AI Agent行业的冰火两重天现象&#xff0c;揭示了技术不成熟、伪需求泛滥、基础设施不完善等六大核心底层逻辑导致变现困难。同时&#xff0c;文章指出了电商全链路、企业办公自动化、本地生活商家、开发者垂直、垂类定制化等五大变现蓝海赛道&#xff0c;并…...

【日本顶尖名校-日本早稻田大学主办 | ACM出版 | 连续2届稳定检索,见刊后1个月左右即完成EIScopus双检索 | 设海内外双会场】第三届人工智能与未来教育国际学术会议(AIFE 2026)

可靠&#xff01;连续2届稳定检索&#xff0c;见刊后1个月左右即完成EI & Scopus双检索&#xff0c;检索速度快、收录稳定 热门&#xff01;会议聚焦“人工智能教育”这一前沿交叉主题&#xff0c;契合当前学术热点 权威&#xff01;本届会议由日本顶尖名校-日本早稻田大学…...

别再死记硬背了!用Python和NumPy从零实现5大激活函数(附梯度消失/爆炸分析)

用Python和NumPy实战五大激活函数&#xff1a;从公式推导到梯度问题深度解析 在深度学习的世界里&#xff0c;激活函数如同神经元的"开关"&#xff0c;决定了信息能否在网络中流动。很多初学者面对教科书上抽象的数学公式时&#xff0c;常常陷入死记硬背的困境。本文…...

基于视觉大模型的桌面自动化:Screen Vision技能实现AI操控电脑

1. 项目概述&#xff1a;让AI成为你的“数字双手” 你有没有想过&#xff0c;有一天你可以像指挥一个真人助手一样&#xff0c;用自然语言告诉AI&#xff1a;“帮我把桌面上的那个PDF文件拖到‘已处理’文件夹里”&#xff0c;或者“打开浏览器&#xff0c;搜索一下今天北京的…...

观测Taotoken在每日大赛期间API调用的延迟与稳定性表现

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 观测Taotoken在每日大赛期间API调用的延迟与稳定性表现 在技术社区组织的每日编程挑战赛期间&#xff0c;开发者们通常会集中、高频…...

颠覆性网络拓扑可视化:基于Vue+SVG的一站式轻量级解决方案

颠覆性网络拓扑可视化&#xff1a;基于VueSVG的一站式轻量级解决方案 【免费下载链接】easy-topo vuesvgelement-ui 快捷画出网络拓扑图 项目地址: https://gitcode.com/gh_mirrors/ea/easy-topo 在复杂的网络架构设计和运维管理中&#xff0c;网络工程师和开发人员经常…...