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);  
          
    }  
      
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章