大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。
Hibernate配置二级缓存: — 使用EhCache
1.hibernate.cfg.xml中配置二级缓存
<hibernate-configuration> <session-factory> <!– 使用EHCache配置Hibernate二级缓存 –> <property name=”hibernate.cache.user_second_level_cache”>true</property> <property name=”hibernate.cache.provider_class”>org.hibernate.cache.EhCacheProvider</property> </session-factory> </hibernate-configuration>
2.在持久化类的映射文件里须要指定缓存的同步策略,关键代码:
— 产品信息字段配置信息 <hibernate-mapping> <class name=”com.mr.product.Product” table=”tab+product”> <cache usage=”read-only”> //指定缓存的同步策略 </hibernate-mapping>
3.在项目的classpath根文件夹下添�换成配置文件ehcache.xml,该文件可一直hibernate的zip包下的etc文件夹中找到。 缓存配置文件代码例如以下:
<ehcache> <defaultCache maxElementsInMemory=”10000″ eternal=”false” timeToIdleSeconds=”120″ timeToLiveSeconds=”120″ overflowToDisk=”true” /> </ehcache>
Hibernate的映射关系
双向多对一关联:product n <—> 1 factory
既能够通过主控方载入被控方,也能够通过被控方载入主控方 factory配置: //定义一对多映射 <set name=”products” inverse=”true”> <key column=”factoryid”/> <one-to-many class=”com.mr.product.Product”/> </set>
product配置: //定义多对一映射 <many-to-one name=”factory” class=”com.mr.factory.Factory”> <column name=”factoryid”> </many-to-one>
一对一外键关联 — 实际上就是多对一关联的一个特例而已,须要保证关联字段的唯一性, 在<many-to-one>元素中通过 unique属性就可实现关联字段的唯一性
配置一对一关联: People <—> IDcard People映射配置:一对一映射 <many-to-one name=”idcard” unique=”true”> <column name=”card_id”/> </many-to-one>
多对多关联: — 通过中间表 user user-role role id id id name user_id rolename role_id
User映射:
<set name=”roles” table=”table_user_role”> <key column=”user_id”></key> <many-to-many class=”com.mr.role.Role” column=”role_id”/> </set> Role映射:
<set name=”users” table=”table_user_role”> <key column=”role_id”></key> <many-to-many class=”com.mr.user.User” column=”user_id”/> </set>
级联操作: — 当主控方运行save、update、delete操作时,管理对象(被控方)是否进行同步操作, 在映射文件里通过对 cascade属性的设置决定是否对关联对象採用级联操作。 cascade级联操作參数设置: all 全部情况均採用级联操作 none 默认參数,全部情况下均不採用级联操作 save-update 在运行save-update方法时运行级联操作 delete 在运行delete方法时运行级联操作 eg:对于People — 设置级联删除,当删除People对象的时候,会级联删除关联的IDcard对象,即delete People时,会先有一条select的SQL,再有两条delet的SQL <one-to-one name=”idcard” calss=”com.mr.idcard.IDcard” cascade=”delete”></one-to-one>
HQL查询:
语法: select 对象.属性名 from 对象 where 过滤条件 group by 对象.属性名 having 分组条件 order by 对象.属性名
实体对象查询 from 对象 对象别名 where 条件 eg: Query q = session.createQuery(“from Employee emp”); emplist = q.list();
HQL參数绑定机制: 1.绑定?占位符 eg: Query q = session.createQuery(“from Employee emp where sex =?”); q.setParameter(0,”男”); emplist = q.list();
1.绑定:parameter占位符 eg: Query q = session.createQuery(“from Employee emp where sex =:sex”); q.setParameter(“sex”,”男”); emplist = q.list();
Spring : 1.使用BeanFactory管理bean ,在getBean方法之前,不会实例化对象 eg:装载bean: Resource resource = new ClassPathResource(“applicationContext.xml”); //装载配置文件 BeanFactory factory = new XmlBeanFactory(resource); Test test = (Test)factory.getBean(“test”); 2.ApplicationContext的应用: ApplicationContext扩展了BeanFactory的功能,加入�了如I18n,生命周期时间的公布监听等更强大的功能 ApplicationContext接口有3个实现类,能够实例化当中不论什么一个类来创建Spring的ApplicationContext容器。 1.ClassPathXmlApplicationContext — 从当前类路径中检索配置文件并装载它来创建容器的实例: eg: ApplicationContext context = new ClassPathXmlApplicationContext(String configLocation); 2.FileSystemXmlApplicationContext — 通过參数指定配置文件的位置,能够获取类路径之外的资源。 eg: ApplicationContext context = FileSystemXmlApplicationContext(String configLocation); 3.WebApplicationContext — Spring的Web应用容器,有两种方法能够在Servlet中使用WebApplicationContext 1.在Servlet的web.xml中配置Sping的ContextLoaderListener监听器 2.web.xml中,加入�一个Servlet,使用Spring的org.springframework.web.context.ContextLoaderServlet类 依赖注入的三种类型: 1.接口注入 2.setter注入 3.构造器注入
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/118882.html原文链接:https://javaforall.cn