歌者悦

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.hibernate实体数据类型可以与数据表字段类型不一致,尤其页面传过来数据一般为String型,最好定义为String类型。插入数据时需要转一下数据类型。

 

2.数据类型不一致时,插入数据需要注意。

楼主是想写个批量插入数据,而且不想用hibernate的save来操作,因为save来操作这里是很麻烦的,因为save只能保存一个对象,要是这个对象过多的话,而且每个对像的数据都有所不一样,用save就花大量的时间来处理对象了,所以还不如直接来操作数据库,所以会想到用insert into来解决这问题。不过hibernate hql不支持这种insert 就像上楼说的那样。
个人建议用事务来处理,用SQLQuery 原生态sql来写这里,就能解决你的问题了
SQLQuery query=session.createSQLQuery("insert into youtable (username,password,email) values(?,?,?)");

 

因为Hibernate的HQL语言是用于面向对象实现查询功能的,然而在插入操作中是不会牵涉任何查询动作的,所以HQL不能用于insert语句的插入操作,而select、update、delete语句都可以有可能的查询实现,比如:
select语句时标准的查询,这个就不用再说了
update语句:update 对象名 set.....where......
delete语句:delete from 对象名 where .....
看到了没有,update与delete牵涉到where的查询筛选过程,这个过程是需要采用HQL来实现的,然而insert永远牵涉不到查询筛选过程,所以Hibernate没有对插入做insert实现

 

3.hibernate

A:自增列 

<id name="id">

<column name="U_USER_ID" /><generator class="native" />

</id>

 

B:不需要插入

insert="false"

 

C:多对一

<many-to-one name="department" class="com.qkj.entity.Department" lazy="false">

            <column name="D_DEPARTMENT_ID" not-null="true" />

 </many-to-one>

 

实体定义

private Department department; // 部门编号 

 

赋值

Department department = departmentService.getDep(userdepID);

int i = userService.updateUser(user, department);

 

后台操作实体

Query query = session.createQuery("update User u set u.nickName = ?,u.subscribeEmail= ?,u.tel= ? ,u.department= ? where u.userName = ?");

query.setEntity(3, department);

 

G:一对多

<set name="subModel" inverse="false" cascade="all"  lazy="false" table="M_MODEL_2" order-by="showOrder asc">

            <key column="LEVEL1CODE" ></key>

            <one-to-many class="com.qkj.entity.M_Model_2"/>

 </set>

 

实体定义

private Set<M_Model_2> subModel = new HashSet<M_Model_2>();//子菜单list

 

前台取值

<ul>

<s:iterator value="subMenuList" id="menu2" status="st2">

<li><span id="menu2"><s:property value="#menu2.levelName"   /></span></li>

<ul>

<s:iterator value="#menu2.subModel" id="menu3" status="st3">

<li><a href='<s:property value="#menu3.actionName" />' target="content"><span id="menu2"><s:property value="#menu3.levelName" /></span></a></li>

<ul>

<s:iterator value="#menu3.subModel" id="menu4" status="st4">

<li><a href='<s:property value="#menu4.actionName" />' target="content"><span id="menu2"><s:property value="#menu4.levelName" /></span></a></li>

</s:iterator>

</ul>

</s:iterator>

</ul>

</s:iterator>

</ul>

4.前台传汉字,需要在servers里的server.xml填写如下编码。

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8" useBodyEncodingForURI="true"  />

 

5.统计在线用户:监听session的创建和销毁

A. 实现接口:HttpSessionListener

B.实现方法:sessionCreated,sessionDestroyed

C.web.xml

 <listener>  

 <listener-class>com.qkj.action.UserStatisticsAction</listener-class>  

 </listener>

实例

public class UserStatisticsAction extends ActionSupport implements HttpSessionListener {

private static int sessionCount = 0;

public void sessionCreated(HttpSessionEvent event) {

System.out.println("==================sessionCreated");

HttpSession session = event.getSession();

ServletContext application = session.getServletContext();

sessionCount++;

application.setAttribute("onlineCount", sessionCount);

}

 

public void sessionDestroyed(HttpSessionEvent event) {

System.out.println("==================sessionDestroyed");

HttpSession session = event.getSession();

ServletContext application = session.getServletContext();

if (sessionCount >= 1) {

sessionCount = sessionCount - 1;

}

application.setAttribute("onlineCount", sessionCount);

}

 

public String execute() {

return SUCCESS;

}

 

}

6.table下拉框,垂直下拉,水平隐藏

#statistics #tabdiv{

padding-top:5px;

height:278px;

width:100%;

overflow-y:auto;

overflow-x:hidden;

font-size:0.8em;

}

7.时间戳timestamp

new Timestamp(System.currentTimeMillis())

