整合SSH:1. 加jar包
2. spring: 创建applicationContext.xml 需要AOP标签,context扫包标签,tx标签
3. springmvc: 创建controller-servlet.xml 需要context扫包标签,mvc标签
4.配置web.xml
spring:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoad</listener-class>
</listener>
springmvc:
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.Dispac</servlet-class> :配置mvc加载器
</servlet>
<servlet-mapping>
<servlet-name>controller<servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<fifter>
<fifter-name>encoding</fifter-name>
<fifter-class>org.springframework.web.filter.characterEncoding</fifter-class>
<init-param>
<param-mame>encoding</patam-mame>
<param-value>utf-8</param-value>
</init-param>
</fifter>
<fifter-mapping>
<fifter-name>encoding</fifter-name>
<url-pattern>/</url-pattern>
</fifter-mapping>
5.配置好webxml 就可以加载到服务器 试运行!报错说明文件配置填写错误
6.配置applicationxml和controller-servlet.xml
applicationxml:
扫包(注解): <context:component-scan base-package=""></context:component-scan>
扫properties: <context:property-placeholder location=""/>
配置datesoure: <bean id="dataSource" class="" distory-method"">
<property name="" value=""></property>
<property name="" value=""></property>
<property name="" value=""></property>
<property name="" value=""></property>
</bean>
sessionFactory: <bean id="sessionFactory" class="">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<prop key="hibernate.dislect.MYSQLDialect"></prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</property>
<property name="packagesToScan">
<list>
<value>entity</value>
</list>
</property>
</bean>
TransactionManager:<bean id="transactionMnager" class="org.springframework.web......">
<property name="sessionFactory" ref="sessionFactory">
</bean>
注解事物: <tx:annotation-driver transaction-namager="transactionMnager" />
hibernate异常转换后处理bean: <bean class=""></bean>
controller-servlet:
扫包(注解): <context:component-scan base-package="controller"></context:component-scan>
<context:component-scan base-package="exceptionhandler"></context:component-scan>
使用注解: <mvc:annotation-driver></mvc:annotation-driver>
viewResolver: <bean class="....." >
view: <property name="viewClass" value="....." ></property>
<property name="prefix" value="/WEB-INF/jsp/" ></property>
<property name="suffix" value=".jsp" ></property>
</bean>
viewcontroller: <mvc:viewcontroller path="/" view-name="regist" />
静态资源访问: <mvc:resource location="/js/" mapping="/js/** />
配置完毕,下面是写代码需要注意的问题:
定义常量用接口
daoimpl需要注入sessionFactory,调用sessionFactory.getcurrentSession;
String hql= new StringBuffer()
.append("from uesr u ")
.append(" where u.username=:username ")
.append(" where u.password=:password ")
return (User)session.createQuery(hql)
.setString("")
.setString("")
.uniqueRusult();
1.Service层用注解调用UserDao 注意,这个是接口
2.业务层压不要忘记加@Transactional(propagation=PropagationREQUIRED,rollbackFor=Exception.class)
3.如果只是查询的话,在查询的方法上加@Transactional(readonly=true)
1.Stringmvc:@controller @RequestMapping("/user")
2.controller层调用service接口
3.redirect:"/user/showsecess"
4.@ExceptionHandler(value={UserNotFoundException.class,UserStateException.class})
public ModelAndView login(Exception e){
String viewName=null;
if(e instanceof UserNotFoundException){
vieeName="regist";
}
if(e instanceof UserStateException){
vieeName="login";
}
return new ModelAndView(viewName,"e",e)
}
spring和mybatis是怎么整合的?
扫包(注解): <context:component-scan base-package=""></context:component-scan>
扫properties: <context:property-placeholder location=""/>
配置datesoure: <bean id="dataSource" class="" distory-method"">
<property name="" value=""></property>
<property name="" value=""></property>
<property name="" value=""></property>
<property name="" value=""></property>
</bean>
sqlsessionFactory: <bean id="sqlsessionFactory" class="">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations">
<list>
<value>classPath:mapper/*.xml</value>
</list>
</property>
</bean>
扫包Dao: <bean class="mapper.....">
<property name="basepackage" value="dao">
</bean>
整合SSH框架配置文件
最新推荐文章于 2019-03-06 19:21:31 发布