项目整合所需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...