package cn.itcast.dao.impl;
import cn.itcast.dao.StudentDao;
import cn.itcast.domain.Student;
import cn.itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<Student> findAll() {
//使用JDBC操作数据库...
//1.定义sql
String sql = "select * from student";
List<Student> students = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
return students;
}
@Override
public void add(Student student) {
//1.定义sql
String sql = "insert into student values(null,?,?,?,?,?,?)";
//2.执行sql
template.update(sql, student.getName(), student.getGender(), student.getAge(), student.getStunumber(), student.getHeight(), student.getWeight());
}
@Override
public void delete(int id) {
//1.定义sql
String sql = "delete from student where id = ?";
//2.执行sql
template.update(sql, id);
}
@Override
public Student findById(int id) {
String sql = "select * from student where id = ?";
return template.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), id);
}
@Override
public void update(Student student) {
String sql = "update student set name = ?,gender = ? ,age = ? , stunumber = ? , height = ?, weight = ? where id = ?";
template.update(sql, student.getName(), student.getGender(), student.getAge(), student.getStunumber(), student.getHeight(), student.getWeight(), student.getId());
}
@Override
public int findTotalCount(Map<String, String[]> condition) {
//1.定义模板初始化sql
String sql = "select count(*) from student where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
//2.遍历map
Set<String> keySet = condition.keySet();
//定义参数的集合
List<Object> params = new ArrayList<Object>();
for (String key : keySet) {
//排除分页条件参数
if("currentPage".equals(key) || "rows".equals(key)){
continue;
}
//获取value
String value = condition.get(key)[0];
//判断value是否有值
if(value != null && !"".equals(value)){
//有值
sb.append(" and "+key+" like ? ");
params.add("%"+value+"%");//?条件的值
}
}
System.out.println(sb.toString());
System.out.println(params);
return template.queryForObject(sb.toString(),Integer.class,params.toArray());
}
@Override
public List<Student> findByPage(int start, int rows, Map<String, String[]> condition) {
String sql = "select * from student where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
//2.遍历map
Set<String> keySet = condition.keySet();
//定义参数的集合
List<Object> params = new ArrayList<Object>();
for (String key : keySet) {
//排除分页条件参数
if("currentPage".equals(key) || "rows".equals(key)){
continue;
}
//获取value
String value = condition.get(key)[0];
//判断value是否有值
if(value != null && !"".equals(value)){
//有值
sb.append(" and "+key+" like ? ");
params.add("%"+value+"%");//?条件的值
}
}
//添加分页查询
sb.append(" limit ?,? ");
//添加分页查询参数值
params.add(start);
params.add(rows);
sql = sb.toString();
System.out.println(sql);
System.out.println(params);
return template.query(sql,new BeanPropertyRowMapper<Student>(Student.class),params.toArray());
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
UserManagerSystem (2).zip (180个子文件)
StudentDaoImpl.class 5KB
StudentDaoImpl.class 5KB
StudentDaoImpl.class 5KB
LoginServlet.class 3KB
LoginServlet.class 3KB
LoginServlet.class 3KB
StudentServiceImpl.class 3KB
StudentServiceImpl.class 3KB
StudentServiceImpl.class 3KB
CheckCodeServlet.class 3KB
CheckCodeServlet.class 3KB
CheckCodeServlet.class 3KB
FindUserByPageServlet.class 2KB
FindUserByPageServlet.class 2KB
FindUserByPageServlet.class 2KB
UpdateUserServlet.class 2KB
UpdateUserServlet.class 2KB
UpdateUserServlet.class 2KB
AddUserServlet.class 2KB
AddUserServlet.class 2KB
AddUserServlet.class 2KB
RegisterUserServlet.class 2KB
RegisterUserServlet.class 2KB
RegisterUserServlet.class 2KB
Student.class 2KB
Student.class 2KB
Student.class 2KB
PageBean.class 2KB
PageBean.class 2KB
PageBean.class 2KB
FindUserServlet.class 2KB
FindUserServlet.class 2KB
FindUserServlet.class 2KB
UserDaoImpl.class 2KB
UserDaoImpl.class 2KB
UserDaoImpl.class 2KB
DelSelectedServlet.class 2KB
DelSelectedServlet.class 2KB
DelSelectedServlet.class 2KB
DelUserServlet.class 2KB
DelUserServlet.class 2KB
DelUserServlet.class 2KB
JDBCUtils.class 1KB
JDBCUtils.class 1KB
JDBCUtils.class 1KB
UserServiceImpl.class 1002B
UserServiceImpl.class 1002B
UserServiceImpl.class 1002B
User.class 920B
User.class 920B
User.class 920B
Test.class 803B
Test.class 803B
Test.class 803B
StudentService.class 740B
StudentService.class 740B
StudentService.class 740B
StudentDao.class 647B
StudentDao.class 647B
StudentDao.class 647B
UserDao.class 256B
UserDao.class 256B
UserDao.class 256B
UserService.class 235B
UserService.class 235B
UserService.class 235B
bootstrap.css 143KB
bootstrap.css 143KB
bootstrap.min.css 118KB
bootstrap.min.css 118KB
bootstrap-theme.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
.gitignore 224B
UserManagerSystem.iml 1KB
mysql-connector-java-8.0.21.jar 2.29MB
mysql-connector-java-8.0.21.jar 2.29MB
mysql-connector-java-8.0.21.jar 2.29MB
druid-1.0.9.jar 1.76MB
druid-1.0.9.jar 1.76MB
druid-1.0.9.jar 1.76MB
spring-core-4.2.4.RELEASE.jar 1.03MB
spring-core-4.2.4.RELEASE.jar 1.03MB
spring-core-4.2.4.RELEASE.jar 1.03MB
mysql-connector-java-5.1.18-bin.jar 771KB
mysql-connector-java-5.1.18-bin.jar 771KB
mysql-connector-java-5.1.18-bin.jar 771KB
spring-beans-4.2.4.RELEASE.jar 714KB
spring-beans-4.2.4.RELEASE.jar 714KB
spring-beans-4.2.4.RELEASE.jar 714KB
spring-jdbc-4.2.4.RELEASE.jar 413KB
spring-jdbc-4.2.4.RELEASE.jar 413KB
spring-jdbc-4.2.4.RELEASE.jar 413KB
jstl-impl.jar 379KB
jstl-impl.jar 379KB
jstl-impl.jar 379KB
spring-tx-4.2.4.RELEASE.jar 259KB
共 180 条
- 1
- 2
资源评论
2201_75560353
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功