springmvc spring3.0 jpa2.0(eclipselink)

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       ">
 
 <!-- 配置eclipselink entityManagerFactory -->
   <bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" >
  <property name="persistenceUnitName" value="WebApplication2PU" />
                <!-- property name="loadTimeWeaver" ref="loadTimeWeaver" / -->
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    <property name="showSql" value="false" />
    <property name="generateDdl" value="false" />
   </bean>
  </property> 
                 <property name="jpaDialect">
                      <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
                </property>
 </bean>
<!-- 配置事务管理器 -->
 <bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory"
   ref="entityManagerFactory" />
               
 </bean>
    <!-- Needed so the @PersistenceUnit annotation is recognized -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<!-- 采用@Transaction注解的方式使用事务  -->
<tx:annotation-driven transaction-manager="transactionManager"/>  

     <!-- tx:annotation-driven mode="aspectj" />
     <context:load-time-weaver aspectj-weaving="on"/ -->
    <!-- Activates scanning of @Autowired -->
     <context:annotation-config/>

     <!-- Activates scanning of @Repository -->
     <context:component-scan base-package="com.stuke.DAO"/>
     <!-- need agent--> 
     <!--context:load-time-weaver weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" / -->
    <bean class="com.stuke.DAO.Impl.AdminTJpaController" id="admintJpa" />
</beans>

 <?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app>

  

!----------------------------------------------------------------------------------------------------------------------

 

!---------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
 
 <!-- Enables the Spring MVC @Controller programming model -->
 <annotation-driven />
 <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
       
 <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
 <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <beans:property name="prefix" value="/WEB-INF/views/" />
  <beans:property name="suffix" value=".jsp" />
 </beans:bean>
 
 <!-- Imports user-defined @Controller beans that process client requests -->
 <beans:import resource="controllers.xml" />
</beans:beans>

!---------------------------------------------------------------------------------------------------------

<?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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <!-- Scans within the base package of the application for @Components to configure as beans -->
 <context:component-scan base-package="com.stuke.action" />
 
</beans>
!--------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="AppPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.stuke.entity.WebConfig</class>
    <class>com.stuke.entity.AdminT</class>
    <class>com.stuke.entity.BigClass</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=dsbk"/>
      <property name="javax.persistence.jdbc.password" value="************"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
      <property name="javax.persistence.jdbc.user" value="dsbk"/>
    </properties>
  </persistence-unit>
</persistence>

!-----------------------------------------------------------------------------------------------------------------------

package com.stuke.DAO.Impl;

import com.stuke.DAO.IAdminTDAO;
import com.stuke.DAO.Impl.exceptions.NonexistentEntityException;
import com.stuke.DAO.Impl.exceptions.PreexistingEntityException;
import com.stuke.entity.AdminT;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;

import javax.persistence.Query;

import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Administrator
 */

@Repository 
@Transactional
public class AdminTJpaController implements Serializable,IAdminTDAO {
    private static final long serialVersionUID = 1L;
   
    @PersistenceContext
    private EntityManager em;


    public void create(AdminT adminT) throws PreexistingEntityException, Exception {
     
    }

    public void edit(AdminT adminT) throws NonexistentEntityException, Exception {
     
    }

    public void destroy(Integer id) throws NonexistentEntityException {
     
    }

    public List<AdminT> findAdminTEntities() {
        return findAdminTEntities(true, -1, -1);
    }

    public List<AdminT> findAdminTEntities(int maxResults, int firstResult) {
        return findAdminTEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings("unchecked")
    private List<AdminT> findAdminTEntities(boolean all, int maxResults, int firstResult) {
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(AdminT.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
   
  
    @Override
    public AdminT findAdminT(Integer id) {
            Query q=em.createNativeQuery("select * from admin_t",AdminT.class);
           /* System.out.println("111111111111");
            System.out.println(""+em.isOpen()+":"+em.toString());
            System.out.println(em.getEntityManagerFactory().getPersistenceUnitUtil().toString());
            System.out.println(em.getProperties().toString());
            System.out.println(q.getSingleResult().toString());
            */
            return (AdminT)q.getSingleResult();
    }
   
    @SuppressWarnings("unchecked")
    public int getAdminTCount() {
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<AdminT> rt = cq.from(AdminT.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
!------------------------------------------------------------------------------------------------

 */

package com.stuke.DAO;

import com.stuke.entity.AdminT;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Administrator
 */
@Repository
public interface IAdminTDAO {
      public AdminT findAdminT(Integer id);
}
!--------------------------------------------------------------------------------

package com.stuke.action;

import com.stuke.DAO.IAdminTDAO;
import com.stuke.entity.AdminT;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;

/**
 *
 * @author Administrator
 */
@Controller
@RequestMapping(value="/liruiadmin") 
public class AdminTAction {
    @Autowired
    @Qualifier("admintJpa")
    private IAdminTDAO iatDAO;
    /**
     * 登陆
     * @return ModelAndView
     */
    //
    @RequestMapping(value="login",method=RequestMethod.GET)
    public ModelAndView login(AdminT a,Map<String,Object> model){
          ModelAndView av=new ModelAndView();
          model.put("message","dsd");
          av.addAllObjects(model);
          a=iatDAO.findAdminT(4);
           model.put("admint", a);
          return  av;
    }
   
    @RequestMapping(value="login",method=RequestMethod.POST)
    public String loginDo(AdminT a,HttpSession session){
        //
        session.setAttribute("admin",a.getAdminName());
        return "redirect:main.htm";
    }
   
}


图片

图片

你可能感兴趣的:(spring,mvc,bean,servlet,jpa)