MyBatis——学生信息查询系统
实验要求
本实验要求根据表1在数据库中创建一个t_student表,并利用动态SQL进行条件查询、更新和复杂查询操作的相关知识完成一个学生信息查询系统。
实验内容
| 学生编号(id) | 学生姓名(sname) | 学生学号(sno) | 学生专业(smajor) |
|---|---|---|---|
| 1 | 张三 | 001 | 电气工程及自动化 |
| 2 | 李四 | 002 | 自动化 |
| 3 | 王五 | 003 | 测控 |
| 4 | 悟空 | 999 | 降妖除魔 |
| 5 | Hades | 985 | 计算机科学与技术 |
该学生信息查询系统需要实现以下几个功能:
实验分析
本实验主要考查对MyBatis的动态SQL的掌握。
代码实现
db.properties(数据库连接配置文件)
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&\ characterEncoding=utf8&useUnicode=true&useSSL=false
username=root
password=1
mybatis-config.xml(MyBatis的核心配置文件)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 环境配置 -->
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"> </properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置,db.properties文件中的内容 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
StudentMapper.xml(映射文件)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cqust.pojo.Student">
<select id="findStudentBySnameAndSmajor" parameterType="com.cqust.pojo.Student" resultType="com.cqust.pojo.Student">
select * from t_student where 1=1
<choose>
<when test="sname !=null and sname !='' ">
and sname like concat ('%',#{sname},'%')
</when>
<when test="smajor !=null and smajor !='' ">
and smajor like concat ('%',#{smajor},'%')
</when>
<otherwise>
and sno is not null
</otherwise>
</choose>
</select>
<select id="findStudentById" parameterType="java.util.Arrays" resultType="com.cqust.pojo.Student">
select * from t_student where id in
<foreach collection="list" item="id" index="index"
open="(" separator="," close=")">
#{id}
</foreach>
</select>
</mapper>
Student类
package com.cqust.pojo;
public class Student {
private int id; //学生id
private String sname; //学生姓名
private String sno; //学生学号
private String smajor; //学生专业
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSmajor() {
return smajor;
}
public void setSmajor(String smajor) {
this.smajor = smajor;
}
@Override
public String toString() {
return "Student{" +
"学生id=" + id +
", 学生姓名='" + sname + '\'' +
", 学号='" + sno + '\'' +
", 专业='" + smajor + '\'' +
'}';
}
}
MyBatisUtils类
package com.cqust.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.Reader;
/**
* 工具类
*/
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
//初始化SQLSessionFactory对象
static {
try{
//使用MyBatis提供的Resource类加载MyBatis的配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//构建SQLSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e){
e.printStackTrace();
}
}
//获取SqlSession对象的方法
public static SqlSession getSession(){
//若传入true表示关闭事务控制,自动提交;false表示开启事务控制
return sqlSessionFactory.openSession(true);
}
}
Test1(测试类)
import com.cqust.pojo.Student;
import com.cqust.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
@Test
public void findStudentBySnameAndSmajor(){
//获取SqlSession
SqlSession session = MyBatisUtils.getSession();
//创建Student对象,并向对象中添加数据
Student student = new Student();
student.setSname("Hades"); //设置学生姓名
student.setSmajor("计算机科学与技术"); //设置学生专业
//执行SqlSession的查询方法,返回结果集
List<Student> students = session.selectList("findStudentBySnameAndSmajor",student);
//输出查询结果信息
for(Student Student1 : students){
//输出结果信息
System.out.println(Student1);
}
//关闭SqlSession
session.close();
}
@Test
public void findStudentByListTest(){
//获取SqlSession
SqlSession session = MyBatisUtils.getSession();
//创建List集合,封装查询id
List<Integer> list = new ArrayList<>();
//遍历id<5
for (int i = 0; i < 5; i++) {
list.add(i);
}
//执行SqlSession的查询方法,返回结果集
List<Student> students = session.selectList("findStudentById",list);
//输出查询结果信息
for(Student Student1 : students){
//输出结果信息
System.out.println(Student1);
}
//关闭SqlSession
session.close();
}
}
实验小结
本实验主要对动态SQL的相关知识进行了大体的应用。包括动态SQL中的元素,条件查询操作,包括< choose>元素、< when>元素、< otherwise>元素和< where>元素的使用。通过本实验,可以初步掌握常用动态SQL元素的主要作用。
谢谢浏览!
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
电脑0x0000001A蓝屏错误怎么U盘重装系统教学分享。有用户电脑开机之后遇到了系统蓝屏的情况。系统蓝屏问题很多时候都是系统bug,只有通过重装系统来进行解决。那么蓝屏问题如何通过U盘重装新系统来解决呢?来看看以下的详细操作方法教学吧。 准备工作: 1、U盘一个(尽量使用8G以上的U盘)。 2、一台正常联网可使用的电脑。 3、ghost或ISO系统镜像文件(Win10系统下载_Win10专业版_windows10正式版下载-系统之家)。 4、在本页面下载U盘启动盘制作工具:系统之家U盘启动工具。 U盘启动盘制作步骤: 注意:制作期间,U盘会被格式化,因此U盘中的重要文件请注
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
项目介绍随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱小学生兴趣延时班预约小程序的设计与开发被用户普遍使用,为方便用户能够可以随时进行小学生兴趣延时班预约小程序的设计与开发的数据信息管理,特开发了小程序的设计与开发的管理系统。小学生兴趣延时班预约小程序的设计与开发的开发利用现有的成熟技术参考,以源代码为模板,分析功能调整与小学生兴趣延时班预约小程序的设计与开发的实际需求相结合,讨论了小学生兴趣延时班预约小程序的设计与开发的使用。开发环境开发说明:前端使用微信微信小程序开发工具:后端使用ssm:VU
需求:要创建虚拟机,就需要给他提供一个虚拟的磁盘,我们就在/opt目录下创建一个10G大小的raw格式的虚拟磁盘CentOS-7-x86_64.raw命令格式:qemu-imgcreate-f磁盘格式磁盘名称磁盘大小qemu-imgcreate-f磁盘格式-o?1.创建磁盘qemu-imgcreate-fraw/opt/CentOS-7-x86_64.raw10G执行效果#ls/opt/CentOS-7-x86_64.raw2.安装虚拟机使用virt-install命令,基于我们提供的系统镜像和虚拟磁盘来创建一个虚拟机,另外在创建虚拟机之前,提前打开vnc客户端,在创建虚拟机的时候,通过vnc
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
因为我现在正在做一些时间测量,我想知道是否可以在不使用Benchmark类或命令行实用程序time的情况下测量用户时间或系统时间。使用Time类只显示挂钟时间,而不显示系统和用户时间,但是我正在寻找具有相同灵active的解决方案,例如time=TimeUtility.now#somecodeuser,system,real=TimeUtility.now-time原因是我有点不喜欢Benchmark,因为它不能只返回数字(编辑:我错了-它可以。请参阅下面的答案。)。当然,我可以解析输出,但感觉不对。*NIX系统的time实用程序也应该可以解决我的问题,但我想知道是否已经在Ruby中实