公司之前的旧项目,没看懂,这个里面的sessionFactory在哪里连接数据库和服务的,谁能指点下吗
package com.janus.dao.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.janus.dao.IUserDao;
import com.janus.model.User;
@Repository
public class UserDaoImpl implements IUserDao{
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession(){
return this.sessionFactory.openSession();
}
public User findUserByUsername(String username){
User user = new User();
Session session = getCurrentSession();
try{
Query query = session.createQuery("from User where loginName = :username").setParameter("username", username);
List<User> list = query.list();
if(list.size() > 0){
user = list.get(0);
}
}catch(Exception e){
e.printStackTrace();
}finally {
if(session.isOpen()){
session.close();
}
}
return user;
}
public User load(Long id) {
// TODO Auto-generated method stub
return null;
}
public User get(Long id) {
// TODO Auto-generated method stub
return null;
}
public List<User> findAll() {
// TODO Auto-generated method stub
return null;
}
public void persist(User entity) {
// TODO Auto-generated method stub
}
public Long save(User entity) {
// TODO Auto-generated method stub
return null;
}
//更新保存用户
public void saveOrUpdate(User user) {
Session session = getCurrentSession();
try {
//开启事务
session.beginTransaction();
//session执行
session.saveOrUpdate(user);
//事务提交
session.getTransaction().commit();
} finally {
if(session.isOpen()){
session.close();
}
}
}
public void delete(Long id) {
// TODO Auto-generated method stub
}
public void flush() {
// TODO Auto-generated method stub
}
@Override
public List<User> findUserPageByCondition(int pageSize, int pageNumber, String loginName, String name) {
List<User> list = new ArrayList<User>();
Session session = getCurrentSession();
try{
StringBuffer sb = new StringBuffer("from User where 1 = 1");
if(!StringUtils.isEmpty(loginName)){
sb.append(" and loginName = '" + loginName + "'");
}
if(!StringUtils.isEmpty(name)){
sb.append(" and name like '%" + name + "%'");
}
//System.out.println("sql condition:" + sb.toString());
//开启事务
session.beginTransaction();
Query query = session.createQuery(sb.toString());
if(pageNumber < 0){
pageNumber = 0;
}
if(pageSize <= 0){
pageSize = 10;
}
query.setFirstResult(pageNumber);//从第几条记录开始
query.setMaxResults(pageSize);//取出多少条数据
list = query.list();
//事务提交
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally {
if(session.isOpen()){
session.close();
}
}
return list;
}
@Override
public Long countUser(int status) {
Long totalAll = 0L;
Session session = getCurrentSession();
try{
String hql = "SELECT COUNT(*) FROM User";
session.beginTransaction();
Query query = session.createQuery(hql);
totalAll = (Long) query.uniqueResult();
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally {
if(session.isOpen()){
session.close();
}
}
return totalAll;
}
@Override
public int deleteUsersBatchByIds(int[] ids) {
int rst = 0;
Session session = getCurrentSession();
try{
StringBuffer sb = new StringBuffer("UPDATE User SET status = '0' Where id IN (");
String str = "";
for(int i = 0; i < ids.length; i++){
str += ids[i] + ",";
}
sb.append(str.substring(0, str.length()-1) + ")");
session.beginTransaction();
Query query = session.createQuery(sb.toString());
rst = query.executeUpdate();
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally {
if(session.isOpen()){
session.close();
}
}
return rst;
}
}