JDBC基础操作一套

package com.atguigu.test;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.sql.DataSource;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;


import com.atguigu.bean.Employee;
import com.atguigu.dao.BookDao;
import com.atguigu.dao.UserDao;


@Repository
class BookDao {

@Autowired
private JdbcTemplate jdbcTemplate;

public void save(){
System.out.println(jdbcTemplate);
String sql = "update employee set salary=? where emp_id=?";
jdbcTemplate.update(sql, 1300.00, 5);
}


}




public class JDBCTest {
ApplicationContext ioc = new ClassPathXmlApplicationContext(
"applicationContext.xml");
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
NamedParameterJdbcTemplate template = ioc.getBean(NamedParameterJdbcTemplate.class);



/**
* 实验9:创建JdbcTemplateDao,自动装配JdbcTemplate对象
*/
@Test
public void test08(){
BookDao bean = ioc.getBean(BookDao.class);
bean.save();
}
/**
* 实验8:以SqlParameterSource形式传入参数值
*/
@Test
public void test07() {
//具名参数sql语句的书写
//格式  :参数名 替换原有的? 作为占位符
String sql = "insert into employee(emp_name,salary) values(:empName,:salary)";
//用来保存要传入的参数
//具名参数的名和对象的属性名也应该一致
Map<String, Object> map = new HashMap<>();
map.put("empName", "李四3");
map.put("salary", 9989.7);
Employee employee = new Employee(3, "李四2", 998.98);
template.update(sql, new BeanPropertySqlParameterSource(employee));
//有map直接传map就行
//template.update(sql, new MapSqlParameterSource(map));
}

/**
* 实验7:使用带有具名参数的SQL语句插入一条员工记录,并以Map形式传入参数值
* 以前的参数的传入是按照?的索引顺序来的。
*/
@Test
public void test06() {
//具名参数sql语句的书写
//格式  :参数名 替换原有的? 作为占位符
String sql = "insert into employee(emp_name,salary) values(:empName,:salary)";
//用来保存要传入的参数
Map<String, Object> map  = new HashMap<String, Object>();
map.put("empName", "李四1");
map.put("salary", 9989.98);
template.update(sql, map);
}


/**
* 实验6:查询最大salary
*/
@Test
public void test05() {
String sql = "SELECT MAX(salary) FROM employee";
// queryForObject查出单个对象并封装
// 如果是自定义的类型,需要封装就使用BeanPropertyRowMapper
// 如果是jdk内置的内型(基本类型,Date,...)就可以直接传递类的类型
Double max = jdbcTemplate.queryForObject(sql, Double.class);
System.out.println(max);
}


/**
* 实验5:查询salary>4000的数据库记录,封装为List集合返回
*/
@Test
public void test04() {
String sql = "select emp_id id,emp_name empName,salary from employee where salary>?";
// RowMapper只是来说明返回的数据封装为什么类型
// query用来查询集合数据
List<Employee> query = jdbcTemplate.query(sql,
new BeanPropertyRowMapper<Employee>(Employee.class), 4000);
System.out.println(query);
}


/**
* 实验4:查询emp_id=5的数据库记录,封装为一个Java对象返回
*/
@Test
public void test03() {
String sql = "select emp_id id,emp_name empName,salary from employee where emp_id=?";
// 使用RowMapper的实现类BeanPropertyRowMapper,来实现查出的数据封装为javaBean功能
// 查询单个值并封装对象queryForObject
Employee employee = jdbcTemplate.queryForObject(sql,
new BeanPropertyRowMapper<>(Employee.class), 5);
System.out.println(employee);
}


/**
* 实验3:批量插入
*/
@Test
public void test02() {
String sql = "insert into employee(emp_name,salary) values(?,?)";
List<Object[]> list = new ArrayList<>();
list.add(new Object[] { "张三1", "18991.98" });
list.add(new Object[] { "张三2", "18992.98" });
list.add(new Object[] { "张三3", "18993.98" });
list.add(new Object[] { "张三4", "18994.98" });
jdbcTemplate.batchUpdate(sql, list);
}


/**
* 实验2:将emp_id=5的记录的salary字段更新为1300.00
*/
@Test
public void test01() {
String sql = "update employee set salary=? where emp_id=?";
jdbcTemplate.update(sql, 1300.00, 5);
}


/**
* 实验1:测试数据源

* @throws SQLException
*/
@Test
public void test() throws SQLException {
DataSource bean = ioc.getBean(DataSource.class);
Connection connection = bean.getConnection();
System.out.println(connection);
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值