首页 > 代码库 > spring4集成hibernate4:xml方式
spring4集成hibernate4:xml方式
User.java
package com.lzj.www.model;
public class User {
国医一号 http://www.tdhzp.com
private Integer id;
private String name;
临沂批发网 http://www.shoudashou.com
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Java代码 保藏代码
UserServiceImpl.java
package com.lzj.www.service.impl;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import com.lzj.www.model.User;
import com.lzj.www.service.UserService;
public class UserServiceImpl extends HibernateDaoSupport implements UserService{
@Override
public void addUser(User user) {
this.getHibernateTemplate().save(user);
}
}
Xml代码 保藏代码
User.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Xml代码 保藏代码
hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Xml代码 保藏代码
applicationContext.xml
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
hibernate.jdbc.fetch_size=50
hibernate.jdbc.batch_size=50
hibernate.connection.autocommit=true
hibernate.connection.release_mode=auto
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
javax.persistence.validation.mode=none
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
Java代码 保藏代码
test.java
package com.lzj.www.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lzj.www.model.User;
import com.lzj.www.service.UserService;
public class test {
@Test
public void test01(){
// BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
// UserService userService = (UserService)factory.getBean("userService");
// userService.service("xiaoming");
// UserService userService = new UserServiceImpl();
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)factory.getBean("userService");
User user = new User();
user.setName("name");
userService.addUser(user);
}
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)factory.getBean("userService");
User user = new User();
user.setName("xiaoming");
userService.addUser(user);
}
}
spring4集成hibernate4:xml方式