• zbb20170223_spring_hibernate


    1、结构图

    2、class文件

    BaseController.java

    package com.zbb.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.zbb.entity.Event;
    import com.zbb.itf.iservice.IBaseService;
    
    @Controller
    @RequestMapping("/baseController")
    public class BaseController {
        @Autowired
        IBaseService iBaseService;
        @RequestMapping("/toLogin")
        public String toLogin() {
            return "login";
        }
        @ResponseBody
        @RequestMapping("/getAll")
        public String getAll(){
            List all = iBaseService.getAll(Event.class);
            for (Object object : all) {
                System.out.println((Event)object);
            }
            return all.toString();
        }
    }

    BaseService.java

    package com.zbb.cls.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.zbb.itf.idao.IBaseDao;
    import com.zbb.itf.iservice.IBaseService;
    @Service
    public class BaseService implements IBaseService{
        @Autowired
        IBaseDao iBaseDao;
        @Override
        public List getAll(Class cls) {
            // TODO Auto-generated method stub
            return iBaseDao.getAll(cls);
        }
        
    }

    IBaseService.java

    package com.zbb.itf.iservice;
    
    import java.util.List;
    
    public interface IBaseService {
    
        public List getAll(Class cls);
    }

    BaseDao.java

    package com.zbb.cls.dao;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.stereotype.Repository;
    
    import com.zbb.itf.idao.IBaseDao;
    @Repository
    public class BaseDao extends HibernateDaoSupport implements IBaseDao {
    
        @Override
        public List getAll(final Class cls) {
            // TODO Auto-generated method stub
            return super.getHibernateTemplate().executeFind(new HibernateCallback() {
    
                @Override
                public Object doInHibernate(Session session)
                        throws HibernateException, SQLException {
                    // TODO Auto-generated method stub
                    return session.createQuery("from " + cls.getSimpleName()).list();
                }
            });
        }
        @Autowired  
        public void setMySessionFactory(SessionFactory sessionFactory){  
            super.setSessionFactory(sessionFactory);  
        }
    }

    IBaseDao.java

    package com.zbb.itf.idao;
    
    import java.util.List;
    
    public interface IBaseDao {
        public List getAll(Class cls);
    }

    Event.java

    package com.zbb.entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    /**
     * Event entity. @author MyEclipse Persistence Tools
     */
    @Entity
    @Table(name = "event", catalog = "test")
    public class Event implements java.io.Serializable {
    
        // Fields
    
        private Long EId;
        private String ETitme;
        private String EDate;
    
        // Constructors
    
        /** default constructor */
        public Event() {
        }
    
        /** minimal constructor */
        public Event(Long EId) {
            this.EId = EId;
        }
    
        /** full constructor */
        public Event(Long EId, String ETitme, String EDate) {
            this.EId = EId;
            this.ETitme = ETitme;
            this.EDate = EDate;
        }
    
        // Property accessors
        @Id
        @Column(name = "e_id", unique = true, nullable = false)
        public Long getEId() {
            return this.EId;
        }
    
        public void setEId(Long EId) {
            this.EId = EId;
        }
    
        @Column(name = "e_titme")
        public String getETitme() {
            return this.ETitme;
        }
    
        public void setETitme(String ETitme) {
            this.ETitme = ETitme;
        }
    
        @Column(name = "e_date")
        public String getEDate() {
            return this.EDate;
        }
    
        public void setEDate(String EDate) {
            this.EDate = EDate;
        }
    
        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("Event [EId=");
            builder.append(EId);
            builder.append(", ETitme=");
            builder.append(ETitme);
            builder.append(", EDate=");
            builder.append(EDate);
            builder.append("]");
            return builder.toString();
        }
    
    }

    3、配置文件

    applicationContext.xml

    <?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"
        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-3.0.xsd">
    
        <!-- 启动注解支持 -->
        <context:annotation-config />
        <context:component-scan base-package="com.zbb"></context:component-scan>
        <!-- 视图解析 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver">
            </property>
            <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.MySQLDialect
                    </prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.use_sql_comments">false</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                    <value>com.zbb.entity.Event</value></list>
            </property></bean>
    </beans>

    log4j.properties

    log4j.rootCategory=INFO,stdout,R
    #stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=[BOSS] %d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
    #File
    log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.R.File=${catalina.home}\logs\boss\boss.log
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%d-[BOSS] %d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
    #Class
    log4j.logger.org.apache.commons=ERROR
    log4j.logger.org.displaytag=ERROR
    log4j.logger.org.springframework=INFO
    log4j.logger.org.apache.velocity=FATAL
    log4j.logger.org.hibernate=INFO
    log4j.logger.org.hibernate.SQL=DEBUG
    log4j.logger.com.zbb=DEBUG

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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_3_0.xsd">
        <display-name></display-name>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <error-page>
            <error-code>404</error-code>
            <location>/WEB-INF/jsp/errorInfo/ERROR_404.jsp</location>
        </error-page>
        <error-page>
            <error-code>500</error-code>
            <location>/WEB-INF/jsp/errorInfo//ERROR_500.jsp</location>
        </error-page>
    </web-app>

    jsp文件

    主要用于访问资源

    <a href="${pageContext.request.contextPath }/baseController/toLogin">toLogin</a>

  • 相关阅读:
    中国石油昆仑加油卡
    157 01 Android 零基础入门 03 Java常用工具类01 Java异常 01 异常介绍 02 异常内容简介
    156 01 Android 零基础入门 03 Java常用工具类01 Java异常 01 异常介绍 01 Java常用工具类简介
    155 01 Android 零基础入门 02 Java面向对象 07 Java多态 07 多态知识总结 01 多态总结
    154 01 Android 零基础入门 02 Java面向对象 07 Java多态 06 内部类 05 匿名内部类
    153 01 Android 零基础入门 02 Java面向对象 07 Java多态 06 内部类 04 方法内部类
    152 01 Android 零基础入门 02 Java面向对象 07 Java多态 06 内部类 03 静态内部类
    151 01 Android 零基础入门 02 Java面向对象 07 Java多态 06 内部类 02 成员内部类
    150 01 Android 零基础入门 02 Java面向对象 07 Java多态 06 内部类概述 01 内部类概述
    149 01 Android 零基础入门 02 Java面向对象 07 Java多态 05 接口(重点)07 接口的继承
  • 原文地址:https://www.cnblogs.com/super-admin/p/6436055.html
Copyright © 2020-2023  润新知