视频地址:哔哩哔哩
讲义作业飞书地址:day06作业
基础性作业,加油!
1、SQL语句的编写
-- 1. 查询所有的性别为男(gender 为 1)的 讲师 (job 为 2) , 并根据入职时间, 对员工进行升序排序
select * from emp where gender = 1 and job = 2 order by entry_date asc;
-- 2. 根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 ID 进行降序排序
select * from emp order by entry_date asc,id desc;
-- 3. 查询性别为男(gender 为 1) 且 薪资大于 6000 的员工, 并根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 ID 进行降序排序
select * from emp where gender = 1 and salary > 6000 order by entry_date asc ,id desc ;
-- 4. 查询性别为男(gender 为 1)的员工 且 在 '2005-10-01' 至 '2018-10-01' 之间入职的员工, 并根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 ID 进行降序排序
select * from emp where gender = 1 and (entry_date between '2005-10-01' and '2018-10-01') order by entry_date asc ,id desc;
-- 5. 查询姓 '阮' 且 在 '2010-10-01' 之后入职的员工, 并根据入职时间进行升序排序, 并对结果分页操作, 展示第1页员工数据, 每页展示5条记录
select * from emp where name like '阮%' and entry_date > '2010-10-01' order by entry_date asc limit 5;
2、通过JDBC程序,基于预编译SQL,执行更新操作
@Test
public void testUpdate() throws ClassNotFoundException, SQLException {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取数据库连接
String url = "jdbc:mysql://localhost:3306/web01";
String username = "root";
String password = "moon";
String sql = "update user set password = ?, name = ?, age = ? where id = ?";
PreparedStatement ps = DriverManager.getConnection(url, username, password).prepareStatement(sql);
ps.setString(1,"haveagoodluck");
ps.setString(2,"关羽");
ps.setInt(3,32);
ps.setInt(4,4);
int i = ps.executeUpdate();
System.out.println(i);
ps.close();
}
3、通过JDBC程序,基于预编译SQL,执行查询语句
@Test
public void select() throws ClassNotFoundException, SQLException {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取数据库连接
String url = "jdbc:mysql://localhost:3306/web01";
String username = "root";
String password = "moon";
String sql = "select id,username,password,name,age from user where age >= ? and id <= ?";
PreparedStatement ps = DriverManager.getConnection(url, username, password).prepareStatement(sql);
ps.setInt(1,20);
ps.setInt(2,4);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
User user = new User(
rs.getInt("id"),
rs.getString("username"),
rs.getString("password"),
rs.getString("name"),
rs.getInt("age")
);
System.out.println(user);
}
rs.close();
ps.close();
}
4、导入项目,基于Mybatis完成SQL语句实现
①导入项目:将文件解压后放在目录里,然后导入xml文件
②创建数据库,创建表
③实现SQL语句(注意更改数据库连接信息,密码更改为自己的,url更改为对应数据库)
- StudentMapper.java
package com.itheima.mapper;
import com.itheima.pojo.Student;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@Mapper
public interface StudentMapper {
@Select("select * from student")
public List<Student> findAll();
@Insert("insert into student values(null,#{name},#{no},#{gender},#{phone},#{idCard},#{degree},#{graduationDate},#{createTime},#{updateTime})")
public void insert(Student s);
@Update("update student set no = #{no}, phone = #{phone}, id_card = #{idCard} where id = #{id}")
public void update(Student s);
//下面两个基于XML配置SQL
public Student findById(Integer id);
public void deleteById(Integer id);
}
- 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.itheima.mapper.StudentMapper">
<!--resultType:查询返回的单条记录所封装的类型-->
<select id="findById" resultType="com.itheima.pojo.Student">
select * from student where id = #{id}
</select>
<select id="deleteById" resultType="com.itheima.pojo.Student">
delete from student where id = #{id}
</select>
</mapper>
-
SpringBootMybatisApplicationTests.java
package com.itheima;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringBootMybatisApplicationTests {
@Autowired
public StudentMapper studentMapper;
@Test
public void testFindAll(){
studentMapper.findAll().forEach(System.out::println);
}
@Test
public void testInsert(){
Student s = new Student(null, "小明", "2019011", 1, "13888888888", "123456789012345678", 1, null, null, null);
studentMapper.insert(s);
}
@Test
public void testUpdate(){
Student s = new Student(12, "小明", "2019012", 1, "13888889998", "123456789012345789", 1, null, null, null);
studentMapper.update(s);
}
@Test
public void testFindById(){
System.out.println(studentMapper.findById(5));
}
@Test
public void testDeleteById(){
studentMapper.deleteById(7);
}
}
有问题可以私信我或者评论~