select rs.REPORT_NAME,ds.DS_NAME,rs.TIME,rs.USERNAME,dep.D_NAME from REPORT_STT rs,USER user,DEPARTMENT dep,DATA_SOURCE ds where rs.USERNAME=user.U_NAME and user.D_DEPARTMENT_ID=dep.D_DEPARTMENT_ID and ds.DS_CODE=rs.DS_CODE  and rs.DS_CODE=1  and user.D_DEPARTMENT_ID=18  and rtrim(char(year(rs.time)))||'-'||rtrim(char(month(rs.time)))||'-'||rtrim(char(day(rs.time))) >= '2015-12-3'  and   rtrim(char(year(rs.time)))||'-'||rtrim(char(month(rs.time)))||'-'||rtrim(char(day(rs.time))) <= '2015-12-8' order by rs.TIME desc

 

8.返回listjsp页面取得

<s:property value="%{reportStaticsList.get(#st.index)[0]}" />

9.导出数据到Excel

前台导出,火狐不支持,而且需要浏览器设置,选择后台jxl导出。

前台jslocation=url地址; public void exporExcel() throws IOException {

ServletOutputStream out = null;

Date date = new Date();   

        //format的格式可以任意   

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

// 下载文件名

String filename = "报表统计"+ sdf.format(date)+".xls";

// 对文件名作处理、避免中文乱码

filename = new String(filename.getBytes("gbk"), "iso8859-1");

// 获得response中的输出流

HttpServletResponse response = ServletActionContext.getResponse();

// 设置response相应属性,设置为下载

response.setContentType("application/x-msdownload");

response.setHeader("Content-Disposition", "attachment;filename="+ filename);

out = response.getOutputStream();

try {

// 打开文件

WritableWorkbook book = Workbook.createWorkbook(out);

// 生成名为“第一页”的工作表,参数0表示这是第一页

WritableSheet sheet = book.createSheet("报表统计 ", 0);

            //取得报表统计列表

getReportStatistics();

//格式化报表创建时间

String newdate="";

for (int i = 0; i < reportStaticsList.size(); i++) {

Object[] contentA = (Object[]) reportStaticsList.get(i);

for (int j = 0; j < contentA.length; j++) {

if (j == 2){

     newdate = sdf.format(contentA[j]); 

     sheet.addCell(new Label(j, i, newdate));

}else

 sheet.addCell(new Label(j, i, (String) contentA[j]));

}

}

// 写入数据并关闭文件

book.write();

book.close();

out.flush();

} catch (Exception e) {

e.printStackTrace();

}finally {

try {

if(out != null) {

out.close();

}

} catch(Exception e) {

} finally {

out = null;

}

}

 

}

 

10.打印指定区域

//打印指定区域,tableId打印区域div的id值
function printTable(tableId) {
//获取当前页的html代码
bdhtml=window.document.body.innerHTML;
//打印边框
$("#" + tableId + " table").css("border-collapse","collapse");
$("#" + tableId + " table td").css("border","1px solid #000");
$("#" + tableId + " table th").css("border","1px solid #000");
//打印区域
document.body.innerHTML=document.getElementById(tableId).innerHTML;
window.print();
//还原body内容
document.body.innerHTML=bdhtml;
}

11.Jquery+Ajax+json:火狐不支持js方式

   //根据部门类型联动部门名称

function getAjaxDep(obj, depIdS) {

var depKId = obj.value;

$.ajax({

     url: "DepartmentManage!getDepartmentSList.action?depKIdS="+depKId,

     async: false,

     dataType: "json",  

        success:function(data){ 

 $("#depIdS option").remove();

 $.each(data.jsonArray,function(index){     

 $.each(data.jsonArray[index],function(key,value){     

 $("#depIdS").append("<option value='"+key+"'>"+value+"</option>");

      }); 

  });

        },

        error:function () {

           location = "error.jsp";

        }

     });

}

后台java

// 用于取得部门集合

public void getDepartmentSList() throws IOException {

System.out.println("JqueryAjaxServlet :  begin");

 

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("application/x-json;charset=utf-8");

 

JSONObject json = new JSONObject();

JSONArray jsonArray = new JSONArray();

JSONObject member = new JSONObject();

 

try {

departmentList = departmentService.getDepListByDepK(depKIdS);

// 拼前台处室下拉列表

 

//下拉列表无数据

if (departmentList.size() == 0) {

member.put(0, "");

jsonArray.add(member);

}else{

member.put(0, "请选择");

for (int i = 0; i < departmentList.size(); i++) {

int index = ((Department) departmentList.get(i)).getId();

String value = ((Department) departmentList.get(i)).getName();

member.put(index, value);

}

jsonArray.add(member);

}

 

json.put("jsonArray", jsonArray);

pw = response.getWriter();

pw.print(json.toString());

//System.out.println(json.toString());

} catch (Exception ex) {

ex.printStackTrace();

} finally {

pw.close();

}

 

}

posted on 2015-12-14 13:23  歌者悦  阅读(186)  评论(0)    收藏  举报