- 浏览: 174150 次
- 性别:
- 来自: 重庆
最新评论
-
2047699523:
java图片工具类源代码下载:http://www.zuida ...
java实现图片的打印 -
34176470:
Exception in thread "main& ...
java打印Excel -
lionios:
请问为什么一定要有PrintService service = ...
java实现图片的打印 -
javawang2012:
如果是java web树就好了
java自动生成目录树
今天突然感觉到自己学的东西,都快忘完了,而且可能以后会用到,所以看了看自己以前写的东西,回忆一下,若以后用到上手快
一接口
import java.util.List;
import tmc.domail.Favor;
import tmc.domail.Nation;
import tmc.domail.User;
public interface IUserService {
public User login(String name,String pwd);
public void create(User user);
public void del(int id);
public User findUserById(int id);
public List<User> findUsers();
public List<User> findUsers(int pageNo,int maxNo);
public void update(User user);
public List<Nation> nations();
public List<Favor> favors();
public Nation findNativeById(int id);
public void createUserFavor(int uid,Integer[] fid);
}
二:加载hibernate文件
package tmc.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
三:执行相应的数据库操作
package tmc.util;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import tmc.domail.Favor;
import tmc.domail.Nation;
import tmc.domail.User;
import tmc.domail.UserFavor;
public class UserServiceImp implements IUserService{
/**
* 添加用户
*/
public void create(User user) {
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
session.save(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 删除用户
*/
public void del(int id) {
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
User user = (User)session.get(User.class,id);
session.delete(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 根据id查看用户信息
*/
public User findUserById(int id) {
Session session = null;
User user = null;
try{
session = HibernateSessionFactory.getSession();
user = (User)session.get(User.class,id);
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return user;
}
/**
* 查看所有的用户信息
*/
public List<User> findUsers() {
Session session = null;
List<User> user = null;
try{
session = HibernateSessionFactory.getSession();
String hql = "from User";
Query query = session.createQuery(hql);
user = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return user;
}
/**
* 分页显示信息
*/
public List<User> findUsers(int pageNo,int maxNo) {
Session session = null;
List<User> user = null;
try{
session = HibernateSessionFactory.getSession();
String hql = "from User";
Query query = session.createQuery(hql);
user =query.setFirstResult(pageNo).setMaxResults(maxNo).list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return user;
}
/**
* 用户登录验证
*/
public User login(String username, String password) {
Session session = null;
try {
session = HibernateSessionFactory.getSession();
User users = (User)session.createQuery("from tmc.domail.User u where u.username = :name and u.password = :pwd")
.setString("name", username)
.setString("pwd", password)
.uniqueResult();
return users;
} catch (HibernateException e) {
e.printStackTrace();
return null;
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 修改用户信息
*/
public void update(User user) {
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
user = (User)session.get(User.class, user.getId());
session.update(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 查看所有的民族
*/
public List<Nation> nations(){
Session session = null;
List<Nation> nation = null;
try{
session = HibernateSessionFactory.getSession();
String hql ="from Nation";
Query query = session.createQuery(hql);
nation = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return nation;
}
/**
* 查询出所有的爱好
*/
public List<Favor> favors(){
Session session = null;
List<Favor> favor = null;
try{
session = HibernateSessionFactory.getSession();
String hql = "from Favor";
Query query = session.createQuery(hql);
favor = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return favor;
}
/**
* 创建三张表的对应关系,即用户表,爱好表,和中间表
* 本来是多对多的关系,变成了两张多对一的关系
* 用户表--》中间表
* 中间表--》爱好表
* @param uid
* @param fid
*/
public void createUserFavor(int uid,Integer[] fid){
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
User user = (User)session.get(User.class,uid);
for(int i=0;i<fid.length;i++){
Favor fa = (Favor)session.get(Favor.class, fid[i]);
UserFavor uf = new UserFavor();
uf.setUser(user);
uf.setFavors(fa);
session.save(uf);
}
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
public Nation findNativeById(int id) {
Session session = null;
//Transaction tx = null;
Nation nation = null;
try{
session = HibernateSessionFactory.getSession();
nation = (Nation)session.get(Nation.class,id);
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return nation;
}
}
一接口
import java.util.List;
import tmc.domail.Favor;
import tmc.domail.Nation;
import tmc.domail.User;
public interface IUserService {
public User login(String name,String pwd);
public void create(User user);
public void del(int id);
public User findUserById(int id);
public List<User> findUsers();
public List<User> findUsers(int pageNo,int maxNo);
public void update(User user);
public List<Nation> nations();
public List<Favor> favors();
public Nation findNativeById(int id);
public void createUserFavor(int uid,Integer[] fid);
}
二:加载hibernate文件
package tmc.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
三:执行相应的数据库操作
package tmc.util;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import tmc.domail.Favor;
import tmc.domail.Nation;
import tmc.domail.User;
import tmc.domail.UserFavor;
public class UserServiceImp implements IUserService{
/**
* 添加用户
*/
public void create(User user) {
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
session.save(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 删除用户
*/
public void del(int id) {
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
User user = (User)session.get(User.class,id);
session.delete(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 根据id查看用户信息
*/
public User findUserById(int id) {
Session session = null;
User user = null;
try{
session = HibernateSessionFactory.getSession();
user = (User)session.get(User.class,id);
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return user;
}
/**
* 查看所有的用户信息
*/
public List<User> findUsers() {
Session session = null;
List<User> user = null;
try{
session = HibernateSessionFactory.getSession();
String hql = "from User";
Query query = session.createQuery(hql);
user = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return user;
}
/**
* 分页显示信息
*/
public List<User> findUsers(int pageNo,int maxNo) {
Session session = null;
List<User> user = null;
try{
session = HibernateSessionFactory.getSession();
String hql = "from User";
Query query = session.createQuery(hql);
user =query.setFirstResult(pageNo).setMaxResults(maxNo).list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return user;
}
/**
* 用户登录验证
*/
public User login(String username, String password) {
Session session = null;
try {
session = HibernateSessionFactory.getSession();
User users = (User)session.createQuery("from tmc.domail.User u where u.username = :name and u.password = :pwd")
.setString("name", username)
.setString("pwd", password)
.uniqueResult();
return users;
} catch (HibernateException e) {
e.printStackTrace();
return null;
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 修改用户信息
*/
public void update(User user) {
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
user = (User)session.get(User.class, user.getId());
session.update(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
/**
* 查看所有的民族
*/
public List<Nation> nations(){
Session session = null;
List<Nation> nation = null;
try{
session = HibernateSessionFactory.getSession();
String hql ="from Nation";
Query query = session.createQuery(hql);
nation = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return nation;
}
/**
* 查询出所有的爱好
*/
public List<Favor> favors(){
Session session = null;
List<Favor> favor = null;
try{
session = HibernateSessionFactory.getSession();
String hql = "from Favor";
Query query = session.createQuery(hql);
favor = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return favor;
}
/**
* 创建三张表的对应关系,即用户表,爱好表,和中间表
* 本来是多对多的关系,变成了两张多对一的关系
* 用户表--》中间表
* 中间表--》爱好表
* @param uid
* @param fid
*/
public void createUserFavor(int uid,Integer[] fid){
Session session = null;
Transaction tx = null;
try{
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
User user = (User)session.get(User.class,uid);
for(int i=0;i<fid.length;i++){
Favor fa = (Favor)session.get(Favor.class, fid[i]);
UserFavor uf = new UserFavor();
uf.setUser(user);
uf.setFavors(fa);
session.save(uf);
}
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
public Nation findNativeById(int id) {
Session session = null;
//Transaction tx = null;
Nation nation = null;
try{
session = HibernateSessionFactory.getSession();
nation = (Nation)session.get(Nation.class,id);
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return nation;
}
}
发表评论
-
OpenBravo 在eclipse下项目建立
2012-09-28 11:12 1080OpenBravo 在eclipse下项目建立 介绍 Open ... -
Spring 3.0 基于Annotation 的依赖注入实现详解
2012-04-13 13:36 797今天在网上看到有关spri ... -
java定时操作的三种方式
2012-04-12 10:40 10201)java.util.Timer 这个方法应该是最常用的,不 ... -
把数据存在磁盘中
2011-10-18 11:08 1088一:数据封转成集合,保存在磁盘的临时文件中 /** * 写 ... -
java程序创建导入,导出,创建数据库
2011-08-23 15:45 2779在做项目的时候,往往会遇到数据库创建,备份,还原等功能,jav ... -
java自动生成目录树
2011-07-29 19:41 7515今天写了一天的目录树 ... -
java过滤图片
2011-07-17 13:23 2271有时候我们再写项目的时候需要对读取或保存文件进行过滤,便于安全 ... -
java实现Excel和图片的打印预览
2011-07-09 18:00 9025今天忙了一天,终于把打印预览的功能给实现了,因为昨天写了两个打 ... -
java打印Excel
2011-07-09 14:25 4261首先先下载jcom.jar和jcom.dll文件,把dll文件 ... -
java实现图片的打印
2011-07-08 17:40 18078打印图片,支持打印gif,jpg,png等格式图片的 pack ... -
刷新Jtable
2011-06-30 16:40 3018以前在做GUI开发的时候,往往会碰到Jtable的数据不能刷新 ... -
自定义画图
2011-06-29 15:43 1059近来在做绘图程序,对自定义绘图很模糊,一直想了很久,因为在项目 ... -
Tomcat环境变量配置
2011-06-17 10:39 16971.===> 进入bin目录下,双击startup.ba ... -
一个简单的工资支付系统
2011-06-13 15:13 1185帮同学做了一个工资支付系统,感觉功能还可以,基本上达到了要求, ... -
sqlserver驱动jar包
2011-04-30 12:18 1430近来在网上找了了sqlserver的驱动jar包感觉到很难,弄 ... -
文件下载
2011-04-29 11:44 887在android开发中,会经常用的文件下载的功能,比如有时候我 ... -
android简单的乘法运算
2011-04-27 09:44 1751最近在自学android,自己在学习的时候,做了一个简单的乘法 ... -
Android_SDK与Eclipse最新版开发环境搭建
2011-03-13 16:32 774android的安装步骤收集 -
java垃圾回收机制详解
2011-02-07 12:24 757今天在看java基础的时候 ... -
hibernate对数据库的操作
2011-01-16 21:14 1106以前听起hibernate总感觉是一个很奇怪的东西,不知道是用 ...
相关推荐
Hibernate复习题1含答案要点.doc
hibernate+spring的复习大纲,重点中的重点,包括选择题,填空题及常见题
全面讲解hibernate,缓存,配置, 以及状态
复习hibernate复习hibernate复习hibernate复习hibernate复习hibernate复习hibernate复习hibernate复习hibernate复习hibernate
hibernate达内五天复习笔记.pdf
如果想复习hibernate,了解hibernate新技术,这个真的很不错.也可以做为复习材料使用.里面介绍了很多hibernate常用的插件,hibernate配置文件,hibernate原理,hibernate缓存机制.你选它,真的没有错.
Hibernate练习题 Hibernate 练习题 考试 复习
j2ee框架Struts2HibernateSpring考试复习题库.pdf
本ppt是传智播客的教学ppt,对学习和复习Hibernate 基础有很大的帮助。
此资料为2009届燕山大学里仁学院 spring hibernate struts 三大框架期末考试重点 供下载参考
NULL 博文链接:https://sxpujs.iteye.com/blog/370852
J2EE考试复习试题_struts2_spring_hiberna,很好的学习资料哦
Struts2.0 + Spring + Hibernate + DWR 对于三大框架目前还在学习阶段,做得不好的地方欢迎来mail指正yukiceo@126.com,或http://blog.csdn.net/yukiceo 每天面对大量的知识,时间一长,容易造成某些知识点的生疏...
NULL 博文链接:https://sxpujs.iteye.com/blog/371163
韩顺平的hibernate录视频时,同时做了笔记,这个资料可以帮助看过那个视频的人课后复习。
J2EE考试复习试题-struts2-spring-hibernate(答案整合完毕).doc
NULL 博文链接:https://sxpujs.iteye.com/blog/370855
hibernate框架重点复习内容.ppt
总结的Hibernate全部知识点,方便复习和使用!希望对大家有帮助
整合最全的struts2_spring_hibernate