package cs.itcast.mysqlconn;
import cs.itcast.Student.student;
import cs.itcast.utils.JdbcUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
/
修改
添加
删除
查询dept_id为1的记录封装map集合
查询所有记录封装为list
查询所有记录将其封装为emp对象的list集合
查询总记录数 /
public class Jdbcpractice {
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());
@Test
public void text(){
//获取jdbctemplate
//sql
String sql = "update stu set weight = 120 where name = '郑梓桐'";
int count =jdbcTemplate.update(sql);
System.out.println(count);
}
@Test
public void add(){
String sql ="insert into stu values('张麻子','女',100,173,'java')";
int count = jdbcTemplate.update(sql);
System.out.println(count);
}
@Test
public void delete(){
String sql ="delete from stu where name = '张麻子'";
int count = jdbcTemplate.update(sql);
System.out.println(count);
}
@Test//封装为map
public void select(){
String sql = "select from stu where hobby = 'JAVA'";
List> maps = jdbcTemplate.queryForList(sql);
System.out.println(maps);
}
@Test//封装为list
public void select01(){
String sql = "select from stu";
List> maps = jdbcTemplate.queryForList(sql);
System.out.println(maps);
}
@Test
public void select02(){
String sql = "select *from stu";
List list = jdbcTemplate.query(sql,new BeanPropertyRowMapper(student.class));
for(student stu:list)
System.out.println(stu);
}
@Test
public void selectcount(){
String sql = "select count(name) from stu";
long total = jdbcTemplate.queryForObject(sql,long.class);
System.out.println(total);
}
}