Spring整合web项目

Spring整合web项目


UserDao.java
package com.cn.dao;

public class UserDao {
	public void add(){
		System.out.println("dao.........");
	}
}
UserService.java
package com.cn.service;

import com.cn.dao.UserDao;

public class UserService {
	private UserDao  userDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void add(){
		System.out.println("service.........");
	    userDao.add();
	}
}
UserAction.java
package com.cn.action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
	   System.out.println("action.........");
		ApplicationContext  context=
			new ClassPathXmlApplicationContext("bean1.xml");
		UserService   userService=(UserService) context.getBean("userService");
		userService.add();
		return NONE;
	}
}
Spring配置文件 bean1.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
	
	<bean id="userService"  class="com.cn.service.UserService">
	  <property name="userDao" ref="userDao"></property>
	  
	</bean>
	<bean id="userDao"  class="com.cn.dao.UserDao"></bean>
</beans>
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="demo1" extends="struts-default" namespace="/">
	  	<action name="userAction" class="com.cn.action.UserAction"></action>
	</package>
	
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Spring_day_web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 指定spring配置文件位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:bean1.xml</param-value>
  </context-param>
  <!-- struts拦截器配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
     <!-- 配置监听器 -->
      <listener>
         	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
	<!-- struts拦截器过滤路径 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

	

	

运行图







 1 演示问题
    (1)action调用service,service调用dao
      每次访问action时候,都会加载spring配置文件
2 解决方案
   (1)在服务器启动时候,创建对象加载文件。
    (2)底层使用监听器,Servicontext对象.


3  在spring里面不需要我们自己写代码实现,帮封装
    (1)封装了一个监听器,只需要配置监听器就可以了。
    (2)配置监听器之前做事情,导入sping整合web项目jar包
    



     (3) 指定加载spring配置文件位置(系统默认为/WEB-INF/applicationContext.xml)

   
所以我们要修改配置文件的位置


改完之后我们会看到,重启服务器,然后会看到



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值