发表于: 2018-03-26 20:10:16

1 399


今天完成的事情:

整合springMVC,mybatis

<?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-3.0.xsd">
   <!-- 配置SpringMVC -->
   <!-- 1.开启SpringMVC注解模式 -->
   <!-- 简化配置:           (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter           (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持   -->
   <mvc:annotation-driven />

   <!-- 2.静态资源默认servlet配置          (1)加入对静态资源的处理:js,gif,png          (2)允许使用"/"做整体映射  -->
   <mvc:default-servlet-handler/>

   <!-- 3.配置jsp 显示ViewResolver -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
       <property name="prefix" value="/WEB-INF/jsp/" />
       <property name="suffix" value=".jsp" />
   </bean>

   <!-- 4.扫描web相关的bean -->
   <context:component-scan base-package="com.student" />
</beans>

<?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:tx="http://www.springframework.org/schema/tx"
      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/tx      http://www.springframework.org/schema/tx/spring-tx.xsd">
   <!-- 扫描service包下所有使用注解的类型 -->
   <context:component-scan base-package="com.student" />

   <!-- 配置事务管理器 -->
   <bean id="transactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!-- 注入数据库连接池 -->
       <property name="dataSource" ref="dataSource" />
   </bean>

   <!-- 配置基于注解的声明式事务 -->
   <tx:annotation-driven transaction-manager="transactionManager" />
</beans>
<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" metadata-complete="true">
 <!-- 如果是用mvn命令生成的xml,需要修改servlet版本为3.1 -->
 <!-- 配置DispatcherServlet -->
 <servlet>
   <servlet-name>seckill-dispatcher</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- 配置springMVC需要加载的配置文件                  spring-dao.xml,spring-service.xml,spring-web.xml                  Mybatis - > spring -> springmvc          -->
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring/spring-*.xml</param-value>
   </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>seckill-dispatcher</servlet-name>
   <!-- 默认匹配所有的请求 -->
   <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>
<?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 ">
   <!-- 配置整合mybatis过程 -->
   <!-- 1.配置数据库相关参数properties的属性:${url} -->
   <context:property-placeholder location="classpath:jdbc.properties"/>
   <!-- 2.数据库连接池 -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <!-- 配置连接池属性 -->
       <property name="driverClassName" value="${jdbc.driver}"/>
       <property name="url" value="${jdbc.url}"/>
       <property name="username" value="${jdbc.username}"/>
       <property name="password" value="${jdbc.password}"/>
       <!-- 连接池的私有属性 -->
       <!--<property name="maxActive" value="30"/>-->
       <!--<property name="minIdle" value="10"/>-->
       <!-- 关闭连接后不自动commit -->
       <!--<property name="defaultAutoCommit" value="false"/>-->
       <!-- 获取连接超时时间 -->
       <!--<property name="loginTimeout" value="1000"/>-->
       <!-- 当获取连接失败重连次数 -->
       <!--<property name="defaultTransactionIsolation" value="2"/>-->
   </bean>
   <!-- 3.配置SqlSessionFactory对象 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 注入数据库连接池 -->
       <property name="dataSource" ref="dataSource"/>
       <!-- 配置mybatis全局配置文件:mybatis-config.xml -->
       <property name="configLocation" value="classpath:mybatis-config.xml"/>
       <!-- 扫描sql配置文件:mapper需要的xml文件 -->
       <property name="mapperLocations" value="classpath:mapper/*.xml"/>
   </bean>
   <!-- 4.配置扫描dao接口包,动态实现dao接口,注入到spring的容器中 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <!-- 注入sqlSessionFactory -->
       <property name="sqlSessionFactoryBeanName" value="sqlSeesionFactory"/>
       <!-- 给出需要扫描dao接口包 -->
       <property name="basePackage" value="com"/>
   </bean>

</beans>
<?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:tx="http://www.springframework.org/schema/tx"
      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/tx      http://www.springframework.org/schema/tx/spring-tx.xsd">
   <!-- 扫描service包下所有使用注解的类型 -->
   <context:component-scan base-package="com.student" />

   <!-- 配置事务管理器 -->
   <bean id="transactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!-- 注入数据库连接池 -->
       <property name="dataSource" ref="dataSource" />
   </bean>

   <!-- 配置基于注解的声明式事务 -->
   <tx:annotation-driven transaction-manager="transactionManager" />
</beans>


明天计划的事情:

继续整合 
遇到的问题:

层级未曾分辨清楚,配置文件的扫描还要继续,这个配置有点绕,明天画图分解下
收获:


返回列表 返回列表
评论

    分享到