一、是什么
學(xué)習(xí)跟蹤系統(tǒng)是一個記錄學(xué)習(xí)過程的系統(tǒng)。
采用springMVC+Spring+Mybatis技術(shù),以下為該系統(tǒng)的思維導(dǎo)圖牢硅,同時歡迎加入QQ交流群進(jìn)行交流:5369686,一起做項(xiàng)目芝雪,一起High
在線學(xué)習(xí)跟蹤系統(tǒng)思維導(dǎo)圖
二减余、框架搭建
配置在官方文檔中都能找到相關(guān)配置:一下文檔均在項(xiàng)目中有體現(xiàn),可以再源碼中下載查看惩系,以后得分享中會對這些知識點(diǎn)進(jìn)行分別講解位岔。
spring官方文檔:https://docs.spring.io/spring/docs/4.3.9.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#transaction-declarative-txadvice-settings
mybatis官方文檔:http://www.mybatis.org/mybatis-3/zh/configuration.html
mybatis-spring官方文檔:http://www.mybatis.org/spring/zh/batch.html
mybatis-generator官方文檔:http://www.mybatis.org/generator/configreference/javaModelGenerator.html
druid數(shù)據(jù)源官方文檔:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE
logback日志框架官方文檔:https://logback.qos.ch/manual/layouts.html
源碼地址:https://pan.baidu.com/s/1b231Wa
視頻講解:http://www.365yg.com/item/6461501385442591246/
maven依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.begincode</groupId>
<artifactId>begin-study</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>begin-study</name>
<properties>
<spring.version>4.3.9.RELEASE</spring.version>
<guava.version>20.0</guava.version>
<log4j.version>2.7</log4j.version>
<jcl-over-slf4.version>1.7.21</jcl-over-slf4.version>
<logback.version>1.1.7</logback.version>
<jackson.version>2.8.1</jackson.version>
<mybatis.version>3.4.1</mybatis.version>
<mybatis-generator.version>1.3.2</mybatis-generator.version>
<druid.version>1.0.27</druid.version>
<mysql.version>5.1.34</mysql.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- 數(shù)據(jù)源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!--日志依賴 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl-over-slf4.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- mybatis generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>begin-study</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
</plugin>
</plugins>
</build>
</project>
三、文字配置描述
1堡牡、Spring配置
SpringMVC是Spring家族的一部分抒抬,天然支持Spring,所以配置Spring一句話晤柄,配置一個監(jiān)聽器即可
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2擦剑、SpringMVC配置
web.xml
<!-- mvc配置文件,說明是位于resources目錄下的sprign-config.xml
如果不通過這樣的配置芥颈,默認(rèn)是讀取WEB-INF目錄下的servlet名稱對應(yīng)的xml文件惠勒。
如:本例中DispatcherServlet的名稱定義為web,則需要讀取web-servlet.xml作為SpringMVC的配置文件
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3爬坑、spring-config.xml配置
<!-- 加載屬性文件-->
<context:property-placeholder location="classpath:config.properties" />
<!-- 加載其他配置文件纠屋,本項(xiàng)目將mvc和mybatis分為兩個文件配置,
這種方式只是為了讓配置文件更加清晰-->
<import resource="classpath:spring/spring-mvc.xml"/>
<import resource="classpath:spring/spring-mybatis.xml"/>
4盾计、spring-mvc.xml 位于resources.spring目錄下
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="net.begincode"/>
<!-- jackson解析解析對象到j(luò)son-->
<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="indentOutput" value="false"/>
<property name="simpleDateFormat" value="yyyy-MM-dd"/>
</bean>
<!-- 用jackson處理responseBody注解 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 資源文件位置售担,jsp文件和favico.ico文件 -->
<mvc:resources mapping="/" location="/WEB-INF/jsp/"/>
<mvc:resources mapping="/favicon.ico" location="/"/>
<!-- jsp view支持 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5赁遗、spring-mybatis.xml 位于resource.spring目錄下
<?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:aop="http://www.springframework.org/schema/aop"
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/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">
<!-- 注解的事務(wù)支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 數(shù)據(jù)源采用阿里的druid數(shù)據(jù)源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本屬性 url、user族铆、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
<!-- 配置初始化大小岩四、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置獲取連接等待超時的時間 -->
<property name="maxWait" value="60000" />
<!-- 配置間隔多久才進(jìn)行一次檢測骑素,檢測需要關(guān)閉的空閑連接炫乓,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個連接在池中最小生存的時間刚夺,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打開PSCache献丑,并且指定每個連接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters -->
<property name="filters" value="stat" />
</bean>
<!-- 事務(wù)配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="configLocation" value="classpath:spring/mybatis.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.begincode.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
6、mybatis.xml 上一個配置中用到了mybatis侠姑,需要對他進(jìn)行一些基礎(chǔ)參數(shù)的設(shè)置创橄,就在這文件中配置如下
<?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>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
</configuration>