- util
- 分页和中文乱码过滤器
- EncodingFiter
- PageBean
- entity
- Account实体类
- private String userName; 名字
- private Float balance; 钱的数量
- set()get()toString()
- dao
- IAccountDAO接口
- public void add(Account account);开户
- public void del(Account account);销户
- public void doSaveMoney(Account account);存钱
- public void doFetchMoney(Account account;取钱
- public Account load(Account account);根据userName查个,查不到返回null
- public Float loadBalance(Account account);根据userName查余额
- public List<Account> list(Account account,PageBean pageBean);查询全部‘、
- impl实现类
- BaseDao<T>有大改动
- public HibernateTemplate hibernateTemplate;
- 给它set()get()
- 给hql参数赋值,query 参数,mao需要赋值的接收值
- public void setParams(Query query,Map<String,Object> amp){
- if(null==map||map.size()==0){//如果为空直接返回
- return ;
- }
- //通过迭代器获取到用户传递的所有参数
- Set<Entry<String,Object>> entrySet=map.entrySet();
- for(Entry<String,Object> entry:entrySet){
- //判断是什么数据类型
- String key=entry.getKey();//String类型
- Object value=entry.getValue();//Object类型
- if(value instancof Collection){//集合赋值方式
- query.setParaneter(Key,(Collection)value);//键位,值
- }else if(value instanceof Object[]){//对象数组
- query.setParameter(Key,(Object[]) value);//键位,值
- }else {
- query.setParameter(Key,value);//单个的数据
- }
- }
- }
- 查询方法@SuppressWarnings("unchecked")可以在方法上写上这个
- public List executeQuery(final String hql, final PageBean pageBean,final Map<String,Object> map){
- return this.hibernateTemolate.execute(new HibernateCallback(){
- Query query=null;
- if(null!=pageBean&&pageBean.isPagination()){
- String countHql=countHql(hql);//得到总条目数语句
- query=session.createQuery(countHql);
- setParams(query,map);//设置hql语句的参数内容
- Object count=query.getSingleResult();//唯一结果
- pageBean.setTotal(count.toString());
- }
- query=session.createQuery(hql);//创建执行语句
- if(null!=pageBean&&pageBean.isPagination()){
- query.setFirstResult(pageBean.getStartIndex());
- query.setMaxResults(pageBean.getRows());
- }
- setParams(query,map);//解决where后面的参数赋值,给参数赋值
- List list=query.list();//赋完值然后在执行
- return list;
- }
- });
- 获取总条目数
- public static String countHql(String hql){
- 全部小写然后截取到from
- int start=hql.toLowerCase().lastIndexOf("from");
- String str=hql.substring(start);
- String countHql="select count(*)"+str;
- return countHql;
- }
- AccountDaoImpl 继承 BaseDao<Account> 实现 IAccountDAO
- 重写方法 this.getHibernateTemplate().save(account);
- 查询方法写语句 from Account 返回值
- biz
- IAccountBiz接口方法
- impl
- AccountBizImpl 实现 IAccountBiz
- private IAccountDAO accountDao;//交给spring实例化(set()get())
- action
- AccountAction 实现 ModelDriven<Account>
- private IAccountBiz accountBiz;
- private Account account=new Account();
- 在getModel中返回account
- load查询单个
- public String load(){
- Account load=accountBiz.load(account);
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpSession session = request.getSession();
- session.setAttribute("ac", load);
- return "update";
- }
- saveMoney存款
- publicc String saveMoney(){
- accountBiz.doSaveMoney(account);
- return "list";
- }
- FetchMoney取款
- public String FetchMoney(){
- accountBiz.doFetchMoney(account);
- return "list";
- }
- xml文件的配置,自己需要动的文件并不多,只是核心配置文件必须要然后就有点多
- Account.xml
- log4j.xml(这个文件可以去下载)
- spring.xml
- 注册以下两个文件
- <import resource="spring-base.xml"/>//连接池c3p0
- 必须是这些方法名才能开启环绕通知
- <import resource="spring-sys.xml"/>
- struts.xml
- 声明以下两个文件
- <include file="struts-base.xml"/>
- <include file="struts-sys.xml"/>
- 这个是你在action写的方法返回的值然后跳转的页面
- web.xml的相关配置
- 页面只写了提交路径
- savemoney.jsp
- action="accountAction_saveMoney"
- fetchmoney.jsp
- action="accountAction_fetchMoney"
- update.jsp
- action="accountAction-update"
- index.jsp