Spring整合WEB项目

前面我们提到,整合struts2和spring时,在Web层的action里创建工厂,获取业务层的类,调用业务层的service,但是,这会导致一个问题就是,由于struts2是单例模式,每次请求都会加载配置文件,都会创建一个工厂,频繁的创建资源会降低整体效率性能和内存问题。如下所示:
这里写图片描述
实际上我们只需加载一次配置文件就可以。下面我们来介绍关于Spring的整合。
一,引入Spring-web.jar包
网盘链接: https://pan.baidu.com/s/1CN6mnyewjYYUiA-54BzljA 密码: pv7x
二,配置监听器
步骤一导入的包已经实现了监听器,我们只需要配置好就ok。
这里写图片描述
具体配置如下:在web.xml里增加监听器

<!--配置web整合的监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

三,最后我们需要改写action的内容,从ServletContext里获取工厂

WebApplicationContext applicationContext =WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());

可以看到,整合之后,没有出现加载多次配置文件的情况,加载applicationcontext.xml只加载了一次,如下所示
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以给您提供以下步骤: 1. 在pom.xml中添加spring和mybatis的依赖 ``` <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> ``` 2. 配置数据源和SqlSessionFactory ``` @Configuration public class DataSourceConfig { @Bean public DataSource dataSource() { // 配置数据源 return new DriverManagerDataSource("jdbc:mysql://localhost:3306/test", "root", "123456"); } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); // 配置mapper文件路径 sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")); return sqlSessionFactoryBean; } } ``` 3. 配置MapperScannerConfigurer ``` @Configuration public class MybatisConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("com.example.mapper"); return mapperScannerConfigurer; } } ``` 4. 编写Mapper接口和对应的Mapper.xml文件 ``` public interface UserMapper { User selectUserById(Integer id); } <mapper namespace="com.example.mapper.UserMapper"> <select id="selectUserById" parameterType="java.lang.Integer" resultType="com.example.entity.User"> select * from user where id = #{id} </select> </mapper> ``` 5. 在Service中注入Mapper并使用 ``` @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Integer id) { return userMapper.selectUserById(id); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值