🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ package cn.itcast.datasource.jdbctemplate; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ import cn.itcast.datasource.utils.DruidUtils; import org.springframework.jdbc.core.JdbcTemplate; /** * JdbcTemplate入门 */ public class JdbcTemplateDemo1 { public static void main(String[] args) { //1.导入jar包 //2.创建JDBCTemplate对象 JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource()); //3.调用方法 String sql="update account set balance=? where id=?"; int count = template.update(sql, 5000.98,6); System.out.println(count); } } ~~~ ***** ~~~ package cn.itcast.datasource.jdbctemplate; import cn.itcast.datasource.domain.Emp; import cn.itcast.datasource.utils.DruidUtils; import org.junit.Test; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ public class JdbcTemplateDemo2 { //Junit单元测试,可以让方法独立执行 //1.获取JDBCTemplate对象 private JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource()); /** * 1) 修改1号数据的salary为10000 */ @Test public void test1() { //2.定义sql // String sql = "update emp set salary=10000 where id=1"; String sql = "update emp set salary=? where id=?"; //3.执行sql // int count = template.update(sql); int count = template.update(sql, 10000, 1); System.out.println(count); } /** * 2) 添加一条记录 */ @Test public void test2() { //2.定义sql String sql = "insert into emp(ename,dept_id) values(?,?)"; //3.执行sql int count = template.update(sql, "乔峰", 2); System.out.println(count); } /** * 3)删除刚才添加的记录 */ @Test public void test3() { String sql = "delete from emp where id=?"; int count = template.update(sql, 4); System.out.println(count); } /** * 4) 查询id为1的记录,将其封装为Map集合 * 注意:这个方法查询的结果集长度只能是1 */ @Test public void test4() { String sql = "select * from emp where id=?"; // String sql = "select * from emp where id=? or id=?"; Map<String, Object> map = template.queryForMap(sql, 1); // Map<String, Object> map = template.queryForMap(sql, 1,2);//出错 System.out.println(map); System.out.println(map.get("id")); } /** * 5) 查询所有记录,将其封装为List */ @Test public void test5() { String sql = "select * from emp"; List<Map<String, Object>> list = template.queryForList(sql); for (Map<String, Object> stringObjectMap : list) { System.out.println(stringObjectMap); } } /** * 6) 查询所有记录,将其封装为Emp对象的List集合 * 自定义emp */ @Test public void test6_1() { String sql = "select * from emp"; List<Emp> list = template.query(sql, new RowMapper<Emp>() { @Override public Emp mapRow(ResultSet rs, int i) throws SQLException { Emp emp = new Emp(); int id = rs.getInt("id"); String ename = rs.getString("ename"); int job_id = rs.getInt("job_id"); int mgr = rs.getInt("mgr"); Date joindate = rs.getDate("joindate"); double salary = rs.getDouble("salary"); double bouns = rs.getDouble("bouns"); int dept_id = rs.getInt("dept_id"); emp.setId(id); emp.setEname(ename); emp.setJob_id(job_id); emp.setMgr(mgr); emp.setJoindate(joindate); emp.setSalary(salary); emp.setBounds(bouns); emp.setDept_id(dept_id); return emp; } }); for (Emp emp : list) { System.out.println(emp); } } /** * 6) 查询所有记录,将其封装为Emp对象的List集合 * 使用系统emp */ @Test public void test6_2() { String sql = "select * from emp"; 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); } } ~~~