SSH整合方式二:不带hibernate配置文件
1.复制一个项目
2.hibernate配置文件中有哪些内容:
数据库连接的配置
Hibernate的相关的属性的配置
方言
显示SQL
格式化SQL
。。。
C3P0连接池
映射文件
3.将Hibernate的配置交给Spring
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0连接池 ==========-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- spring和hibernate的整合 -->
<!-- 引入hibernate的配置的信息========== -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 引入映射===== -->
<property name="mappingResources">
<list>
<value>com/wangshi/ssh/entity/Customer.hbm.xml</value>
</list>
</property>
</bean>
4.删除hibernate.cfg.xml文件
5.配置jdbc.properties
最后测试,如果有问题,重启软件…
Hibernate模板的使用
Hibernate模板的常用的方法:
-
保存操作
save(Object obj); -
修改操作
update(Object obj); -
删除操作
delete(Object obj); -
查询操作
查询一个 get(Class c,Serializable id); load(Class c,Serializable id); 查询多个 List find(String hql,Object… args); List findByCriteria(DetachedCriteria dc); List findByCriteria(DetachedCriteria dc,int firstResult,int maxResults); List findByNamedQuery(String name,Object… args);
延迟加载问题的解决
Spring提供了延迟加载的解决方案:
OpenSessionInViewFilter:提供过滤器[在web中加入过滤器]
在SSH整合开发中哪些地方会出现延迟加载?
使用load方法查询某一个对象的时候(不常用)
查询到某个对象以后,显示其关联对象。
代码演示:
package com.wangshi.ssh.dao.Impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.wangshi.ssh.dao.CustomerDao;
import com.wangshi.ssh.entity.Customer;
/**
* @author wanghaichuan
*这是客户管理dao层的实现
*Hibernate模板的常用的方法:
*/
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Override
public void save(Customer customer) {
System.out.println("这是dao层中的save方法.......");
//使用模板完成操作
this.getHibernateTemplate().save(customer);
}
@Override
public void update(Customer customer) {
this.getHibernateTemplate().update(customer);
}
@Override
public void delete(Customer customer) {
this.getHibernateTemplate().delete(customer);
}
@Override
public Customer findById(Long cust_id) {
//return this.getHibernateTemplate().load(Customer.class, cust_id);
return this.getHibernateTemplate().get(Customer.class, cust_id);
}
@Override
public List<Customer> findAllByHQL() {
List<Customer> list = (List<Customer>) this.getHibernateTemplate().find( "from Customer");
return list;
}
@Override
public List<Customer> findAllByQBC() {
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
@SuppressWarnings("unchecked")
List <Customer>list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
return list;
}
@Override
public List<Customer> findAllByNamedQuery() {
@SuppressWarnings("unchecked")
List<Customer>list = (List<Customer>) this.getHibernateTemplate().findByNamedQuery("queryAll");
return list;
}
}
测试:
package com.wangshi.ssh.text;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.wangshi.ssh.entity.Customer;
import com.wangshi.ssh.service.CustomerService;
/**
* @author wanghaichan
*hibernate的模板测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SSHTest {
@Resource(name="customerService")
private CustomerService customerService;
@Test
//修改
public void demo(){
Customer customer = customerService.findById(1l);
customer.setCust_name("武当");
customerService.update(customer);
}
@Test
//删除
public void demo1(){
Customer customer = customerService.findById(4l);
customerService.delete(customer);
}
@Test
//查询 HQL
public void demo2(){
List<Customer> list = customerService.findAllByHQL();
for (Customer customer : list) {
System.out.println(customer);
}
}
@Test
//查询 QBC
public void demo3(){
List <Customer>list = customerService.findAllByQBC();
for (Customer customer : list) {
System.out.println(customer);
}
}
@Test
// 查询所有:命名查询
public void demo4(){
List<Customer> list = customerService.findAllByNamedQuery();
for (Customer customer : list) {
System.out.println(customer);
}
}
}
注意要插入: