标签:location listener required configure mon inter prope 文件中 rri
<?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>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--监听器,容器加载监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--初始化容器参数--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> </web-app>
最好每一步集成完都先进行相应测试
控制器
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<Account> 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"; } }
springmvc.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" 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"></context:component-scan>--> <!--包扫描冲突 解决1 改springmvc.xml包扫描,只扫控制器--> <!--包扫描冲突 解决2 改spring.xml包扫描--> <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:注解驱动--> <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> <!--包扫描冲突 方法二,导入spring.xml 如果有了这个配置,可以不用在web.xml中配置容器加载监听器--> <!--<import resource="spring.xml"></import>--> </beans>
package com.zl.service; import com.bean.Account; import java.util.List; public interface AccountService { List<Account> 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<Account> 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<Account> findAll(); @Insert("insert into account (name,money) values(#{name},#{money})") Integer addAccount(Account account); }
spring.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: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"> <!--Spring加载Properties配置文件的方式--> <!--通过 util:properties 标签实现配置文件加载--> <!--<util:properties id="util_spring" local-override="true" location="classpath:jdbc.properties"/>--> <!--通过 PropertyPlaceholderConfigurer 类读取配置文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!--以前的bean配置文件,创建service bean对象的--> <!--配置注解包扫描创建对象--> <!--<context:component-scan base-package="com.zl"></context:component-scan>--> <!--包扫描冲突 方法一解决1 改springmvc.xml包扫描--> <!--包扫描冲突 方法一解决2 改spring.xml包扫描,不扫控制器(扫service与dao)--> <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> <!--配置mybatis三部曲--> <!--配置数据源连接池,连上数据库服务器--> <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> <!-- <property name="driverClass" value="#{util_spring[‘driver‘]}"></property> <property name="jdbcUrl" value="#{util_spring[‘url‘]}"></property> <property name="user" value="#{util_spring[‘username‘]}"></property> <property name="password" value="#{util_spring[‘password‘]}"></property>--> </bean> <!--配置工厂对象拿到session--> <!--SqlSessionFactoryBean主要取代了之前SqlSessionFactory的相关操作--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factoryBean"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置mapper扫描--> <!--MapperScannerConfigurer主要是取代之前的bean.xml中mappers配置,指定mapper接口的包路径--> <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集成
标签:location listener required configure mon inter prope 文件中 rri
原文地址:https://www.cnblogs.com/21556guo/p/13761715.html