1.导入所需jar包
2.设置数据库信息
3.创建jdbcTemplate对象
4.调用方法
@Test
public void add(){
//设置数据库信息
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("123");
//创建jdbcTemplate对象,设置数据源
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//调用方法实现
//编写sql语句
String sql = "insert into user values(?,?)";
jdbcTemplate.update(sql, "baozi", "250");
}
@Test
public void update(){
//将之前的数据库信息设置封装成了一个方法
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "update user set password = ? where username = ?";
jdbcTemplate.update(sql, "sb" , "baozi");
}
@Test
public void delete(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "delete from user where username = ?";
jdbcTemplate.update(sql, "baozi");
}
(1)查询返回某一个值
@Test
public void select1(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select count(*) from user";
//返回一个值用queryForObject方法,第一个参数为sql语句,第二个参数为返回值类型的class
int count = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(count);
}
(2)查询返回对象
queryForObject方法,第一个参数为sql语句,第二个参数为RowMapper接口,需要自己写类实现接口,第三个参数是查询参数
class MyRowMapper implements RowMapper{
@Override
public User mapRow(ResultSet rs, int num) throws SQLException {
//1.从结果集中得到数据
String username = rs.getString("username");
String password = rs.getString("password");
//2.把得到的数据封装到对象中
User user = new User();
user.setUsername(username);
user.setPassword(password);
return user;
}
}
@Test
public void select2(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select * from user where username = ?";
User user = jdbcTemplate.queryForObject(sql, new MyRowMapper(), "孙笑川");
System.out.println(user);
}
(3)返回list集合
@Test
public void select3(){
DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "select * from user";
List list = jdbcTemplate.query(sql, new MyRowMapper());
System.out.println(list);
}
1.导入jar包
2.配置连接池
1.创建service对象,在service中注入dao
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
userDao.add();
}
}
2.在dao对象中注入jdbcTemplate
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void add(){
String sql = "insert into user values(?,?)";
jdbcTemplate.update(sql, "嗯扫", "eight");
}
}
3.在jdbcTemplate中注入dataSource
测试代码
@Test
public void testC3p0(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
1.声明式事务管理(xml)
2.声明式事务管理(注解)