Spring4+SpringMVC4+Hibernate4整合

2.导包

1.springMVC包
2.hibernate基本包
3.日志包
4.mysql驱动包

3.web.xml配置

1.配置spring的IOC容器

    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    

2.配置springMVC的控制器(servlet)

    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml
        
        
        1
    
    
        dispatcherServlet
        
        /*
    
  1. 配置编码方式过滤器
    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    

    
        CharacterEncodingFilter
        /*
    

4 配置HiddenHttpMethodFilter(实现REST)

    
    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    

    
        HiddenHttpMethodFilter
        /*
    

4.配置springMVC配置文件

1.导入命名空间

2.配置扫描的包


    

3.配置视图解析器


    
        
        
        
    

4.配置静态资源和注解


    
    
    

5.配置spring

1.导入命名空间
2.配置自动扫描的包

    
    
        
        
    

3.配置数据源


    

4.配置dataSource

    
        
        
        
        
        
        
    

5.整合Hibernate
1)配置sessionFactory


        

        
        
            
                
                org.hibernate.dialect.MySQLInnoDBDialect
                true
                true
                update
            
        
    

2)配置hibernate事务管理器


    
        
    

测试与总结

1.新建test测试包

2.新建Junit测试类

public class TestSSH {
    private ApplicationContext ctx;

    @Test
    public void testDataSource() throws Exception {
        ctx = new ClassPathXmlApplicationContext("applictionContext.xml");
        System.out.println(ctx);
        System.out.println("--------------");
        //1.测试数据源
        DataSource ds = ctx.getBean(DataSource.class);
        System.out.println(ds);
        System.out.println(ds.getConnection().toString());
        //2.测试sessionFactory
        SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
        System.out.println(sessionFactory);

        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        User user = new User("111", "12345", "collin");
        session.save(user);

        tx.commit();
        session.close();

    }
}

本节使用的是注解开发,所以需要在实体类中添加注释,已完成部分功能。

@Entity
@Table(name="")
public class User implements Serializable{

    @Id
    @Generated()
    private int id;
    private String userId;
    private String pass;
    private String name;

    public User() {
        super();
    }

    public User(String userId, String pass, String name) {
        super();
        this.userId = userId;
        this.pass = pass;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

你可能感兴趣的:(Spring4+SpringMVC4+Hibernate4整合)