1.学习使用ajax
代码:
function req(){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","response.html",true);
xmlhttp.send();
}
如果申请的是html文件 , 则返回的response是body里面的内容
2.事物不回滚问题解决方法
mysql 默认引擎不支持事务,所以在后台无论如何回滚都不会回滚,因此需要设置支持事务的引擎才能实现回滚
参考链接: https://blog.youkuaiyun.com/u011622226/article/details/43985929
package DAO;
import java.util.List;
import java.util.Map;
import javax.transaction.Transaction;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import Bean.BaseBean;
import Bean.Budget;
public class BaseDao {
protected SessionFactory sessionFactory;
public BaseDao() {
// TODO Auto-generated constructor stub
}
public void save(BaseBean bb){
sessionFactory.getCurrentSession().save(bb);
}
public void save(List<BaseBean> bbs){
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.save(bb);
}
}
public void delete(BaseBean bb){
sessionFactory.getCurrentSession().delete(bb);
}
public void delete(List<BaseBean> bbs){
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.delete(bb);
}
}
public void update(BaseBean bb){
sessionFactory.getCurrentSession().update(bb);
}
public void update(List<BaseBean> bbs){
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.update(bb);
}
}
public void saveOrUpdate(BaseBean bb){
sessionFactory.getCurrentSession().saveOrUpdate(bb);
}
public void saveOrUpdate(List<BaseBean> bbs){
//sessionFactory.getCurrentSession().saveOrUpdate(bb);
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.saveOrUpdate(bb);
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
3.加载静态模板
package Service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
/*
* 这个类适用于加载已经写好的模板文件的静态部分head部分,
* js代码以及css部分和一些body部分
* 但是同一模板文件的不同部分可能要分开存放,当需要插入一些
* 可变部分的时候,需要从其中分开,并将分开后的各部分用含有
* 一定意义的名字标记,方便加载
*/
public class HTMLWriter {
public HTMLWriter() {
// TODO Auto-generated constructor stub
}
//将一个文件加载到response里面
public static void load(HttpServletResponse response,String staticfile) throws IOException{
File f = new File(staticfile);
String content = "";
String line = "";
BufferedReader br=new BufferedReader( new FileReader(f));
while((line=br.readLine())!=null){
content+=line+"\r\n";
}
br.close();
response.getOutputStream().println(content);
}
}
4.重新写dao层
在bean包里:
1.首先定义一个bean的基类BaseBean 不声明方法和变量
2.然后让所有映射表的类都继承basebean
3.在里面定义变量和setter getter
在dao包里:
1.首先定义一个BaseDao类
代码如下:
package DAO;
import java.util.List;
import java.util.Map;
import javax.transaction.Transaction;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import Bean.BaseBean;
import Bean.Budget;
public class BaseDao {
protected SessionFactory sessionFactory;
public BaseDao() {
// TODO Auto-generated constructor stub
}
public void save(BaseBean bb){
sessionFactory.getCurrentSession().save(bb);
}
public void save(List<BaseBean> bbs){
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.save(bb);
}
}
public void delete(BaseBean bb){
sessionFactory.getCurrentSession().delete(bb);
}
public void delete(List<BaseBean> bbs){
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.delete(bb);
}
}
public void update(BaseBean bb){
sessionFactory.getCurrentSession().update(bb);
}
public void update(List<BaseBean> bbs){
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.update(bb);
}
}
public void saveOrUpdate(BaseBean bb){
sessionFactory.getCurrentSession().saveOrUpdate(bb);
}
public void saveOrUpdate(List<BaseBean> bbs){
//sessionFactory.getCurrentSession().saveOrUpdate(bb);
Session session = sessionFactory.getCurrentSession();
for (BaseBean bb:bbs){
session.saveOrUpdate(bb);
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
以上定义的方法可以在各个子类中通用
2.令所有表的到类继承basedao
以Budget_DAO 为例:代码如下
package DAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import Bean.BaseBean;
import Bean.Budget;
@Repository
@Transactional
public class Budget_DAO extends BaseDao{
protected List<Budget> selectAll(){
String hql ="from Budget";
return (List<Budget>)sessionFactory.getCurrentSession().createQuery(hql).list();
//return null;
}
public List<Budget> queryBudget(List<Map<String,Object>> params) {
//Configuration config = new Configuration();
//SessionFactory sessionfactory = config.configure().buildSessionFactory();
//Session session = sessionfactory.openSession();
//List<Budget> budgetResult = new ArrayList<Budget>();
StringBuilder sb = new StringBuilder();
sb.append("from Budget where 1 = 1");
if(params != null && params.size() > 0){
for(int i = 0 ; i < params.size() ; i++){
Map<String,Object> map = params.get(i);
Object value = map.get("value");
if(value instanceof String)value = "'"+value+"'";
sb.append(" and " + map.get("name") + " " +map.get("rela") + value);
}
}
Query query = sessionFactory.getCurrentSession().createQuery(sb.toString());
List <Budget> budgetResult = query.list();
// TODO Auto-generated method stub
return budgetResult;
}
public static void main(String args[])
{
ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");
Budget_DAO bd = (Budget_DAO) actx.getBean("budgetDao");
List <Map<String,Object>> params = new ArrayList<Map<String ,Object>>();
Map<String,Object> param = new HashMap<String, Object>();
param.put("name", "state");
param.put("rela", "=");
param.put("value", "w");
params.add(param);
List<Budget> l = bd.queryBudget(params);
//System.out.println(l.size());
for(BaseBean b :l){
System.out.println(b);
}
HibernateTransactionManager manager = (HibernateTransactionManager) actx.getBean("transactionManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
TransactionStatus status = manager.getTransaction(def); // 获得事务状态
Budget budget = new Budget();
budget.setBudg_id("999");
budget.setDate("1200-02-29");
budget.setDep_id("102");
budget.setMoney(9000);
budget.setState("通过");
budget.setType_id("01");
budget.setUser_id("10203040");
try{
//bd.delete(l);
for (Budget b:l){
bd.delete(b);
}
//bd.save(budget);
//bd.sessionFactory.getCurrentSession().getTransaction().commit();
manager.commit(status);
}catch(Exception e){
e.printStackTrace();
//manager.rollback(status);
System.out.println("erroe");
}
}
}
在父类中的save update , delete的返回均为void 因此可以通用
但是select有返回类型,最主要的是hql = "from Budget" 部分 不相同,因此,需要在每个子类中定义select方法,以及query方法