SSM-整合_02

web.xml中配置前端控制, 整合SpringMVC

  • 用于整合SpringMVC
  	<!-- 启动SpringMVC -->
  		<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  • 防止乱码配置
    <filter>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  		<init-param>
  			<param-name>encoding</param-name>
  			<param-value>UTF-8</param-value>
  		</init-param>
  		<init-param>
  			<param-name>forceEncoding</param-name>
  			<param-value>true</param-value>
  		</init-param>
    </filter>
    <filter-mapping>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
    </filter-mapping>

配置SpringMVC的配置xml

	<!-- 扫描Controller所在的包 -->
	<context:component-scan base-package="com.xian.controller"></context:component-scan>

	<!-- 注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>

	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

web.xml中配置ContextLoaderListener监听器, 整合Spring

 	<!-- 启动spring -->
 	<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

applicationContext.xml

spring配置及整合mybatis配置:

	<!-- 开启Spring的IOC基于注解的扫描 -->
   <context:component-scan base-package="com.xian.*"></context:component-scan>
   
   <!-- 读取db.properties -->
   <context:property-placeholder location="classpath:db.properties"/>
   
   <!--创建DateSource -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
   	  <property name="username" value="${db.username}"/>
         <property name="password" value="${db.password}"/>
         <property name="url" value="${db.url}"/>
         <property name="driverClassName" value="${db.driver}"/>
         <property name="maxActive" value="${db.maxActive}"></property>
   </bean>
   
   <!-- 创建一个SqlSessFactory对象 -->
   <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
   	<!-- 关联连接池 -->
   	<property name="dataSource" ref="dataSource"></property>
   	<!-- 加载SQL映射文件 -->
   	<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
   </bean>
   <!-- 配置Mapper接口的扫描 
   	每个Mapper接口在Spring容器中id名称为id的类名首字母小写
   -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   	<!-- 配置Mapper接口所在包的路径 -->
   	<property name="basePackage" value="com.xian.dao"></property>
   </bean>
   
   <!-- 开启Spring事务 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   	<property name="dataSource" ref="dataSource"></property>
   </bean>
   
   <!-- 启用事务注解 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>

Controller 与 Service

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@RequestMapping("/test")
	public String test() {
		return "test";
	}
	@RequestMapping("/save")
	public String save(Customer customer) {
		customerService.saveCustomer(customer);
		return "success";
	}	
}

@Transactional()
@Service
public class CustomerServiceImpl implements CustomerService {
	
	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public void saveCustomer(Customer customer) {
		customerMapper.save(customer);
		//模拟异常
		//int i = 100/0;
		customerMapper.save(customer);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值