0

我英语很差,所以我只能描述一下esayliy这个问题。这是我在使用spring注解到spring mvc项目时发生的。我准备得到答案。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMvcDemo</display-name>
  <welcome-file-list>

    <welcome-file>NewFile.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>mvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>mvc</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>`

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


<context:component-scan base-package="/"></context:component-scan>

<!--  配置 Hibernate -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
  </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="annotatedClasses">
      <list>
        <value>pojo.Contact</value>
        </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
        show_sql=true
      </value>
    </property>
  </bean>
<bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 支持事务注解功能 -->

  <tx:annotation-driven/>

  <!-- 配置事务 -->
  <bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
</beans>

控制器类

@Controller("contactController")
@RequestMapping(value="/contact")
public class ContactController {
@Autowired
private ContactServiceDao contactservice;


@RequestMapping(value="/list")
public String listContacts(@ModelAttribute("contact")Contact contact,Model model)
{

Contact contact2=new Contact();
contact2.setPassword("123");contact2.setUsername("sc");
System.out.println(contact.getPassword());
List<Contact> contacts=contactservice.findContacts();
    if(contact2.getPassword().equals(contact.getPassword())&&contact2.getUsername().equals(contact.getUsername()))
    {
        return "success";
    }

return "false";
}

}

**

**

@Repository("contactdao")
public class ContactImpl implements ContactDao {

@Autowired
private SessionFactory sessionFactory;
    @Override
    @Transactional
    public List<Contact> findContacts() {

    return  (List<Contact>)sessionFactory.getCurrentSession().createQuery("from contact").list();

    }   
}

**

服务

**

@Service("contactservice")
@Transactional
public class ContactServiceImpl implements ContactServiceDao{
@Autowired
private ContactDao contactdao;



@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.SERIALIZABLE)





    @Override
    public List<Contact> findContacts() {
        return contactdao.findContacts();
    }



}
4

0 回答 0