springmvc_ssm Spring、SpringMVC、MyBatis集成
2021-03-26 00:26
标签:location listener required configure mon inter prope 文件中 rri 三层架构中,控制层调用Service层,Service层调用Dao层,控制层会将Bean对象注入到SpringMVCIOC容器中,Service和Dao层会将Bean对象注入到SpringIOC容器中,而SpringMVCIOC容器的父容器正好是SpringIOC容器,子容器可以使用父容器的Bean对象,所以可以实现三大框架集成 SpringMVCIOC容器会自动将SpringIOC容器设置为自己的父容器。 环境准备:在pom文件中导入各种依赖 最好每一步集成完都先进行相应测试 控制器 springmvc.xml 集成Spring和SpringMVC,只需要创建SpringIOC容器即可,SpringMVCIOC容器会自动将SpringIOC容器设置为自己的父容器。 如果不做处理会导致事务处理失效 两种处理方式:spring.xml与springmvc.xml分别只扫描对应部分,注意要配置容器加载监听器;或者全交由子类容器springmvc.xml来扫描,注意要导入 spring.xml springmvc_ssm Spring、SpringMVC、MyBatis集成 标签:location listener required configure mon inter prope 文件中 rri 原文地址:https://www.cnblogs.com/21556guo/p/13761715.html集成流程理解
web.xml
xml version="1.0" encoding="UTF-8"?>
web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
servlet>
servlet-name>dispatcherServletservlet-name>
servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
init-param>
param-name>contextConfigLocationparam-name>
param-value>classpath:springmvc.xmlparam-value>
init-param>
servlet>
servlet-mapping>
servlet-name>dispatcherServletservlet-name>
url-pattern>/url-pattern>
servlet-mapping>
filter>
filter-name>characterEncodingFilterfilter-name>
filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
init-param>
param-name>encodingparam-name>
param-value>utf-8param-value>
init-param>
filter>
filter-mapping>
filter-name>characterEncodingFilterfilter-name>
url-pattern>/*url-pattern>
filter-mapping>
listener>
listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
context-param>
param-name>contextConfigLocationparam-name>
param-value>classpath:spring.xmlparam-value>
context-param>
web-app>
集成SpringMVC
package com.zl.controller;
import com.bean.Account;
import com.zl.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping("/find")
public String find() {
List list = accountService.findAll();
System.out.println(list);
return "success";
}
@RequestMapping("/add")
public String add(Account account) {
Integer i = accountService.addAccount(account);
System.out.println(i);
return "success";
}
}
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
context:component-scan base-package="com.zl" use-default-filters="false">
context:include-filter type="annotation" expression="org.springframework.stereotype.Controller">context:include-filter>
context:component-scan>
mvc:annotation-driven>mvc:annotation-driven>
bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
property name="prefix" value="/WEB-INF/pages/">property>
property name="suffix" value=".jsp">property>
bean>
mvc:default-servlet-handler>mvc:default-servlet-handler>
beans>
集成Spring
包扫描冲突解决
集成MyBatis
package com.zl.service;
import com.bean.Account;
import java.util.List;
public interface AccountService {
List findAll();
Integer addAccount(Account account);
}
package com.zl.service.impl;
import com.bean.Account;
import com.zl.mapper.AccountMapper;
import com.zl.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
//@Autowired
@Resource //减少与spring的耦合
private AccountMapper accountMapper;
@Override
public List findAll() {
System.out.println("com.zl.service.impl.AccountServiceImpl#findAll");
return accountMapper.findAll();
}
@Override
public Integer addAccount(Account account) {
System.out.println("com.zl.service.impl.AccountServiceImpl#addAccount");
Integer i = accountMapper.addAccount(account);
//System.out.println(10/0);
return i;
}
}
package com.zl.mapper;
import com.bean.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface AccountMapper {
@Select("select * from account")
List findAll();
@Insert("insert into account (name,money) values(#{name},#{money})")
Integer addAccount(Account account);
}
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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
property name="locations">
list>
value>classpath:jdbc.propertiesvalue>
list>
property>
bean>
context:component-scan base-package="com.zl" use-default-filters="true">
context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller">context:exclude-filter>
context:component-scan>
bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
property name="driverClass" value="${driver}">property>
property name="jdbcUrl" value="${url}">property>
property name="user" value="${username}">property>
property name="password" value="${password}">property>
bean>
bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factoryBean">
property name="dataSource" ref="dataSource">property>
bean>
bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
property name="basePackage" value="com.zl.mapper">property>
bean>
bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
property name="dataSource" ref="dataSource">property>
bean>
tx:advice transaction-manager="transactionManager" id="txadvice">
tx:attributes>
tx:method name="findAll" read-only="true"/>
tx:method name="addAccount" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
tx:attributes>
tx:advice>
aop:config>
aop:advisor advice-ref="txadvice" pointcut="execution(* com.zl.service.impl.AccountServiceImpl.*(..))">aop:advisor>
aop:config>
beans>
文章标题:springmvc_ssm Spring、SpringMVC、MyBatis集成
文章链接:http://soscw.com/index.php/essay/67994.html