JdbcTemplate入门
简介
Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作
准备工作
(1)引入相关jar包
(2)在spring配置文件配置数据库连接池
<context:property-placeholder location="classpath:jdbc.properties" />
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
(3)配置JdbcTemplate对象,注入DataSource
<!--配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
4)创建service类,创建dao类,在dao注入jdbcTemplate对象
<!--配置扫描-->
<context:component-scan base-package="com.athome" />
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
JdbcTemplate操作数据库
添加
public int addUser(User user){
return userDao.add(user);
}
@Override
public int add(User user) {
String sql = "insert into boys(boyName,userCP) values(?,?)";
return jdbcTemplate.update(sql,user.getBoyName(),user.getUserCP());
}
删除
public int delete(int id){
return userDao.delete(id);
}
@Override
public int delete(int id) {
String sql = "delete from boys where id = ?";
return jdbcTemplate.update(sql, id);
}
修改
public int update(User user) {
return userDao.update(user);
}
@Override
public int update(User user) {
String sql = "update boys set boyName = ?,userCP = ? where id = ?";
return jdbcTemplate.update(sql, user.getBoyName(), user.getUserCP(), user.getId());
}
查询
查询单个值
public int getValue() {
return userDao.getValue();
}
@Override
public int getValue() {
String sql = "select count(*) from boys";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
查询一个对象
public User getUser(int id) {
return userDao.getUser(id);
}
@Override
public User getUser(int id) {
String sql = "select * from boys where id = ?";
return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
}
查询一个集合
public List<User> getUsers() {
return userDao.getUsers();
}
@Override
public List<User> getUsers() {
String sql = "select * from boys";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
}
批量操作
批量插入
public int[] batchAdd(List<Object[]> args) {
return userDao.batchAdd(args);
}
@Override
public int[] batchAdd(List<Object[]> args) {
String sql = "insert into boys(boyName,userCP) values(?,?)";
return jdbcTemplate.batchUpdate(sql, args);
}
批量修改
public int[] batchUpdate(List<Object[]> args) {
return userDao.batchUpdate(args);
}
@Override
public int[] batchUpdate(List<Object[]> args) {
String sql = "update boys set boyName = ?,userCP = ? where id = ?";
return jdbcTemplate.batchUpdate(sql, args);
}
批量删除
public int[] batchDel(List<Object[]> args) {
return userDao.batchDel(args);
}
@Override
public int[] batchDel(List<Object[]> args) {
String sql = "delete from boys where id = ?";
return jdbcTemplate.batchUpdate(sql, args);
}
结语
只要能收获甜蜜,荆棘丛中也有蜜蜂忙碌的身影,未来的你一定会感谢现在努力的自己。