设为首页 加入收藏

TOP

简单Spring+Struts2+Hibernate框架搭建(五)
2017-10-12 18:17:19 】 浏览:9837
Tags:简单 Spring Struts2 Hibernate 框架 搭建
> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置Spring的监听器,用于初始化ApplicationContext对象 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置Struts2的主过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
  • employee
    package com.ssh.model;
    
    import java.io.Serializable;
    
    public class Employee implements Serializable
    {
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = 5954986571237608774L;
    	private String id;
        private String code;
        private String name;
    
        public String getId()
        {
            return id;
        }
    
        public void setId(String id)
        {
            this.id = id;
        }
    
        public String getCode()
        {
            return code;
        }
    
        public void setCode(String code)
        {
            this.code = code;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
    }
  • Employee.hbm.xml
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.ssh.model">
    	<class name="Employee" table="EMPLOYEE">
    		<id name="id" column="ID">
    			<generator class="identity"/>
    		</id>
    		<property name="code" column="CODE"/>
    		<property name="name" column="NAME"/>
    		
        </class>
    </hibernate-mapping>
  • EmployeeDao
    package com.ssh.dao;
    
    import java.util.List;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import com.ssh.model.Employee;
    
    @Component
    public class EmployeeDao {
    
    	
    	@Autowired 
    	private SessionFactory sessionFactory;
    	
    	
    	public void addEmp(Employee emp){
    		Session session = sessionFactory.openSession();
    		session.beginTransaction();
    		if(emp.getId().equals("")||emp.getId()==null){
    			session.save(emp);
    		}else{
    			session.update(emp);	
    		}
    		
    		session.getTransaction().commit();
    		session.close();
    	}
    	
    	/**
    	 * findall 
    	 * @return
  • 首页 上一页 2 3 4 5 6 7 下一页 尾页 5/7/7
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    上一篇JAVA垃圾回收机制概要 下一篇关于Springmvc中include与Sitemes..

    最新文章

    热门文章

    Hot 文章

    Python

    C 语言

    C++基础

    大数据基础

    linux编程基础

    C/C++面试题目