Inject Bean
packagecn.xiaojf;Importcn.xiaojf.today.data.rdb.repository.RdbCommonRepositoryImpl;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.context.annotation.Bean;Importorg.springframework.data.jpa.repository.config.EnableJpaRepositories;Importorg.springframework.orm.jpa.vendor.hibernatejpasessionfactorybean;@ Springbootapplication@enablejparepositories (repositorybaseclass= Rdbcommonrepositoryimpl.class) public classApplication { public Static voidmain (string[] Args) {springapplication.run (application.class, args); } @Bean public hibernatejpasessionfactorybean sessionfactory () { return new Hibernatejpasessionfactorybean (); }}
Configuration in Application.properties
Spring.jpa.show-sql = Truespring.jpa.hibernate.ddl-auto=nonespring.jpa.database=mysql spring.jpa.properties.hibernate.current_session_context_class= Org.springframework.orm.hibernate4.SpringSessionContext
Inject directly into your code
@Autowired Private Sessionfactory sessionfactory;
Example
Package cn.xiaojf.today.base.service.impl;
Import cn.xiaojf.today.base.constant.SystemConstant;
Import cn.xiaojf.today.base.service.EntityService;
Import cn.xiaojf.today.base.util.Asserts;
Import cn.xiaojf.today.base.util.BeanUtil;
Import cn.xiaojf.today.base.util.DaoUtil;
Import cn.xiaojf.today.base.util.StringUtil;
Import cn.xiaojf.today.data.rdb.entity.AbstractEntity;
Import cn.xiaojf.today.data.rdb.entity.BaseEntity;
Import cn.xiaojf.today.data.rdb.util.EntityUtil;
Import org.apache.commons.beanutils.BeanUtils;
Import org.apache.commons.lang.StringUtils;
Import org.hibernate.Criteria;
Import org.hibernate.Session;
Import org.hibernate.SessionFactory;
Import org.hibernate.criterion.Example;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Service;
Import org.springframework.transaction.annotation.Transactional;
Import javax.validation.constraints.NotNull;
Import java.io.Serializable;
Import java.lang.reflect.InvocationTargetException;
Import java.util.ArrayList;
Import java.util.List;
/**
* @author XIAOJF 2017/2/24 11:06.
*/
@Service
@Transactional
public class Entityserviceimpl implements Entityservice {
@Autowired
Private Sessionfactory sessionfactory;
@Override
Public <t extends Baseentity> boolean checkfieldrepeat (T entity, String FieldName) throws exception{
String id = stringutil.tostring (beanutils.getproperty (entity, "id"));
Serializable filedvalue = stringutil.tostring (beanutils.getproperty (entity, fieldName));
BaseEntity baseentity = Entity.getclass (). newinstance ();
Beanutils.setproperty (baseentity, fieldName, filedvalue);
List List = This.findbyexample (baseentity);
If (list = null && list.size () > 0) {
Object o = List.get (0);
String Existid = Beanutils.getproperty (o, "id");
If (existid.equals (ID)) {
Return false;
} else {
Return true;
}
}
Return false;
}
@Override
Public <t extends baseentity> void Saveorupdate (T Entity) throws illegalaccessexception, nosuchmethodexception, invocationtargetexception {
String id = beanutils.getproperty (entity, systemconstant.table_id);
Session session = This.getsession ();
If (! Stringutils.isblank (ID)) {//update operation
T origion = this.get (entity.getclass (), id);
Beanutil.copypropertiesignorenull (entity, origion);
Daoutil.generatedefaultfield (origion); Append partial field Default value
Session.update (origion);
} else {//new
Daoutil.generatedefaultfield (entity); Append partial field Default value
Session.save (entity);
}
}
@Override
Public <t extends baseentity> void Saveorupdate (list<t> List) throws Exception {
Asserts.notnull (list);
For (T T:list) {
GetSession (). saveorupdate (t);
}
}
@Override
public void Enable (Class clazz, list<string> Ids) throws Exception {
list<baseentity> list = Get (clazz, ids);
For (baseentity T:list) {
Beanutils.setproperty (t, "status", abstractentity.status_enabled);
}
Saveorupdate (list);
}
@Override
public void Disable (Class clazz, list<string> Ids) throws Exception {
list<baseentity> list = Get (clazz, ids);
For (baseentity T:list) {
Beanutils.setproperty (t, systemconstant.table_status, abstractentity.status_disabled);
}
Saveorupdate (list);
}
@Override
Public <t extends baseentity> T get (Class clazz, Serializable id) {
if (id = = Null) {
Return null;
}
Return (T) this.getsession (). get (clazz, id);
}
@Override
Public <t extends baseentity> list<t> get (Class clazz, list<string> Ids) {
Asserts.notnull (ids, "primary key cannot be null");
list<t> list = new Arraylist<t> ();
For (String Id:ids) {
T t = Get (clazz, id);
List.add (t);
}
Return list;
}
@Override
public void Logdelbyid (Class clazz, String Id) throws invocationtargetexception, illegalaccessexception, nosuchmethodexception {
Asserts.notnull (id);
Asserts.notnull (clazz);
baseentity entity = Get (clazz, stringutil.tostring (id));
If (entity! = Null) {
Beanutils.setproperty (entity, systemconstant.table_status, abstractentity.status_logdel);
}
This.saveorupdate (entity);
}
@Override
public void Logdelbyid (Class clazz, list<string> Ids) throws Exception {
list<baseentity> list = Get (clazz, ids);
For (baseentity T:list) {
Beanutils.setproperty (t, systemconstant.table_status, abstractentity.status_logdel);
}
Saveorupdate (list);
}
Private <t extends baseentity> list<t> findbyexample (@NotNull T Example) {
Session session = This.sessionFactory.openSession ();
criteria = Session.createcriteria (example.getclass ());
if (example! = null) {
criteria.add (example.create (Example));
}
List List = Criteria.list ();
session.close ();
return list;
}
/**
* Get Session
*
* @return Session
* @author xiaojf 2015-5-9 8:35:08
*/
Private Session getsession () {
return this.sessionFactory.getCurrentSession ();
}
}
Springboot Get Hibernate's Sessionfactory