<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">
<!--須要修改servlet版本爲3.1-->
</web-app>
複製代碼
###修改目錄結構php
src
main
java
resource
webapp
test
java
resource
複製代碼
<!--補全項目依賴-->
<!--1:
日誌 slf4j log4j logback common-logging
slf4j 是規範/接口
剩下的是實現
這裏實現slf4j + logback
-->
複製代碼
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
</dependency>
<!--實現slf4j接口而且整合-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
複製代碼
<!--數據庫相關依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--DAO框架 : Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--這個是mybatis本身整合的與spring的依賴-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--servlet web相關-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--Spring-->
<!--Spring核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<!--SpringDAO-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<!--SpringWEB-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<!--SpringTEST-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
複製代碼
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置全局屬性-->
<settings>
<!--使用jdbc的getGeneratedKeys 獲取數據庫自增主鍵值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列別名替換列名 默認true
select name as title from table name(列名) title(別名)-->
<setting name="userColumnLabel" value="true"/>
<!--駝峯自動轉換-->
<setting name="mapUnderscoreCamelCase" value="true"/>
</settings>
</configuration>
複製代碼
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/seckill
jdbc.username=root
jdbc.password=8023
#有個內置屬性叫作username,因此這裏最後都加前綴
複製代碼
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:contextp="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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd " >
<!--配置數據庫相關參數-->
<!--1.加載jdbc須要的四個參數, 在properties中,能夠用${driver}來取得去驅動的值-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--2.數據庫鏈接池 c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--配置鏈接池屬性-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--鏈接池的私有屬性-->
<!--最大數據鏈接,默認15個-->
<property name="maxPoolSize" value="30"/>
<!--最小數據鏈接,默認3個-->
<property name="minPoolSize" value="10"/>
<!--關閉鏈接後是否自動提交,默認false-->
<property name="autoCommitOnClose" value="false"/>
<!--默認超時爲無限等待-->
<!--<property name="checkoutTimeout" value="1000"/>-->
<!--獲取失敗後重試幾回-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入鏈接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置mybatis全局配置文件 mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!--掃描entity,能夠不用在mapper寫全名 若是倆個就value="org.seckill.entity;org.seckill.entity1"-->
<property name="typeAliasesPackage" value="org.seckill.entity"/>
<!--掃描mapper文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置掃描DAO接口的包,動態實現DAO接口,而且注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--給出掃描DAO接口的包-->
<property name="basePackage" value="org.seckill.dao"></property>
</bean>
</beans>
複製代碼
`ctrl+shift+t` 動態生成test類
複製代碼
進行測試java
###spring託管service依賴(Spring IOC) [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-AIlcepDk-1578628614900)(i.imgur.com/Z8bbbsg.png)]mysql
###配置spring-service.xmlweb
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:contextp="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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd " >
<!--掃描service包下的全部使用了註解的類型-->
<context:component-scan base-package="org.seckill.service"></context:component-scan>
</beans>
複製代碼
<!--配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入數據庫鏈接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置基於註解的聲明式事務-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
複製代碼
@Transactional
複製代碼
<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">
<!--須要修改servlet版本爲3.1-->
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置SpringMVC須要的配置文件spring-dao.xml spring-web.xml spring-service.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:contextp="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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd " >
<!--開啓註解模式-->
<!-- 簡化配置: 自動註冊DefaultAnnotationHandlerMapping AnnotationMethodHandlerAdapter 提供一系列功能:數據綁定:數字和日期的format NumberFormat DataTimeFormat xml, json默認讀寫 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--靜態資源默認servlet配置-->
<!-- 加入對靜態資源的處理 gif png js 容許使用 / 作總體映射 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--配置解析-->
<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>
<!--掃描web相關的bean-->
<context:component-scan base-package="org.seckill.controller"/>
</beans>
複製代碼