栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Web 网络商城案例演示二十一 利用工厂模式解耦(适用于不同数据库的配置)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Web 网络商城案例演示二十一 利用工厂模式解耦(适用于不同数据库的配置)

}

public class CategoryServiceImpl implements CategoryService {

CategoryDao categoryDao = (CategoryDao) BeanFactory.createObject(“CategoryDao”) 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 ;

@Override

public List getAllCats() throws Exception {

return categoryDao.getAllCats();

}

@Override

public void addCategory(Category category) throws Exception {

// 本质是向mysql插入一条数据

categoryDao.addCategory(category);

// 更新redis的缓存

Jedis jedis = JedisUtils.getJedis();// 获取到Jedis的实例

jedis.del(“allCats”);// 删除redis当中对应allCats的数据

JedisUtils.closeJedis(jedis);// 关闭当前jedis对象

// 当访问页面时会在CategoryServlet类当中从新开启redis获取对应数据库当中的内容并将其再次放入到redis

}

}

public class UserServiceImpl implements UserService {

UserDao userDao = (UserDao) BeanFactory.createObject(“UserDao”);

@Override

public void userRegist(User user) throws SQLException {

// TODO Auto-generated method stub

// 实现注册功能

userDao = new UserDaoImpl();

userDao.userRegist(user);

}

@Override

public boolean userActive(String code) throws SQLException {

// TODO Auto-generated method stub

// 对DB发送select * from user where code = ?

User user = userDao.userActive(code);// 将code传入到dao层查看数据,返回该code对应的user,以方便后面操作user

if (null != user) {

// 可以根据激活码查询到一个用户

// 修改用户的状态,清除激活码

user.setState(1);

user.setCode(null);

// 对数据库执行一次真实的更新操作

// update user set username=? ,password = ? ,name = ? ,email = ? ,telephone = ?

// , birthday = ?,sex = ? ,state = ? ,code = ? where uid = ?

userDao.updateUser(user);

return true;

} else {

// 不可以根据激活码查询到一个用户

return false;

}

}

@Override

public User userLogin(User user) throws SQLException {

// 此处:可以利用异常在模块当中传递一些数据

// select * from user where username = ? and password =?

User uu = userDao.userLogin(user);

if (null == uu) {

// 在这里抛出对应的异常让其在Servlet里面捕获到对应的异常,并反馈到对应的页面上去

throw new RuntimeException(“密码有误!”);

} else if (uu.getState() == 0) {

throw new RuntimeException(“用户未激活!”);

} else {

return uu;

}

}

}

public class ProductServiceImpl implements ProductService {

ProductDao productDao = (ProductDao) BeanFactory.createObject(“ProductDao”);

@Override

public void saveProduct(Product product) throws Exception {

productDao.saveProduct(product);

}

@Override

public List findHots() throws Exception {

// TODO Auto-generated method stub

return productDao.findHots();

}

@Override

public List findNews() throws Exception {

// TODO Auto-generated method stub

return productDao.findNews();

}

@Override

public Product findProductByPid(String pid) throws Exception {

// TODO Auto-generated method stub

return productDao.findProductByPid(pid);

}

// 当点入分类信息的时候,默认cid就是1所以,当前页得到的就是第1页,而起始页经过计算得到的是1终止页得到的是3

@Override

public PageModel findProductsByCidWithPage(String cid, int curNum) throws Exception {

// 1、创建PageModel对象目的:计算分页参数

// 统计当前分类下商品的个数 select count(*) from product where cid=?

int totalRecords = productDao.findTotalRecords(cid);

PageModel pm = new PageModel(curNum, totalRecords, 12);

// 2、关联集合 select * from product where cid = ? LIMIT ?,?

List list = productDao.findProductsByCidWithPage(cid, pm.getStartIndex(), pm.getPageSize());

pm.setList(list);

// 3、关联url

pm.setUrl(“ProductServlet?method=findProductsByCidWithPage&cid=” + cid);

// 返回所有的分页数据以及对应的页码

System.out.println(pm);

return pm;

}

@Override

public PageModel findAllProductsWithPage(int curNum) throws Exception {

// 计算数据库当中所有商品分类的个数

int totalRecords = productDao.findTotalRecords();

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930776.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号