Dowemo

Link: https://item. Taobao. Com/item. Htm? spm = a230r. 1.14.1.76 bf523wiso2k & amp; id = 547598359683 & amp; ns = amp; abbucket &.


As we use sprng mvc development projects, we usually use annotations to improve our development efficiency. Implement zero configuration. We're going to do a mvc configuration. This project is completely developed with annotations.

The previous build project imports the jar package no longer.

1, modify Web. Xml. The contents of the file are as follows:

[ html ] view .
  • <. xml version= "1. 0.". encoding= "utf-8."". ->?
  • < 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". >
  • < servlet>
  • < Servlet-name > springmvc</ Servlet-name >
  • < Servlet-class >
  • Org. Dispatcherservlet. Web. Servlet.
  • </ Servlet-class >
  • < Init-param >
  • < Param-name > contextConfigLocation</ Param-name >
  • < Param-value > /WEB-INF/hib-config. Xml,/WEB-INF/springmvc-servlet. Xml. </ Param-value >
  • </ Init-param >
  • < Load-on-startup > 1 </ Load-on-startup >
  • </ servlet>
  • < Servlet-mapping >
  • < Servlet-name > springmvc</ Servlet-name >
  • < Url-pattern > *. Do. </ Url-pattern >
  • </ Servlet-mapping >
  • </ Web-app >

2, Springmvc-servlet. Xml. The configuration is as follows:


<?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" 


 xsi:schemaLocation="http://www.springframework.org/schema/beans 


 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 


 http://www.springframework.org/schema/context 


 http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 



 <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> 


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



 <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> 


 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 



 <!--对模型视图名称的解析,即在模型视图名称添加前后缀 --> 


 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".jsp"/> 



</beans> 





3, Hib-config. Xml (. Configured springIntegration hibernate


<?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: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-2.5.xsd 


 http://www.springframework.org/schema/tx 


 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 


 http://www.springframework.org/schema/aop 


 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 


 http://www.springframework.org/schema/context 


 http://www.springframework.org/schema/context/spring-context-2.5.xsd 


"> 



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


 <!-- 支持aop注解 --> 


 <aop:aspectj-autoproxy/> 



 <bean id="dataSource" 


 class="org.apache.commons.dbcp.BasicDataSource"> 


 <property name="driverClassName" 


 value="com.mysql.jdbc.Driver"> 


 </property> 


 <property name="url" value="jdbc:mysql://localhost:3306/myhib"></property> 


 <property name="username" value="root"></property> 


 <property name="password" value="123456"></property> 


 </bean> 



 <bean id="sessionFactory" 


 class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 


 <property name="dataSource"> 


 <ref bean="dataSource"/> 


 </property> 


 <property name="hibernateProperties"> 


 <props> 


 <!-- key的名字前面都要加hibernate. --> 


 <prop key="hibernate.dialect"> 


 org.hibernate.dialect.MySQLDialect 


 </prop> 


 <prop key="hibernate.show_sql">true</prop> 


 <prop key="hibernate.hbm2ddl.auto">update</prop> 


 </props> 


 </property> 


 <property name="packagesToScan"> 


 <value>com.sxt.po</value> 


 </property> 


 </bean> 



 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 


 <property name="sessionFactory" ref="sessionFactory"></property> 


 </bean> 



 <!--配置一个JdbcTemplate实例--> 


 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 


 <property name="dataSource" ref="dataSource"/> 


 </bean> 



 <!-- 配置事务管理 --> 


 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 


 <property name="sessionFactory" ref="sessionFactory"></property> 


 </bean> 


 <tx:annotation-driven transaction-manager="txManager"/> 


 <aop:config> 


 <aop:pointcut expression="execution(public * com.sxt.service.impl.*.*(..))" id="businessService"/> 


 <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService"/> 


 </aop:config> 


 <tx:advice id="txAdvice" transaction-manager="txManager"> 


 <tx:attributes> 


 <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED"/> 


 <!-- get开头的方法不需要在事务中运行 。 


 有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的--> 


 <tx:method name="*"/> <!-- 其他方法在实务中运行 --> 


 </tx:attributes> 


 </tx:advice> 



</beans> 





view plai co

4, java bean class

[ ] view .
  • package Com. Sxt. Po.
  • import Javax, persistence, entity;
  • import Javax, persistence, generatedvalue;
  • import Javax, persistence, GenerationType;
  • import Javax, persistence, id;
  • @ entity
  • public class User %
  • @ id
  • @ generatedvalue ( strategy = GenerationType. Auto ).
  • private int A id;
  • private String;
  • private String pwd;
  • public String getPwd ( ) % 7b
  • return Pwd;
  • %.
  • public void SetPwd ( string pwd ) % 7b
  • thisPwd = pwd;
  • %.
  • public int Getid ( ) % 7b
  • return A id;
  • %.
  • public void Setid ( ) int Id )
  • this Id = id.
  • %.
  • public String getUname ( ) % 7b
  • return Uname;
  • %.
  • public void SetUname _ % _ _ 7b
  • thisBrad = brad;
  • %.
  • %.

