Spring 整合hibernate

项目整合所需jar包

1.Spring所需要jar包
在这里插入图片描述
2.日志jar包
在这里插入图片描述
3.hibernate所需要jar包
在这里插入图片描述

配置xml文件

配置sessioFactoy

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	
    <context:annotation-config/>
    <context:component-scan base-package="com.login"/>
    <context:property-placeholder location="jdbc.properties"/>
   
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!--hibernate4和hibernate5的配置一样-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         <!-- 注入dbcp连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!-- 引入hibernate的映射文件-->
        <property name="packagesToScan">
            <list>
                <!--此处只写到包名-->
                <value>com.login.model</value>
            </list>
        </property>
         <!--配置hibernate的参数 -->
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.show_sql=true
                hibernate.format_sql=true
                hibernate.hbm2ddl.auto=update
                hibernate.current_session_context_class=thread
            </value>
        </property>
    </bean>
	
</beans>

测试Spring整合hibernate

1.model层,添加hibernate注解。

@Entity
public class User {

	private int id;
	private String password;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(){
		
	}
	

2.impl层
注入sessionFactory

@Component("u")
public class UserDAOImpl implements UserDAO{

	private SessionFactory sessionFactoy;
	
	public SessionFactory getSessionFactoy() {
		return sessionFactoy;
	}
	//Resource通过byName方式查找bean的id="sessionFacory"
	@Resource
	public void setSessionFactoy(SessionFactory sessionFactoy) {
		this.sessionFactoy = sessionFactoy;
	}
	@Override
	public void save(User user) {
		Session s =sessionFactoy.getCurrentSession();
		s.beginTransaction();
		s.save(user);
		s.getTransaction().commit();
		System.out.println("user add to mysql...");
	}

}

3.test

@Test
	public void testAdd() throws Exception {
		BeanFactory apc = new ClassPathXmlApplicationContext("beans.xml");
		UserService service = (UserService) apc.getBean("userService");
		User user = new User();
		user.setPassword("1231");
		service.add(user); 
	}

输出结果:

Hibernate: 
    insert 
    into
        User
        (password) 
    values
        (?)
user add to mysql...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值