package com.looeywei.UserCRDUDemo.dao.impl;
import com.looeywei.UserCRDUDemo.dao.UserDao;
import com.looeywei.UserCRDUDemo.domain.User;
import com.looeywei.UserCRDUDemo.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 UserDaoImpl implements UserDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<User> findAll() {
// 使用JDBC操作数据库
String sql = "select * from user";
List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
return users;
}
@Override
public User findUserByUsernameAndPassword(String username, String password) {
try {
String sql = "select * from user where username = ? and password = ?";
User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
return user;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
@Override
public void addUser(User user) {
String sql = "insert into user values(NULL, ?, ?, ?, ? ,?, ? , null, null)";
template.update(sql, user.getName(), user.getGender(), user.getAge(),
user.getAddress(), user.getQq(), user.getEmail());
}
@Override
public void delUser(int id) {
String sql = "delete from user where id = ?";
template.update(sql, id);
}
@Override
public User findUserById(int id) {
String sql = "select * from user where id = ?";
return template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
}
@Override
public void updateUser(User user) {
String sql = "update user set name=?, gender=?, age=?, address=?, qq=?, email=? where id=?";
template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId());
}
/**
* 查询总数量
* @return
* @param map
*/
@Override
public int findTotalCount(Map<String, String[]> map) {
// 定义初始sql
String sql = "select count(*) from user where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
// 遍历map
Set<String> keySet = map.keySet();
// 参数集合
List<Object> params = new ArrayList<>();
for (String key : keySet) {
// 排除分页条件参数
if("currentPage".equals(key) || "rows".equals(key)){
continue;
}
// 获取value, 明确只有一个值
String value = map.get(key)[0];
if(value != null && !"".equals(value)){
sb.append(" and " + key + " like ? ");
params.add("%" + value + "%");
}
}
System.out.println(sb);
System.out.println(params);
int totalCount = template.queryForObject(sb.toString(), Integer.class, params.toArray());
return totalCount;
}
/**
* 分页查询
* @param start
* @param rows
* @param map
* @return
*/
@Override
public List<User> findByPage(int start, int rows, Map<String, String[]> map) {
String sql = "select * from user where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
// 遍历map
Set<String> keySet = map.keySet();
// 参数集合
List<Object> params = new ArrayList<>();
for (String key : keySet) {
// 排除分页条件参数
if("currentPage".equals(key) || "rows".equals(key)){
continue;
}
// 获取value, 明确只有一个值
String value = map.get(key)[0];
if(value != null && !"".equals(value)){
sb.append(" and " + key + " like ? ");
params.add("%" + value + "%");
}
}
// 添加分页查询
sb.append(" limit ?, ? ");
params.add(start);
params.add(rows);
List<User> list = template.query(sb.toString(), new BeanPropertyRowMapper<>(User.class), params.toArray());
return list;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Javaweb的Demo.zip Javaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zipJavaweb的Demo.zip
资源推荐
资源详情
资源评论
收起资源包目录
Javaweb的Demo.zip (633个子文件)
UserDaoImpl.class 6KB
UserDaoImpl.class 6KB
JedisTest.class 4KB
UserServiceImpl.class 4KB
UserServiceImpl.class 4KB
LoginServlet.class 3KB
LoginServlet.class 3KB
SensitiveWordsFilter.class 3KB
SensitiveWordsFilter.class 3KB
CheckCodeServlet.class 3KB
CheckCodeServlet.class 3KB
User.class 3KB
User.class 3KB
CheckCodeServlet.class 3KB
CheckCodeServlet.class 3KB
CookieServlet.class 3KB
CookieServlet.class 3KB
CheckCodeServlet.class 3KB
CheckCodeServlet.class 3KB
PageBean.class 3KB
PageBean.class 3KB
LoginServlet.class 2KB
LoginServlet.class 2KB
JacksonTest.class 2KB
FindUserByPageServlet.class 2KB
FindUserByPageServlet.class 2KB
UpdateUserServlet.class 2KB
UpdateUserServlet.class 2KB
AddUserServlet.class 2KB
AddUserServlet.class 2KB
LoginServlet.class 2KB
LoginServlet.class 2KB
DownloadServlet.class 2KB
DownloadServlet.class 2KB
ProvinceServiceImpl.class 2KB
ProvinceServiceImpl.class 2KB
LoginFilter.class 2KB
LoginFilter.class 2KB
FindUserServlet.class 2KB
FindUserServlet.class 2KB
SensitiveWordsFilter$1.class 2KB
SensitiveWordsFilter$1.class 2KB
Person.class 2KB
FindUserServlet.class 2KB
FindUserServlet.class 2KB
DelSelectedServlet.class 2KB
DelSelectedServlet.class 2KB
ProxyTest$1.class 2KB
ServletDemo2.class 2KB
ServletDemo2.class 2KB
UserListServlet.class 2KB
UserListServlet.class 2KB
Demo5_http.class 2KB
Demo5_http.class 2KB
JedisPoolUtils.class 2KB
JedisPoolUtils.class 2KB
DelUserServlet.class 2KB
DelUserServlet.class 2KB
JedisPoolUtils.class 2KB
ProvinceServlet.class 2KB
ProvinceServlet.class 2KB
SuccessServlet.class 2KB
SuccessServlet.class 2KB
ContextLoaderListener.class 2KB
ServletDemo1.class 2KB
ServletDemo1.class 2KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
HelloServlet.class 1KB
User.class 1KB
User.class 1KB
Demo3_zhujie.class 1KB
Demo3_zhujie.class 1KB
ServletDemo2.class 1KB
ServletDemo2.class 1KB
UserDao.class 1KB
UserDao.class 1KB
JDBCUtils.class 1KB
JDBCUtils.class 1KB
JDBCUtils.class 1KB
JDBCUtils.class 1KB
JDBCUtils.class 1KB
JDBCUtils.class 1KB
Demo1_Servlet.class 1KB
Demo1.class 1KB
Demo1_Servlet.class 1KB
ProvinceDaoImpl.class 1KB
ProvinceDaoImpl.class 1KB
ProxyTest.class 1KB
共 633 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
程序员张小妍
- 粉丝: 1w+
- 资源: 3252
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功