赞
踩
使用场景:
在简单的大表单CRUD里面,并不一定需要非常灵活的sql或者hql,在非常多的表单和一堆字段面前,灵活反而成了积累。
Hibernate+泛型对象存储,反而成了简单粗暴,最快的解决方式。那么springboot/默认用的是data-jap,而data-Jap用的是EntityManagerFactory。
这让以前的BaseDao不能用了么?随后查了下API,发现了
jpa: hibernate: ddl-auto: none show-sql: true database-platform: org.hibernate.dialect.Oracle10gDialect database: oracle properties: hibernate: current_session_context_class: org.springframework.orm.hibernate4.SpringSessionContext然后是App启动类里面需要注入上面的那个bean
@Bean public HibernateJpaSessionFactoryBean sessionFactory() { return new HibernateJpaSessionFactoryBean(); }
最后也是比较重要的一点,以前SpringMVC里的环境里面配置事务,是用xml配置的,boot里面这么干净的代码
,如果再去配置XML,这是一个强迫症很难接受的。
@Transactional //这里直接简单粗暴加上spring的事务管理 @Repository("aBaseDao") public class ABaseDaoImpl implements ABaseDao {//your implemention
}//下面是测试代码import com.alibaba.fastjson.JSON; import com.xyz.dao.ABaseDao; import com.xyz.model.SysLogs; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.sql.Timestamp; import java.util.List; /** * Created by dell on 2017/10/6. */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class TestDao { @Resource private ABaseDao aBaseDao; @Test public void test_database(){ String sql = "select sysdate from dual"; Timestamp result = (Timestamp) aBaseDao.findBySQL(sql).uniqueResult(); System.out.println(result); List<SysLogs> list = aBaseDao.findAll(SysLogs.class); System.out.println(JSON.toJSONString(list)); } }//Hibernate: select sysdate from dual
//2017-10-06 22:51:21.0
//Hibernate: select syslogs0_.id as id1_0_, syslogs0_.logdate as logdate2_0_, syslogs0_.longmsg as longmsg3_0_, syslogs0_.message as message4_0_, syslogs0_.note as note5_0_, syslogs0_.part_id as part_id6_0_, syslogs0_.type as type7_0_, syslogs0_.xzbm as xzbm8_0_ from sys_logs syslogs0_
//[{"id":1,"logdate":1507171170000,"message":"123123123"},{"id":2,"message":"ceshi"}]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。