(1)spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作
(1) 引入相关jar
druid.jar ,mysql.jar , spring-jdbc.jar,spring-tx.jar,spring-orm.jar
(2)在spring配置 连接池
(3)配置JdbcTemplate对象, 注入DataSource
(4)创建service类 创建dao 类,在dao注入jdbcTemplate 对象
在配置文件中 添加:
在service中注入dao, 在dao中注入jdbcTemplate对象
@Repository public class BookDaoImpl implements BookDao { //注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; }
@Service public class BookService { //注入dao @Autowired private BookDao bookDao; }
1.对应数据库表,创建实体类
2.编写service 和dao
(1) 在dao中实现添加方法
(2)在JDBCTemplate对象里面调用update方法实现添加操作
@Repository public class BookDaoImpl implements BookDao { //注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; @Override public void save(Book book) { String sql = "insert into book(name,status) values(?,?)"; Object[] args = {book.getName(),book.getStatus()}; int row = jdbcTemplate.update(sql,args); System.out.println("受影响的行数"+row); } }
public void saveBook(Book book){ bookDao.save(book); }
3.编写测试类
@Test public void testJdbc(){ ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); BookService bs = ac.getBean("bookService",BookService.class); Book book = new Book("java编程思想","1"); bs.saveBook(book); }
@Service
public class BookService {
//注入dao
@Autowired
private BookDao bookDao;
public void saveBook(Book book){
bookDao.save(book);
}
public void updateBook(Book book){
bookDao.update(book);
}
public void delById(int id){
bookDao.delete(id);
}
}
@Repository
public class BookDaoImpl implements BookDao {
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void save(Book book) {
String sql = "insert into book(name,status) values(?,?)";
Object[] args = {book.getName(),book.getStatus()};
int row = jdbcTemplate.update(sql,args);
System.out.println("添加-受影响的行数"+row);
}
@Override
public void update(Book book) {
String sql = "update book set name=?,status=? where id=? ";
Object[] args = {book.getName(),book.getStatus(),book.getId()};
int row = jdbcTemplate.update(sql,args);
System.out.println("修改-受影响的行数"+row);
}
@Override
public void delete(int id) {
String sql = "delete from book where id="+id;
int row = jdbcTemplate.update(sql);
System.out.println("删除-受影响的行数"+row);
}
}
测试类:
@Test
public void testJdbc(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
BookService bs = ac.getBean("bookService",BookService.class);
// Book book = new Book("html","1");
// bs.saveBook(book);//添加
// bs.delById(1);// 删除
Book b = new Book(2,"1111","3");
bs.updateBook(b);//修改
}
查询表里 一共有多少条数据, 返回是 某个值
使用JdbcTemplate实现,返回某个值
@Override
public int selectCount() {
String sql = " select count(*) from book ";
Integer count = jdbcTemplate.queryForObject(sql,Integer.class);
return count;
}
测试类
int count = bs.findCount();
System.out.println(count);
1.按照 主键id 查询所有信息
2.使用JdbcTemplate的queryForObject方法实现,返回对象
RowMapper是接口,返回不同类型数据
封装
//dao @Override public Book findOne(int id){ String sql = " select * from book where id=?"; Book book = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper
(Book.class),id); return book; } //service public Book findOne(int id){ return bookDao.findOne(id); }
查询 全部图书信息
调用jdbcTemplate方法query实现查询 返回集合
@Override
public List findAllBook() {
String sql = " select * from book ";
List list = jdbcTemplate.query(sql,new BeanPropertyRowMapper(Book.class));
return list;
}
public List findAll(){
return bookDao.findAllBook();
}
批量操作: 操作表里多条记录
2.JdbcTemplate的batchUpdate 方法实习批量添加
//批量添加 @Override public void batchAdd(List
//测试 // 批量添加 List
//批量修改
@Override
public void batchUpdate(List
测试:
//批量修改
List
@Override
public void batchDel(List
//测试
//批量删除
List