小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
重要的注释都写在代码中了!!!
DML练习
需求:
- 修改1号数据的salary 为1000
- 添加一条记录
- 删除刚才添加的记录
- 查询id为1的记录,将其封装为Map集合
- 查询所有记录,将其封装为List
- 查询所有记录,将其封装为Emp对象的List集合
- 查询总记录数
代码
package com.caq.datasource.jdbctemplate;
import com.caq.datasource.utils.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbctemplateDemo02 {
//Junit单元测试,可以让方法独立执行
//1.获取JDBCTemplate对象
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 1.修改1号数据的salary为10000
*/
@Test
public void test1(){
//2.定义sql
String sql = "update emp set salary = 10000 where id = 1001";
//3.执行sql
int count = template.update(sql);
System.out.println(count);
}
/**
* 添加一条记录
*/
@Test
public void test2(){
String sql = "insert into emp(id,ename,dept_id) values(?,?,?)";//双引只能嵌套单引
int count = template.update(sql, 1015, "gj", 10);
System.out.println(count);
}
/**
* 删除刚才添加的记录
*/
@Test
public void test3(){
String sql = "delete from emp where id = ?";
int count = template.update(sql, 1015);
System.out.println(count);
}
}
分别对三个单元进行测试
结果如下:
DQL练习
JavaBeans是Java中一种特殊的类,可以将多个对象封装到一个对象(bean)中。 特点是可序列化,提供无参构造器,提供getter方法和setter方法访问对象的属性
RowMapper的使用实例
1.RowMapper的基本使用
从数据库查询出来的记录全都被保存在ResultSet结果集中,我们需要将结果集中的数据一条条地获取并设置到具体的实体类上,如此,该实体类才能在接下来的程序中使用。然而问题是,每次都要这么操作实在是太麻烦了,Spring就不应该提供什么功能来替我们做这些事情吗?RowMapper就是实现这个功能的
1.1 BeanPropertyRowMapper
当查询数据库返回的是多列数据,且你需要将这些多列数据映射到某个具体的实体类上
代码如下
@Override
public Student getStudentByName2(String name) {
String sql = "select name, gender from test_student where name = ?";
return this.jdbcTemplate.queryForObject(sql, new Object[]{name},
new BeanPropertyRowMapper<>(Student.class));
}
@Override
public List<Student> getStudentsByName2(String name) {
String sql = "select name, gender from test_student where name = ?";
return this.jdbcTemplate.query(sql, new Object[]{name},
new BeanPropertyRowMapper<>(Student.class));
}
代码
package com.caq.datasource.jdbctemplate;
import com.caq.datasource.utils.JDBCUtils;
import com.caq.datasource.domain.Emp;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
public class JdbctemplateDemo02 {
//Junit单元测试,可以让方法独立执行
//1.获取JDBCTemplate对象
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 1.修改1号数据的salary为10000
*/
@Test
public void test1(){
//2.定义sql
String sql = "update emp set salary = 10000 where id = 1001";
//3.执行sql
int count = template.update(sql);
System.out.println(count);
}
/**
* 添加一条记录
*/
@Test
public void test2(){
String sql = "insert into emp(id,ename,dept_id) values(?,?,?)";//双引只能嵌套单引
int count = template.update(sql, 1015, "gj", 10);
System.out.println(count);
}
/**
* 删除刚才添加的记录
*/
@Test
public void test3(){
String sql = "delete from emp where id = ?";
int count = template.update(sql, 1015);
System.out.println(count);
}
/**
* 4.查询id为1001的记录,将其封装为Map集合
* 注意:这个方法查询的结果集长度只能是一
*/
@Test
public void test4(){
String sql = "select * from emp where id = ?";
Map<String, Object> map = template.queryForMap(sql, 1001);
System.out.println(map);
//{id=1001, ename=孙悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20}
}
/**
* 5.查询所有记录,将其封装为List
*/
@Test
public void test5(){
String sql = "select * from emp";
List<Map<String, Object>> list = template.queryForList(sql);
//iter快捷键可快速生成循环
for (Map<String, Object> stringObjectMap : list) {
System.out.println(stringObjectMap);
}
}
/**
* 6.查询所有记录,将其封装为Emp对象的List集合
*/
@Test
public void test6(){
String sql = "select * from emp";
//可以自己实现接口,也可以用别人实现号的类BeanPropertyRowMapper
List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
for (Emp emp:list){
System.out.println(emp);
}
}
/**
* 7.查询总记录数
*/
@Test
public void test7(){
String sql = "select count(id) from emp";
Long total = template.queryForObject(sql, Long.class);
System.out.println(total);
//14
}
}
输出结果
com.caq.datasource.jdbctemplate.JdbctemplateDemo02,test6
十月 21, 2021 10:51:47 下午 com.alibaba.druid.pool.DruidAbstractDataSource error
严重: maxIdle is deprecated
十月 21, 2021 10:51:48 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Emp{id=1001, ename='孙悟空', job_id=4, mgr=1004, joindate=2000-12-17 00:00:00.0, salary=10000.0, bounds=null, dept_id=20}
Emp{id=1002, ename='卢俊义', job_id=3, mgr=1006, joindate=2001-02-20 00:00:00.0, salary=16000.0, bounds=null, dept_id=30}
Emp{id=1003, ename='林冲', job_id=3, mgr=1006, joindate=2001-02-22 00:00:00.0, salary=12500.0, bounds=null, dept_id=30}
Emp{id=1004, ename='唐僧', job_id=2, mgr=1009, joindate=2001-04-02 00:00:00.0, salary=29750.0, bounds=null, dept_id=20}
Emp{id=1005, ename='李逵', job_id=4, mgr=1006, joindate=2001-09-28 00:00:00.0, salary=12500.0, bounds=null, dept_id=30}
Emp{id=1006, ename='宋江', job_id=2, mgr=1009, joindate=2001-05-01 00:00:00.0, salary=28500.0, bounds=null, dept_id=30}
Emp{id=1007, ename='刘备', job_id=2, mgr=1009, joindate=2001-09-01 00:00:00.0, salary=24500.0, bounds=null, dept_id=10}
Emp{id=1008, ename='猪八戒', job_id=4, mgr=1004, joindate=2007-04-19 00:00:00.0, salary=30000.0, bounds=null, dept_id=20}
Emp{id=1009, ename='罗贯中', job_id=1, mgr=null, joindate=2001-11-17 00:00:00.0, salary=50000.0, bounds=null, dept_id=10}
Emp{id=1010, ename='吴用', job_id=3, mgr=1006, joindate=2001-09-08 00:00:00.0, salary=15000.0, bounds=null, dept_id=30}
Emp{id=1011, ename='沙僧', job_id=4, mgr=1004, joindate=2007-05-23 00:00:00.0, salary=11000.0, bounds=null, dept_id=20}
Emp{id=1012, ename='李逵', job_id=4, mgr=1006, joindate=2001-12-03 00:00:00.0, salary=9500.0, bounds=null, dept_id=30}
Emp{id=1013, ename='小白龙', job_id=4, mgr=1004, joindate=2001-12-03 00:00:00.0, salary=30000.0, bounds=null, dept_id=20}
Emp{id=1014, ename='关羽', job_id=4, mgr=1007, joindate=2002-01-23 00:00:00.0, salary=13000.0, bounds=null, dept_id=10}
Process finished with exit code 0
更新完了哦,如有其他更新后续会继续补充!!!