2023年6月21日发(作者:)

JdbcTemplate的使⽤⼤全1、判断数据库是否获得连接@Test public void test() throws SQLException { DataSource bean = n(); Connection connection = nection(); n(connection); }2、sql 语句的按位更新、或插⼊@Test public void test02(){ //1、从容器中获取JdbcTemplate进⾏操作 String sql = "update employee set salary=? where emp_id=?"; int update = (sql, 1300,5); n(update); }3、sql 语句的批量插⼊ @Test public void test03(){ String sql = "INSERT INTO `employee`(`emp_name`,`salary`) VALUES (?,?)"; List args = new ArrayList<>(); (new Object[]{"张三",9989.98}); (new Object[]{"李四",19989.98}); pdate(sql,args); }4、查询单个对象数据字段与数据库相同BeanPropertyRowmapper⽤来将javaBean属性和查出的数据⼀⼀映射 @Test public void test04(){ String sql = "select * from employee where emp_id=?"; Employee employee = orObject(sql, new BeanPropertyRowMapper<>(), 7); }数据字段与数据库不同 需重写⽅法 @Test public void test04(){ String sql = "select * from employee where emp_id=?"; Employee employee = orObject(sql, new RowMapper(){ @Override public Employee mapRow(ResultSet rs, int index)throws SQLException { Employee temp = new Employee (); ountid(("id")); inname(ing("name")); return temp;

} }, 7); }4-2、查询集合对象@Test public void test04(){ String sql = "select * from employee where emp_id=?"; List employees = (sql, new RowMapper(){ @Override public Employee mapRow(ResultSet rs, int index)throws SQLException { Employee temp = new Employee (); ountid(("id")); inname(ing("name")); return temp;

} }, 7); }5、使⽤带有具名参数的SQL语句插⼊⼀条记录,并以Map形式传⼊参数值@Test public void test07(){ String sql = "INSERT INTO `employee`(`emp_name`,`salary`) VALUES (:empName,:salary)"; Map map = new HashMap<>(); ("empName", "韩总"); ("salary", 40000.0); //如果⽀持具名参数的功能。JdbcTemplate就不能⽤了。 /*MapSqlParameterSource source = new MapSqlParameterSource(map); (sql, source);*/ (sql, map); }6、以SqlParameterSource形式传⼊参数值@Test public void test08(){ String sql = "INSERT INTO `employee`(`emp_name`,`salary`) VALUES (:empName,:salary)"; Employee employee = new Employee(null, "韩五", 499998.00); //如果⽀持具名参数的功能。JdbcTemplate就不能⽤了。 //SqlParameterSource //BeanPropertySqlParameterSource //可以直接将Bean的属性映射参数进⾏处理 (sql, new BeanPropertySqlParameterSource(employee)); }7、⾃动装配 JdbcTemplate 对象 @Test public void test09(){ EmployeeDao bean = n(); Employee employee = new Employee(null, "韩五11", 499998.00); (employee); }

2023年6月21日发(作者:)

JdbcTemplate的使⽤⼤全1、判断数据库是否获得连接@Test public void test() throws SQLException { DataSource bean = n(); Connection connection = nection(); n(connection); }2、sql 语句的按位更新、或插⼊@Test public void test02(){ //1、从容器中获取JdbcTemplate进⾏操作 String sql = "update employee set salary=? where emp_id=?"; int update = (sql, 1300,5); n(update); }3、sql 语句的批量插⼊ @Test public void test03(){ String sql = "INSERT INTO `employee`(`emp_name`,`salary`) VALUES (?,?)"; List args = new ArrayList<>(); (new Object[]{"张三",9989.98}); (new Object[]{"李四",19989.98}); pdate(sql,args); }4、查询单个对象数据字段与数据库相同BeanPropertyRowmapper⽤来将javaBean属性和查出的数据⼀⼀映射 @Test public void test04(){ String sql = "select * from employee where emp_id=?"; Employee employee = orObject(sql, new BeanPropertyRowMapper<>(), 7); }数据字段与数据库不同 需重写⽅法 @Test public void test04(){ String sql = "select * from employee where emp_id=?"; Employee employee = orObject(sql, new RowMapper(){ @Override public Employee mapRow(ResultSet rs, int index)throws SQLException { Employee temp = new Employee (); ountid(("id")); inname(ing("name")); return temp;

} }, 7); }4-2、查询集合对象@Test public void test04(){ String sql = "select * from employee where emp_id=?"; List employees = (sql, new RowMapper(){ @Override public Employee mapRow(ResultSet rs, int index)throws SQLException { Employee temp = new Employee (); ountid(("id")); inname(ing("name")); return temp;

} }, 7); }5、使⽤带有具名参数的SQL语句插⼊⼀条记录,并以Map形式传⼊参数值@Test public void test07(){ String sql = "INSERT INTO `employee`(`emp_name`,`salary`) VALUES (:empName,:salary)"; Map map = new HashMap<>(); ("empName", "韩总"); ("salary", 40000.0); //如果⽀持具名参数的功能。JdbcTemplate就不能⽤了。 /*MapSqlParameterSource source = new MapSqlParameterSource(map); (sql, source);*/ (sql, map); }6、以SqlParameterSource形式传⼊参数值@Test public void test08(){ String sql = "INSERT INTO `employee`(`emp_name`,`salary`) VALUES (:empName,:salary)"; Employee employee = new Employee(null, "韩五", 499998.00); //如果⽀持具名参数的功能。JdbcTemplate就不能⽤了。 //SqlParameterSource //BeanPropertySqlParameterSource //可以直接将Bean的属性映射参数进⾏处理 (sql, new BeanPropertySqlParameterSource(employee)); }7、⾃动装配 JdbcTemplate 对象 @Test public void test09(){ EmployeeDao bean = n(); Employee employee = new Employee(null, "韩五11", 499998.00); (employee); }