JdbcTemplate
Spring框架对JDBC进行了封装,使用JdbcTemplate实现对数据库的操作
1-连接数据库
<beans>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql:///book?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<!-- jdbcTemplate对象配置 并注入-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入对象-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<context:component-scan base-package="JdbcTemplate"></context:component-scan>
</beans>
2-注解进行注入
@Repository
public class BookDaoImpl implements BookDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
...
...
}
@Service
public class BookService {
@Autowired
private BookDAO bookDAO;
}
3-编写DAO层实现
package JdbcTemplate.dao;
import JdbcTemplate.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Arrays;
import java.util.List;
@Repository
public class BookDaoImpl implements BookDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
String sql = "insert into t_user values(?,?,?,?)";
int update = jdbcTemplate.update(sql, book.getId(), book.getUsername(), book.getPassword(), book.getEmail());
System.out.println("添加了:" + update + " 行!");
}
@Override
public void update(Book book) {
String sql = "update t_user set username=?,password=?,email=? where id=?";
int update = jdbcTemplate.update(sql, book.getUsername(),book.getPassword(),book.getEmail(),book.getId());
System.out.println("修改了:" + update + " 行!");
}
@Override
public void delete(String id) {
String sql = "delete from t_user where id=?";
int update = jdbcTemplate.update(sql, id);
System.out.println("删除了:" + update + " 行!");
}
@Override
public int selectCount() {
String sql = "select count(*) from t_user";
Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
return integer;
}
/**
* 查询某个信息
* @param id 查询索引
* @return
*/
@Override
public Book findBookInfo(int id) {
String sql = "select * from t_user where id=?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Book.class), id);
return book;
}
@Override
public List<Book> findBookList() {
String sql = "select * from t_user";
List<Book> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Book.class));
return list;
}
@Override
public void addBatch(List<Object[]> batch) {
String sql = "insert into t_user values(?,?,?,?)";
int[] ints = jdbcTemplate.batchUpdate(sql, batch);
System.out.println(Arrays.toString(ints));
}
@Override
public void batchDelete(List<Object[]> batch) {
String sql = "delete from t_user where id=?";
int[] ints = jdbcTemplate.batchUpdate(sql, batch);
System.out.println(Arrays.toString(ints));
}
}
- queryForObject(sql,xxx.class) 查询返回某个值
- queryForObject(sql,new BeanPropertyRowMapper<>(Book.class),xxx) 查询返回某个对象
- update(sql,xxx):更新、删除、修改某个对象
- query(sql,RowMapper<>(Book.class)):返回集合
- batchUpdate(sql,List):批量修改、删除、添加