SSM基础整合
Zero. 创建Maven项目, pom依赖:
1 |
|
1. ApplicationContext.xml
- 新建db.properties,写数据库配置需要的数据
- 加载db.properties,class为:PreferencesPlaceholderConfigurer,属性locations下用array,value,classpath引用db.properties
- datasource,class为:BasicDatasource,property下配置数据库链接信息
- sqlSessionFactory,Class为:SqlSessionFactroyBean,属性需要引用datasource和mapperlocations值为classpath:*.xml加载mybatis的mapperxml
- mappers批量生成mapper接口的对象,class为:MapperScannerConfigurer,注意属性basePackage的值用value,而不是ref
- 开启注解扫描,component-scan,并且使用排除Controller注解(annotation),(org.springframework.stereotype.Controller)
- 配置事务管理,DataSourceTransactionManager,其属性中引入datasource,开启事务注解支持:<tx:annotation-driven,空格后配置transaction-manager属性将上面bean的id值输入
- 代码:
db.properties
1 | driver=com.mysql.jdbc.Driver |
log4j.properties
1 | # Set root category priority to INFO and its only appender to CONSOLE. |
ApplicationContext.xml
1 |
|
2. mpper.xml
- 配置mybaitis的mpper.xml
- 代码:
MapperXml/VocaloidMapper.xml(放置在resources/MapperXml包下)
1 |
|
3. 建立数据库表的对应类
- 生成set、get方法,构造器,重写toString
- 代码:
com.SH.bean.Vocaloid
1 | package com.SH.bean; |
3. Mapper接口
- 建立mapper.xml对应的接口
- 代码:
com.SH.mapper.VocaloidMapper
1 | package com.SH.mapper; |
4. springMVC.xml
- 开启mvc注解支持,annotation-driven
- 开启注解扫描component-scan,使用base-package只扫描Controller包
- 配置视图解析器InternalResourceViewResolver,prefix前缀,suffix后缀
- 代码:
springMVC.xml
1 |
|
5. web.xml
- 配置spring,使用listener,class为ContextLoaderListener,再使用Context-Param指定spring配置文件的位置
- 配置springMVC,使用servlet标签,class为DispatcherServlet,使用init-param初始化参数,使用load-on-start配置随服务器加载,最后再servlet-mapping设置需要拦截的请求,如*.action
- 配置解决中文的过滤器(filter),class为:CharacterEncodingFilter,使用init-param初始化参数(参数名为encoding,值为utf-8) ,在filter-mapping里拦截所有请求(/*)
- classPath的使用
- 代码:
web.xml
1 |
|