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进行条件查询、更新以及复杂查询操作。本实验要求利用本章所学知识完成一个学生信息系统,该系统要求实现3个以下功能: 1、多条件查询: 当用户输入的学生姓名不为空,则根据学生姓名进行学生信息的查询; 当用户…...

【爬虫开发】爬虫从0到1全知识md笔记第5篇:Selenium课程概要,selenium的其它使用方法【附代码文档】
爬虫开发从0到1全知识教程完整教程(附代码资料)主要内容讲述:爬虫课程概要,爬虫基础爬虫概述,,http协议复习。requests模块,requests模块1. requests模块介绍,2. response响应对象,3. requests模块发送请求,4. request…...
【我的代码生成器】React的FrmUser类源码
FrmUser 类的源码中:FrmUser btnSaveClick 等命名方式都是参考VB.Net的写法。 import React, { forwardRef, useImperativeHandle, useState, useEffect, } from "react"; import { makeStyles, TextField, Grid, Paper, Button, ButtonGroup, } from &q…...
Flutter 单例模式的多种实现方法与使用场景分析
单例模式是一种常用的设计模式,用于确保一个类只有一个实例,并提供一个全局访问点。在Flutter应用程序中,单例模式可以有效地管理全局状态、资源共享和对象的生命周期。本文将介绍Flutter中实现单例模式的多种方法,并分析它们的使…...

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

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

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

Weblogic任意文件上传漏洞(CVE-2018-2894)漏洞复现(基于vulhub)
🍬 博主介绍👨🎓 博主介绍:大家好,我是 hacker-routing ,很高兴认识大家~ ✨主攻领域:【渗透领域】【应急响应】 【Java、PHP】 【VulnHub靶场复现】【面试分析】 🎉点赞➕评论➕收…...

链表基础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个小时,只是在数据库中查看显示时间不对,但是在页面,又是正常显示中国时区的时间。 排查 项目中数据库的驱动使用的是8.0.19,驱动类使用的是com.mysq…...

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

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

纯css实现switch开关
代码比较简单,有需要直接在下边粘贴使用吧~ html: <div class"switch-box"><input id"switch" type"checkbox"><label></label></div> css: .switch-box {position: relative;height: 25px…...
Unity3d 微信小游戏 AB资源问题
简介 最近在做微信小游戏,因为对unity比较熟悉,而且微信也支持了用unity3d直接导出到小游戏的工具,所以就记录下这期间遇到的问题 微信小游戏启动时间主要受以下三点影响: 下载小游戏首包数据文件下载和编译wasm代码引擎初始化…...

Leetcode二叉树刷题
给你一个二叉树的根节点 root , 检查它是否轴对称。 示例 1: 输入:root [1,2,2,3,4,4,3] 输出:true public boolean isSymmetric(TreeNode root) {if(rootnull)return true;return compare(root.left,root.right);}public boole…...
如何给自己的网站添加 https ssl 证书
文章目录 一、简介二、申请 ssl 证书三、下载 ssl 证书四、配置 nginx五、开放 443 端口六、常见问题解决(一)、配置后,访问 https 无法连接成功(二) 证书配置成功,但是访问 https 还是报不安全 总结参考资料 一、简介 相信大家都知道 https 是更加安全…...
Vue路由跳转及路由传参
跳转 跳转使用 router vue 的路由跳转有 3 个方法: go 、 push 、 replace go :接收数字, 0 刷新,正数前进,负数后退 push :添加,向页面栈中添加一条记录,可以后退 replace &#…...

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

时隔一年,再次讨论下AutoGPT-安装篇
AutoGPT是23年3月份推出的,距今已经1年多的时间了。刚推出时,我们还只能通过命令行使用AutoGPT的能力,但现在,我们不仅可以基于AutoGPT创建自己的Agent,我们还可以通过Web页面与我们创建的Agent进行聊天。这次的AutoGP…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)
HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...
java_网络服务相关_gateway_nacos_feign区别联系
1. spring-cloud-starter-gateway 作用:作为微服务架构的网关,统一入口,处理所有外部请求。 核心能力: 路由转发(基于路径、服务名等)过滤器(鉴权、限流、日志、Header 处理)支持负…...

(十)学生端搭建
本次旨在将之前的已完成的部分功能进行拼装到学生端,同时完善学生端的构建。本次工作主要包括: 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...
R语言AI模型部署方案:精准离线运行详解
R语言AI模型部署方案:精准离线运行详解 一、项目概述 本文将构建一个完整的R语言AI部署解决方案,实现鸢尾花分类模型的训练、保存、离线部署和预测功能。核心特点: 100%离线运行能力自包含环境依赖生产级错误处理跨平台兼容性模型版本管理# 文件结构说明 Iris_AI_Deployme…...
linux 错误码总结
1,错误码的概念与作用 在Linux系统中,错误码是系统调用或库函数在执行失败时返回的特定数值,用于指示具体的错误类型。这些错误码通过全局变量errno来存储和传递,errno由操作系统维护,保存最近一次发生的错误信息。值得注意的是,errno的值在每次系统调用或函数调用失败时…...

NLP学习路线图(二十三):长短期记忆网络(LSTM)
在自然语言处理(NLP)领域,我们时刻面临着处理序列数据的核心挑战。无论是理解句子的结构、分析文本的情感,还是实现语言的翻译,都需要模型能够捕捉词语之间依时序产生的复杂依赖关系。传统的神经网络结构在处理这种序列依赖时显得力不从心,而循环神经网络(RNN) 曾被视为…...
在鸿蒙HarmonyOS 5中使用DevEco Studio实现录音机应用
1. 项目配置与权限设置 1.1 配置module.json5 {"module": {"requestPermissions": [{"name": "ohos.permission.MICROPHONE","reason": "录音需要麦克风权限"},{"name": "ohos.permission.WRITE…...
力扣-35.搜索插入位置
题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

算法笔记2
1.字符串拼接最好用StringBuilder,不用String 2.创建List<>类型的数组并创建内存 List arr[] new ArrayList[26]; Arrays.setAll(arr, i -> new ArrayList<>()); 3.去掉首尾空格...

人工智能(大型语言模型 LLMs)对不同学科的影响以及由此产生的新学习方式
今天是关于AI如何在教学中增强学生的学习体验,我把重要信息标红了。人文学科的价值被低估了 ⬇️ 转型与必要性 人工智能正在深刻地改变教育,这并非炒作,而是已经发生的巨大变革。教育机构和教育者不能忽视它,试图简单地禁止学生使…...