需要的依賴介紹:
spring-webmvc 導(dǎo)入該jar包會(huì)自動(dòng)導(dǎo)入很多spring相關(guān)依賴的包
jackson-core,data-bind,annotations 這三個(gè)是json相關(guān)的包
spring-jdbc,mybatis,mybatis-spring兩者結(jié)合的一個(gè)插件,mysql-connector-java 是mysql驅(qū)動(dòng),commons-dbcp數(shù)據(jù)庫(kù)連接池
junit測(cè)試
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>Mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
配置Spring MVC控制器: web.xml 添加dispatcherservlet
<servlet>
<description></description>
<display-name>DispatcherServlet</display-name>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description></description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
配置文件格式可以根據(jù)spring版本去官網(wǎng)找 此處格式為
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 具體配置 -- >
<beans>
配置Spring-MVC: conf/spring-mvc.xml
<!-- 配置組件掃描 -->
<context:component-scan
base-package="com.bugsys.controller"/>
<!-- 配置MVC注解掃描 -->
<mvc:annotation-driven />
配置MyBatis: conf/spring-mybatis.xml
<!-- 配置dbcp連接池: 連接到數(shù)據(jù)庫(kù)-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 連接池的基本連接參數(shù) -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/cloud_note"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
<!-- 連接池可選參數(shù) -->
<property name="maxActive" value="50"></property>
<property name="initialSize" value="5"></property>
<property name="maxIdle" value="5"></property>
</bean>
<!-- 配置MyBatis的 Session 工廠 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 聲明MyBatis SQL 聲明文件保存的地方 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"> </property>
</bean>
<!-- 配置MyBatis的自動(dòng)接口掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 數(shù)據(jù)訪問接口的存儲(chǔ)位置 -->
<property name="basePackage" value="cn.tedu.note.dao"></property>
</bean>
配置spring-mybatis.xml 包含bean: dpcp鏈接池, mybatis的session工廠, mybatis自動(dòng)掃描接口
dpcp連接池:
id=dataSource 其參數(shù)是包下的set方法取消set率挣,并且把第一個(gè)字母換成小寫
driverClassName/url/username/password
session工廠:來自mybatis-spring這個(gè)包
sqlSessionFactoryBean dataSource ref='引用前面定義好的的dataSource對(duì)象'
mapperLocations 生命mybatis sql聲明文件保存的地方 value="classpath:mapper/*.xml"
mybatis自動(dòng)掃描接口: 數(shù)據(jù)訪問接口的存放位置
mybatis-spring包下 org.mybatis.spring.mapper包下的 MapperScannerConfigurer方法 basePackage