现在的位置: 首页 > 综合 > 正文

smartclient 连接数据库

2013年08月30日 ⁄ 综合 ⁄ 共 8073字 ⁄ 字号 评论关闭
先学习了extjs框架,体会到了extjs复杂的代码,令人费解的语法,真不是盖得!在接触smartclient,个人觉得比较简单,从语法上来说也比较简单,至少容易理解。

咬咬牙竟然可以做出一个小demo。 哈哈。

js 代码 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="isomorphic" prefix="isomorphic" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <isomorphic:loadISC skin="Enterprise"/> </head> <body> <script type="text/javascript"> isc.DataSource.create({ ID:"countryDS", dataFormat:"xml", //数据格式 xml格式 recordXPath:"//country", //<country></country> fields:[ {name:"countryCode", title:"Code", primaryKey:true}, {name:"countryName", title:"Country"}, {name:"capital", title:"Capital"}, {name:"continent",title:"continent"} ], operationBindings:[ {operationType:"fetch", dataURL:"country/findInfo.action" }, {operationType:"add", dataURL:"country/insertInfo.action" }, {operationType:"update", dataURL:"country/updateInfo.action" }, {operationType:"remove", dataURL:"country/deleteInfo.action" } ] }); isc.ListGrid.create({ ID: "countryList", width:500, height:224, alternateRecordStyles:true, left:20, dataSource: countryDS, canEdit:true, fields:[ {name:"countryCode"}, {name:"countryName"}, {name:"capital"}, {name:"continent"} ], sortFieldNum: 0, dataPageSize: 50, autoFetchData:true }) isc.IButton.create({ ID:"testbutton", click:"countryList.startEditingNew()", title:" Add information", left:20,top:240,width:150 }) isc.IButton.create({ left:350, top:240, width:150, title:"Remove country (UK)", click: function () { if (countryList.getSelectedRecord()) { //判断是否选中了grid行记录 countryList.removeData(countryList.getSelectedRecord()) }else{ isc.say('請選擇要刪除的記錄') } } }); </script> </body> </html> package isc.java.Module; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="country") public class country { private String code; private String countryname; private String capital; private String continent; @Id @Column(name="code") public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Column(name="countryname") public String getCountryname() { return countryname; } public void setCountryname(String countryname) { this.countryname = countryname; } @Column(name="capital") public String getCapital() { return capital; } public void setCapital(String capital) { this.capital = capital; } @Column(name="continent") public String getContinent() { return continent; } public void setContinent(String continent) { this.continent = continent; } } package isc.java.Dao; import isc.java.Module.country; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; @Repository public class CountryDao { private HibernateTemplate hibernateTemplate; @SuppressWarnings("unused") @Autowired private void setHibernateTemplate (SessionFactory sessionFactory){ this.hibernateTemplate =new HibernateTemplate(sessionFactory); } @SuppressWarnings("unchecked") public List<country> findInfo(){ String hql="from country"; return this.hibernateTemplate.find(hql); } public void updateInfo(country country){ this.hibernateTemplate.update(country); } //根據code返回code對應的信息 public country findByCode(String code){ return this.hibernateTemplate.get(country.class, code); } public boolean delete (country countrys){ try{ this.hibernateTemplate.delete(countrys); return true; }catch(Exception e){ System.out.println("丫操作失敗!看來是人品問題,。"); //e.printStackTrace(); return false; } } public void insert(country countrys){ this.hibernateTemplate.save(countrys); } } package isc.java.Service; import isc.java.Dao.CountryDao; import isc.java.Module.country; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class CountryService { private CountryDao countryDao; @Autowired public void setCountryDao(CountryDao countryDao) { this.countryDao = countryDao; } @Transactional("smartclient") public List<country> findInfo( ){ return this.countryDao.findInfo(); } @Transactional("smartclient") public country findByCode(String code){ return this.countryDao.findByCode(code); } @Transactional("smartclient") public void updateInfo(country country){ this.countryDao.updateInfo(country); } @Transactional("smartclient") public boolean delete(country countrys){ return this.countryDao.delete(countrys); } @Transactional("smartclient") public void insert(country countrys){ this.countryDao.insert(countrys); } } package isc.java.Web; import isc.java.Module.country; import isc.java.Service.CountryService; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Iterator; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class CountryController { private CountryService countryService; @Autowired public void setCountryService(CountryService countryService) { this.countryService = countryService; } /*从数据库中提取数据,转换成smartclient能识别的格式,xml*/ @SuppressWarnings("unchecked") @RequestMapping(value="country/findInfo.action") public void findInfo(HttpServletResponse response){ System.out.println("JUST A TEST ABOUT SELECT "); List<country> list =this.countryService.findInfo(); ServletOutputStream sos=null; try { sos = response.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } StringBuffer sbu=new StringBuffer(); Iterator ita = (Iterator) list.iterator(); while(ita.hasNext()){ country countrys=(country) ita.next(); sbu.append("<country>"); sbu.append("<countryCode>"+countrys.getCode()+"</countryCode>"); sbu.append("<countryName>"+countrys.getCountryname()+"</countryName>"); sbu.append("<capital>"+countrys.getCapital()+"</capital>"); sbu.append("<continent>"+countrys.getContinent()+"</continent>"); sbu.append("</country>"); } try { sos.print("<?xml version='1.0' encoding='UTF-8'?><records>"+sbu.toString()+"</records>"); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping(value="country/updateInfo.action") public @ResponseBody Map<String,Object> updateInfo(HttpServletRequest request){ Map<String,Object>map=new HashMap<String,Object>(); System.out.println("測試程序:-----"+request.getParameter("countryCode")); country countrys=this.findByCode(request.getParameter("countryCode")); String countryname=request.getParameter("countryName"); String continent =request.getParameter("continent"); String capital=request.getParameter("capital"); countrys.setCountryname(countryname); countrys.setContinent(continent); countrys.setCapital(request.getParameter(capital)); this.countryService.updateInfo(countrys); return map; } public country findByCode(String code){ return this.countryService.findByCode(code); } @RequestMapping(value="country/deleteInfo.action") public @ResponseBody Map<String ,Object> detete (HttpServletRequest request){ Map<String,Object>map=new HashMap<String,Object>(); String code=request.getParameter("countryCode"); country countrys=this.countryService.findByCode(code); boolean b =this.countryService.delete(countrys); if(b){ System.out.println("恭喜 在數據庫中成功刪除此記錄!"); }else{ System.out.println("I am so sorry! 刪除失敗"); } return map; } @RequestMapping(value="country/insertInfo.action") public @ResponseBody Map<String,Object> insert(HttpServletRequest request){ Map<String,Object>map=new HashMap<String,Object>(); country countrys=new country(); countrys.setCode(request.getParameter("countryCode")); countrys.setCapital(request.getParameter("capital")); countrys.setContinent(request.getParameter("continent")); countrys.setCountryname(request.getParameter("countryName")); this.countryService.insert(countrys); return map; } }

抱歉!评论已关闭.