javaweb常用工具类及配置文件备份
Javaweb常用工具类及配置文件备份
做一个代码备份,以后常用到的。
hibernate工具类备份
package com.dly.service;/* * hibernate获取session 的工具类 */import java.io.Serializable;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.Session;public final class HibernateUtil { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { return sessionFactory; } private HibernateUtil(){}static{ Configuration cfg=new Configuration(); cfg.configure();//配置文件名 sessionFactory=cfg.buildSessionFactory();}public static Session getSession(){ return sessionFactory.openSession(); }/** * 添加信息 * @param entity */public static void add(Object entity){ Session s=null; Transaction tx=null; try{ s=HibernateUtil.getSession(); tx=s.beginTransaction(); s.save(entity); tx.commit(); }finally{ if(s!=null){ s.close(); } }}/** * 更新信息 * @param entity */public static void update(Object entity){ Session s=null; Transaction tx=null; try{ s=HibernateUtil.getSession(); tx=s.beginTransaction(); s.update(entity); tx.commit(); }finally{ if(s!=null){ s.close(); } }}/** * 删除信息 * @param entity */public static void delete(Object entity){ Session s=null; Transaction tx=null; try{ s=HibernateUtil.getSession(); tx=s.beginTransaction(); s.delete(entity); tx.commit(); }finally{ if(s!=null){ s.close(); } }}/** * 根据id查询 * @param clazz * @param id * @return */public static Object get(Class clazz,Serializable id){ Session s=null; try{ s=HibernateUtil.getSession(); Object obj=s.get(clazz, id); return obj; }finally{ if(s!=null){ s.close(); } }}} |
操作数据库举例:
package com.dly.service;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import com.dly.entity.Users;public class UserDao { public static String queryByname(String name){ Session s=null; String result=null; //System.out.println("调用了"); try{ s=HibernateUtil.getSession(); String hql="from Users as user where user.username=:username";//里面User是类名,不是表名,为类名起别名user,查询的是对象 Query query=s.createQuery(hql); query.setString("username", name); Users u=(Users) query.uniqueResult();//确定只有一个数据,限制用户名唯一时使用 // System.out.println(u.getUsername()); if(u!=null){ result=u.getUsername(); System.out.println(result); } return result; }finally{ if(s!=null){ s.close(); } } } public static Users queryByusername(String username,String pwd){ Users user=null; Session s=null; try{ s=HibernateUtil.getSession(); String hql="from Users as users where users.username=:username";//里面User是类名,不是表名,为类名起别名user,查询的是对象 Query query=s.createQuery(hql); query.setString("username", username); List<Users>list =query.list(); for(Users users:list){ String u=users.getUsername(); String p=users.getPassword(); if(u.equals(username)&&p.equals(pwd)){ user=users; } } return user; }finally{ if(s!=null){ s.close(); } } } } |
servlet常用代码举例:
package com.dly.servlet;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.json.JSONArray;import com.dly.entity.Shop;import com.dly.service.BuyCarDao;import com.dly.service.HibernateUtil;@WebServlet("/BuyCarList")public class BuyCarList extends HttpServlet { private static final long serialVersionUID = 1L; // Class clazz=BuyCar.class; Class<Shop> clazz=Shop.class; List<Shop> shop=new ArrayList<Shop>(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); int userid=Integer.parseInt(request.getParameter("userid")); List<Integer> list=new ArrayList<Integer>(); list=BuyCarDao.querayByuserid(userid); Iterator<Integer> it=list.iterator(); List<Shop>shop=new ArrayList<Shop>(); while(it.hasNext()){ int shopid=(Integer) it.next(); shop.add((Shop) HibernateUtil.get(clazz, shopid)); } JSONArray shoplist=new JSONArray(shop); response.getWriter().println(shoplist.toString()); }} |
package com.dly.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.dly.entity.BuyCar;import com.dly.service.BuyCarDao;import com.dly.service.HibernateUtil;@WebServlet("/AddBuyCar")public class AddBuyCar extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); BuyCar buycar; int shopid = Integer.parseInt(request.getParameter("shopid")); int userid=Integer.parseInt(request.getParameter("userid")); BuyCar result =BuyCarDao.queryByshopid(shopid); if(result==null){ buycar=new BuyCar(); buycar.setShopid(shopid); buycar.setUserid(userid); HibernateUtil.add(buycar); response.getWriter().print("添加成功"); response.getWriter().flush(); }else{ response.getWriter().print("该商品已在购物车中"); response.getWriter().flush(); } }} |
hibernate配置文件编写
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"<hibernate-configuration> <session-factory> <!-- 映射文件 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/trade?useUnicode=true&characterEncoding=UTF-8</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="dialect"> org.hibernate.dialect.MySQLDialect</property> <mapping resource="com/dly/entity/Users.hbm.xml"/> </session-factory> </hibernate-configuration> |
Users.hbm.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" <hibernate-mapping package="com.dly.entity"> <class name="Users" table="users" > <!-- id值 --> <id name="userid" type="int" length="11"> <generator class="native"/> </id> <property name="username" type="string" length="32" column="username"></property> <property name="password" type="string" length="32" column="password"></property> <property name="school" type="string" length="64" column="school"></property> <property name="userphone" type="string" length="32" column="userphone"></property> </class> </hibernate-mapping> |
此代码正在完善中。。。。。。
javaweb常用工具类及配置文件备份的更多相关文章
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- PHP常用工具类
<?php namespace isslib\Util; use think\Config; /** * 常用工具类 * User: xaxiong * Date: 2016/12/19 * T ...
- 简单了解Spring中常用工具类_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...
- Maven基础&&Spring框架阶段常用工具类整理
常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- IOS开发--常用工具类收集整理(Objective-C)(持续更新)
前言:整理和收集了IOS项目开发常用的工具类,最后也给出了源码下载链接. 这些可复用的工具,一定会给你实际项目开发工作锦上添花,会给你带来大大的工作效率. 重复造轮子的事情,除却自我多练习编码之外,就 ...
- Apache Commons 常用工具类整理
其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包 public class ApacheCommonsT ...
- Android 常用工具类之SPUtil,可以修改默认sp文件的路径
参考: 1. 利用Java反射机制改变SharedPreferences存储路径 Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...
随机推荐
- SQL Server 地理数据库中的系统表
转自:http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#/na/002q00000080000000/ 地理数据库的系统表可以强制 ...
- bootstrap基本标签总结[转]
文件头: DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- [codevs1557]热浪
本题地址:http://www.luogu.org/problem/show?pid=1339 http://codevs.cn/problem/1557/ http://www.tyvj.cn/p/ ...
- Java笔记(十九)……多线程
概述 进程: 是一个正在执行中的程序 每一个进程执行都有一个执行顺序,该执行顺序是一个执行路径,或者叫一个控制单元 线程: 就是进程中的一个独立的控制单元,线程在控制着进程的执行 一个进程中至少有一个 ...
- Android webView 中loadData方法加载 带中文时出现乱码
WebView出现乱码用LoadData方法来解析html的,但是据说这是官方的一个BUG,不能用来解析中文. 采用loadDataWithBaseURL的方法,其中codeingType设置为utf ...
- HDU1075 - What Are You Talking About(Trie树)
题目大意 给定一些火星文单词以及对应的英语单词,然后给你一些火星文,要求你翻译成对应的英文 题解 第一次写Trie树! 把所有火星文单词插入到Trie树中,并且每个火星文单词结尾的节点记录相应英文单词 ...
- iOS开发:UIImageView常用操作
UIImageView,顾名思义,是用来放置图片的.使用Interface Builder设计界面时,当然可以直接将控件拖进去并设置相关属性,这就不说了,这里讲的是用代码. 1.创建一个UIImage ...
- SQLite数据库如何存储和读取二进制数据
SQLite数据库如何存储和读取二进制数据 1. 存储二进制数据 SQLite提供的绑定二进制参数接口函数为: int sqlite3_bind_blob(sqlite3_stmt*, int, co ...
- 【转】Android平台下利用zxing实现二维码开发
http://www.cnblogs.com/dolphin0520/p/3355728.html 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描 ...
- 删除已分配IP的静态IP地址池
如果静态IP地址池已经分配了IP,则无法直接将其静态IP地址池删除,会提示出错:“已经有IP被分配,需要先将其回收,再删除” 如下: 查看IP地址池: Get-SCStaticIPAddressPoo ...