5, dao class

[ ] view .
  • package Com. Sxt. Dao.
  • import Javax. Annotation. Resource.
  • import Org. Hibernate3. HibernateTemplate. Orm.
  • import Org. Spring framework. Stereotype. Repository.
  • import Com, sxt, po, user;
  • @ repository ( "userdao."". )
  • public class 7b userdao %
  • @ resource
  • private A HibernateTemplateHibernateTemplate;
  • public void Add ( user u ) %
  • System. Out. (. "userdao. Add ( )". );
  • HibernateTemplate. Save ( u ).
  • %.
  • public HibernateTemplategetHibernateTemplate ( ) %
  • return A HibernateTemplate;
  • %.
  • public void Sethibernatetemplate ( HibernateTemplateHibernateTemplate ) %
  • this HibernateTemplate = HibernateTemplate.
  • %.
  • %.


6, service class

[ ] view .
  • package Com. Sxt. Service.
  • import Javax. Annotation. Resource.
  • import Org. Spring framework. Stereotype. Service.
  • import Com, sxt, dao, userdao;
  • import Com, sxt, po, user;
  • @ service ( "userservice."". )
  • public class 7b userservice %
  • @ resource
  • private A userdao userdao;
  • public void Add ( string progid ) %
  • System. Out. (. "userservice. Add ( )". );
  • User u = new User ( );
  • U. SetUname ( brad ).
  • Userdao. Add ( u ).
  • %.
  • public Userdao getUserDao ( ) %
  • return A userdao;
  • %.
  • public void SetUserDao ( userdao userdao ) %
  • this Userdao = userdao.
  • %.
  • %.


7, control class

[ ] view .
  • package Com, sxt, web;
  • import Javax. Annotation. Resource.
  • import Org. Spring framework. Stereotype. Controller.
  • import Org. Framework. ModelMap. UI.
  • import Org. Spring framework. Web. Bind. Annotation. Requestmapping.
  • import Org. Spring framework. Web. Bind. Annotation. Requestparam.
  • import Org. Spring framework. Web. Bind. Annotation. SessionAttributes.
  • import Com, sxt, po, user;
  • import Com, sxt, service, userservice;
  • @ controller ( "usercontroller."". )
  • @ requestmapping ( "/user. Do.". )
  • public class 7b usercontroller %
  • @ resource
  • private A userservice userservice;
  • @ requestmapping ( params = "method = reg" )
  • public String reg ( string brad ) % 7b
  • System. Out. (. "HelloController. HandleRequest ( )". );
  • Userservice. Add ( brad ).
  • return "index."". ;
  • %.
  • public Userservice getUserService ( ) %
  • return A userservice;
  • %.
  • public void SetUserService ( userservice userservice ) %
  • this Userservice = userservice.
  • %.
  • %.


8, Web-inf Build up Index. Jsp. . Index. Jsp. The contents are as follows:


<body> 


 <h1>**********${params.uname}</h1> 


 <h1>**********${requestScope.u}</h1> 


 <h1>**********${requestScope.user}</h1> 


</body> 





All right, the spring mvc project based on spring3. 0 annotations has been completed. Less than configuring xml, however, it isn't clear for a friend who has no experience, creating no experienced friends first from configuration xml, and then learn.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs