ssm框架步骤

第一大步骤:web.xml步骤:

1、在动态的web工程中的web.xml中定义中央前端控制器(dispatherservet),alt+/选择dispatcherservlet,servlet-name写要配置的spring.xml;URL-pattern改为/(拦截所有)。
2、容器监听(contextloaderlistener),alt+/选择contextloaderlistener,param-value改为:classpath*:config/applicationContext.xml;(classpath是代表路径,取消就查到一个,不是所有,config是自定义的config文件夹路径,applicationContext.xml是创建的xml名字,)。
3、字符编码过滤器
filter下的filter-name写web的源代码的字符编码过滤器(encodingFilter),filter-class写源代码的字符编码过滤器路径,在这里面定义源代码的三个初始值,分别有encoding()值为UTF-8,forceRequestEncoding(True),forceResponseEncoding(True)(org.springframework.web.filter.CharacterEncodingFilter)
filter-mapping下的filter-name写字符编码过滤器(encodingFilter),url-pattern改为/*(所有)。
4、提示:查看源代码的步骤:Ctrl+shift+t,输入CharacterEncodingFilter,查看web.xml源代码必须有springweb的jar包;字符编码过滤器必须写在所有过滤器的前面。

第二大步骤:web中配置的spring.xml文件:

1、开启注解并扫描指定包
<context:component-scan base-package=“com.zhiyuan.frank.controller”></context:component-scan>
2、配置试图资源解析器
在bean中的class输入InternalResourceViewResolver然后alt+/,给他前缀(prefix)和后缀(suffix)的jsp路径。

第三大步骤:配置web中监听的applicationContext.xml

<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/user"></property>
	<property name="username" value="root"></property>
	<property name="password" value="root"></property>
</bean>

<!-- 事务管理,在之前的mybatis主配置中,事务管理是在数据源外面的,然后这里用ref引用数据源 -->
<bean id="transactionmanager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 工厂,写config的配置文件路径,映射文件的路径,交给工厂处理 -->
<bean id="sqlsession" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="configLocation" value="classpath:config/config.xml"></property>
	<property name="mapperLocations" value="classpath*:com/zhiyuan/frank/mapper/*.xml"></property>
</bean>

<!-- 扫描映射文件 -->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhiyuan.frank.mapper"></property>
</bean>

第四个步骤:mybatis主配置文件:

只配置别名,不写mapper映射文件,因为这里结合了spring,所以把它交给了spring来智能处理。

猜你喜欢

转载自blog.csdn.net/qq_40349553/article/details/84975447
今日推荐