1. 所需要Jar包.
//Spring3.0.1包
org.springframework.web-3.0.1 系列
//公共包
slf4j-api-1.5.6.jar slf4j-log4j12-1.5.6.jar log4j-1.2.13.jar
commons-logging-1.1.1.jar asm-3.1.jar cglib-2.2.jar
//mybatis與Spring的整合所需的包
mybatis-3.0.5.jar
aopalliance-1.0.jar
mybatis-spring-1.0.1.jar
mybatis-generator-core-1.3.1.jar(mybatis代碼生成器包)
//jdbc driven
mysql-connector-java-3.1.6-bin.jar
//JSR驗證-Hibernate validate 4.1
hibernate-validator-4.1.0.Final.jar
validation-api-1.0.0.GA.jar
//Spring Json 支持包
jackson-all-1.8.1.jar
2. web.xml配置
Servlet配置
org.springframework.web.servlet.DispatcherServlet
init-param配置servlet初始化文件.
以及servlet-mapping配置.
應用路徑配置
webAppRootKey
Log4j配置
Log4jConfigLocation
Log4jRefreshInterval
Spring上下文配置
contextConfigLocation
Spring字符集過濾器配置
org.springframework.web.filter.CharacterEncodingFilter
Spring監聽器配置
org.springframework.web.context.ContextLoaderListener
log4j監聽器配置
org.springframework.web.util.Log4jConfigListener
3. spring mvc - servlet.xml配置
啟動mvc注解驅動
<mvc:annotation-driven/>
組件scanner主要是自動去注入指定包里的對象
<context:component-scan
base-package="com.los.mvc.controller"/>
ViewResolver & View 映射關系
InternalResourceViewResolver 基於resource對jsp/jstl的支持
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
<!-- InternalResourceViewResolver viewClass默認值就是JstlView -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
自定義攔截器配置
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/json*"/>
<bean class="com.los.mvc.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
國際化配置
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message"></property>
</bean>
4. Spring上下文 -- applicationContext.xml 配置
支持注解
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<import resource="service.xml"/>
<import resource="dao.xml"/>
<import resource="orm.xml"/>
service.xml dao.xml 配置@service 和 @Repository
5. Mybatis3.0.5-Spring 整合 -- orm.xml
DataSource配置
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mvc"/>
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
注冊事務管理器(Mybatis將事務轉交給Spring來管理)
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
SqlSessionFactory配置(Mybatis核心是sqlSessionFactory來獲取orm處理對象, dataSource, mapperLocations配置mybaits自動生成的xml文件.就是注入映射關系.)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:/com/los/mvc/mapper/*.xml" />
</bean>
MapperScanner配置.自動去搜索mapper里的對象,並注入.
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.los.mvc.dao" />
</bean>
啟動Spring注解事務
<tx:annotation-driven/>
6. mybatis自動生成器配置 -- generatorConfig.xml
sqlMapGenerator sqlMpper.xml生成器
javaClientGenerator ModelDao生成器
javaModelGenerator Model生成器
com.los.util.MBG.java 運行會自動生成mybatis代碼.然后再配置orm.xml
7. Controller層配置
類注解
@Controller
@RequestMapping("/json")為訪問該層的路徑.
方法注解
@RequestMapping(method = RequestMethod.GET) 只有get方法才能訪問.
@ResponseBody 自動將返回的封裝成json,方法返回值必須是map<String,?>類型.
@RequestMapping(value="/doLogin") value=”doLogin”為訪問該方法的handler mapping
return "login/login";會通過ViewResolver找到對應的view
return "redirect:/user/toRegister.html";為spring-mvc的重定向.
@InitBinder()為綁定器,可以為request傳來的數據進行數據類型轉換.
數據自動驗證
方法中參數需要有后面的兩個(@Valid User user,BindingResult result).@Valid的支持標准是JSR,Hibernate Validate 4是對該標准比較好的實現.需要在Model類中配置驗證的注解.判斷驗證是否正確通過result.hasErrors()或者result.hasFieldErrors()來判斷,通過result.getAllErrors()或者result.getFieldErrors()來獲取Errors然后遍歷Errors獲取相關想要的信息,例如Error.getDeafaultMessage(),這個是獲取錯誤信息.具體的錯誤驗證機制還地在Model類中配置.
屬性注解
@Autowired 會為該屬性自動注入bean,默認方式是byType.也可以用@Resource這個注解默認是byName.
8. Service層配置.(業務層)
類注解
@Service 為@Component的子注解,分工更明細.
@Transactional 可以為該業務類聲明一個全類的事務.也可以將事務寫在方法上.根據不同的需要.
方法注解
@Transactional(readOnly = true)
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) 類的事務聲明,可以設置隔離級別和傳播屬性,以及要回滾的異常名或者異常類,不需要回滾的異常名或者異常類.異常通常拋出給controller層來處理
屬性注解
@Autowired @Resource
9. Repository層配置.(持久層DaoImpl)
類注解
@Repository 為@Component的子注解,意為持久層,分工更明細.一般不在這層處理事務.
10.Entry層配置(Model層)
類注解
@Entry
驗證注解,常用的有:
@NotEmpty
@NotNull
@Size(min=2,max=10,message=”xx必須在{min}和{max}之間”)
@Email
@DecimalMax
@AssertFalse @AssertTrue
@Null
@Valid
@URL(protocol=,host=, port=,regexp=, flags=)
一般情況下屬性或者方法可以放多個約束注解,hibernate validate會以隨機的順序去驗證這些約束.所以多個注解約束會有可能同一個屬性返回多個message.所以有時候需要只返回一條message,則需要使用驗證組Groups來達成.組別序列可以把一系列的組別按照一定的順序排列在一起,然后逐個驗證,只要有一個組別驗證失敗,就不繼續驗證剩余的組別。
@GroupSequence({User.class,GroupB.class,GroupC.class})驗證組的順序,約束里不指定group的為默認的User.class組.
約束組放在類前,User.class為默認的約束組,GroupB,GroupC為空的接口.寫在User外同個java文件下.
@NotEmpty(message="密碼不能為空")
@Size(min=4,max=20,message="密碼長度必須在{min}-{max}范圍內",groups = GroupB.class)
如果@NotEmpty驗證失敗了,就不會繼續驗證@Size
