📄 usersimp.java
字号:
package com.accp.t29.module.imp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import com.accp.t29.hibernate.cfg.HibernateSessionFactory;
import com.accp.t29.hibernate.pojo.UsersTb;
import com.accp.t29.module.service.UsersService;
public class UsersImp implements UsersService {
public boolean addUsers(UsersTb users) {
// TODO 自动生成方法存根
boolean flag = false;
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
if(session.save(users)!=null)
{
flag = true;
tx.commit();
}
} catch (HibernateException e) {
// TODO 自动生成 catch 块
if(tx!=null)
tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
return flag;
}
public List<UsersTb> findUsersByAll(int offset, int pageSize) {
// TODO 自动生成方法存根
List<UsersTb> usersList = null;
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
usersList = session.createQuery("from UsersTb").setFirstResult(offset).setMaxResults(pageSize).list();
tx.commit();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
if(tx!=null)
tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
return usersList;
}
public UsersTb findUsersByID(int id) {
// TODO 自动生成方法存根
UsersTb users = null;
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
users = (UsersTb)session.createQuery("from UsersTb where id=:id").setInteger("id",id).uniqueResult();
tx.commit();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
if(tx != null)
tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
return users;
}
public boolean removeUsers(int[] ids) {
// TODO 自动生成方法存根
boolean flag = false;
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Connection conn = session.connection();
PreparedStatement pst = conn
.prepareStatement("delete from Users_tb where id=?");
for (int i = 0; i < ids.length; i++) {
pst.setInt(1, ids[i]);
pst.addBatch();
}
if (pst.executeBatch().length == ids.length) {
flag = true;
}
tx.commit();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
if (tx != null)
tx.rollback();
e.printStackTrace(System.out);
} catch (SQLException e) {
// TODO 自动生成 catch 块
if (session != null)
session.close();
e.printStackTrace(System.out);
}
return flag;
}
public boolean updateUsers(UsersTb users) {
// TODO 自动生成方法存根
boolean flag = false;
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(session.get(UsersTb.class, users.getId()));
tx.commit();
flag = true;
} catch (HibernateException e) {
// TODO 自动生成 catch 块
if(tx!=null)
tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
return flag;
}
public int getTotalCount()
{
int total = 0;
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
total = Integer.parseInt(session.createCriteria(UsersTb.class).setProjection(Projections.count("id")).uniqueResult().toString());
tx.commit();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
if(tx!=null)
tx.rollback();
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
session.close();
}
return total;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -