1. 程式人生 > >maven+springMVC+mybatis+junit詳細搭建過程 maven+springMVC+mybatis+junit詳細搭建過程

maven+springMVC+mybatis+junit詳細搭建過程 maven+springMVC+mybatis+junit詳細搭建過程

maven+springMVC+mybatis+junit詳細搭建過程

2017年04月20日 15:22:44 閱讀數:761
																				<div class="tags-box space">
							<span class="label">個人分類:</span>
															<a class="tag-link" href="https://blog.csdn.net/dbeautifulLife/article/category/6869339" target="_blank">spring																</a><a class="tag-link" href="https://blog.csdn.net/dbeautifulLife/article/category/6675295" target="_blank">javaWeb開發																</a>
						</div>
																							</div>
			<div class="operating">
													</div>
		</div>
	</div>
</div>
<article>
	<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
							            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-e2445db1a8.css">
					<div class="htmledit_views">

springMVC+mybatis框架搭建

首先我們先要弄清搭建專案的一般流程,需要注意哪些方面,想要什麼樣的效果,自己的功能有哪些?

(假設效果:專案目錄結構清晰,能夠查詢到本地資料庫中的內容。。)

1.  工程目錄結構整理清楚

在src/main/java資料夾中,新建包cn.springmvc.model(存放javabean),

                                            cn.springmvc.dao(存放spring與mybatis連線介面),

                                            cn.springmvc.service(service介面),

                                            cn.springmvc.service.impl(service介面的實現),

                                            cn.springmvc.controller(存放控制層controller)

在src/main/resource資料夾中,新建包conf(存放配置檔案),

                                                   mapper(mybatis的mapper檔案)

在src/test/java資料夾中,新建包cn.springmvc.test(存放測試檔案)

在WEB-INF資料夾下新建jsp資料夾(存放jsp檔案)

這樣專案結構基本完成了

2.  引入依賴包

開啟maven的pom檔案,對本次開發所需使用的架包依次匯入(maven專案管理的優勢)

查詢依賴結構有個不錯的網站,http://search.maven.org/   只要輸入包名即可查詢引來關係

pom.xml(包依賴)


[xml] view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>eyas.springmvc</groupId>  
  5.     <artifactId>springmvc</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>springmvc Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <properties>  
  11.         <!-- spring版本號 -->  
  12.         <spring.version>3.2.4.RELEASE</spring.version>  
  13.         <!-- mybatis版本號 -->  
  14.         <mybatis.version>3.2.4</mybatis.version>  
  15.         <!-- log4j日誌檔案管理包版本 -->  
  16.         <slf4j.version>1.6.6</slf4j.version>  
  17.         <log4j.version>1.2.9</log4j.version>  
  18.     </properties>  
  19.     <dependencies>  
  20.         <!-- spring核心包 -->  
  21.         <!-- springframe start -->  
  22.         <dependency>  
  23.             <groupId>org.springframework</groupId>  
  24.             <artifactId>spring-core</artifactId>  
  25.             <version>${spring.version}</version>  
  26.         </dependency>  
  27.   
  28.         <dependency>  
  29.             <groupId>org.springframework</groupId>  
  30.             <artifactId>spring-web</artifactId>  
  31.             <version>${spring.version}</version>  
  32.         </dependency>  
  33.   
  34.         <dependency>  
  35.             <groupId>org.springframework</groupId>  
  36.             <artifactId>spring-oxm</artifactId>  
  37.             <version>${spring.version}</version>  
  38.         </dependency>  
  39.   
  40.         <dependency>  
  41.             <groupId>org.springframework</groupId>  
  42.             <artifactId>spring-tx</artifactId>  
  43.             <version>${spring.version}</version>  
  44.         </dependency>  
  45.   
  46.         <dependency>  
  47.             <groupId>org.springframework</groupId>  
  48.             <artifactId>spring-jdbc</artifactId>  
  49.             <version>${spring.version}</version>  
  50.         </dependency>  
  51.   
  52.         <dependency>  
  53.             <groupId>org.springframework</groupId>  
  54.             <artifactId>spring-webmvc</artifactId>  
  55.             <version>${spring.version}</version>  
  56.         </dependency>  
  57.   
  58.         <dependency>  
  59.             <groupId>org.springframework</groupId>  
  60.             <artifactId>spring-aop</artifactId>  
  61.             <version>${spring.version}</version>  
  62.         </dependency>  
  63.   
  64.         <dependency>  
  65.             <groupId>org.springframework</groupId>  
  66.             <artifactId>spring-context-support</artifactId>  
  67.             <version>${spring.version}</version>  
  68.         </dependency>  
  69.   
  70.         <dependency>  
  71.             <groupId>org.springframework</groupId>  
  72.             <artifactId>spring-aop</artifactId>  
  73.             <version>${spring.version}</version>  
  74.         </dependency>  
  75.   
  76.         <dependency>  
  77.             <groupId>org.springframework</groupId>  
  78.             <artifactId>spring-test</artifactId>  
  79.             <version>${spring.version}</version>  
  80.         </dependency>  
  81.         <!-- springframe end -->  
  82.   
  83.         <!-- mybatis核心包 -->  
  84.         <dependency>  
  85.             <groupId>org.mybatis</groupId>  
  86.             <artifactId>mybatis</artifactId>  
  87.             <version>${mybatis.version}</version>  
  88.         </dependency>  
  89.         <!-- mybatis/spring包 -->  
  90.         <dependency>  
  91.             <groupId>org.mybatis</groupId>  
  92.             <artifactId>mybatis-spring</artifactId>  
  93.             <version>1.2.2</version>  
  94.         </dependency>  
  95.         <!-- mysql驅動包 -->  
  96.         <dependency>  
  97.             <groupId>mysql</groupId>  
  98.             <artifactId>mysql-connector-java</artifactId>  
  99.             <version>5.1.29</version>  
  100.         </dependency>  
  101.         <!-- junit測試包 -->  
  102.         <dependency>  
  103.             <groupId>junit</groupId>  
  104.             <artifactId>junit</artifactId>  
  105.             <version>4.11</version>  
  106.             <scope>test</scope>  
  107.         </dependency>  
  108.         <!-- 阿里巴巴資料來源包 -->  
  109.         <dependency>  
  110.             <groupId>com.alibaba</groupId>  
  111.             <artifactId>druid</artifactId>  
  112.             <version>1.0.2</version>  
  113.         </dependency>  
  114.   
  115.         <!-- json資料 -->  
  116.         <dependency>  
  117.             <groupId>org.codehaus.jackson</groupId>  
  118.             <artifactId>jackson-mapper-asl</artifactId>  
  119.             <version>1.9.13</version>  
  120.         </dependency>  
  121.   
  122.         <!-- 日誌檔案管理包 -->  
  123.         <!-- log start -->  
  124.         <dependency>  
  125.             <groupId>log4j</groupId>  
  126.             <artifactId>log4j</artifactId>  
  127.             <version>${log4j.version}</version>  
  128.         </dependency>  
  129.         <dependency>  
  130.             <groupId>org.slf4j</groupId>  
  131.             <artifactId>slf4j-api</artifactId>  
  132.             <version>${slf4j.version}</version>  
  133.         </dependency>  
  134.         <dependency>  
  135.             <groupId>org.slf4j</groupId>  
  136.             <artifactId>slf4j-log4j12</artifactId>  
  137.             <version>${slf4j.version}</version>  
  138.         </dependency>  
  139.         <!-- log end -->  
  140.     </dependencies>  
  141.     <build>  
  142.         <finalName>springmvc</finalName>  
  143.     </build>  
  144. </project>  
<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>eyas.springmvc</groupId>
	<artifactId>springmvc</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>springmvc Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<!-- spring版本號 -->
		<spring.version>3.2.4.RELEASE</spring.version>
		<!-- mybatis版本號 -->
		<mybatis.version>3.2.4</mybatis.version>
		<!-- log4j日誌檔案管理包版本 -->
		<slf4j.version>1.6.6</slf4j.version>
		<log4j.version>1.2.9</log4j.version>
	</properties>
	<dependencies>
		<!-- spring核心包 -->
		<!-- springframe start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-test&lt;/artifactId&gt;
		&lt;version&gt;${spring.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;!-- springframe end --&gt;

	&lt;!-- mybatis核心包 --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.mybatis&lt;/groupId&gt;
		&lt;artifactId&gt;mybatis&lt;/artifactId&gt;
		&lt;version&gt;${mybatis.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;!-- mybatis/spring包 --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.mybatis&lt;/groupId&gt;
		&lt;artifactId&gt;mybatis-spring&lt;/artifactId&gt;
		&lt;version&gt;1.2.2&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;!-- mysql驅動包 --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;mysql&lt;/groupId&gt;
		&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
		&lt;version&gt;5.1.29&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;!-- junit測試包 --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;junit&lt;/groupId&gt;
		&lt;artifactId&gt;junit&lt;/artifactId&gt;
		&lt;version&gt;4.11&lt;/version&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;!-- 阿里巴巴資料來源包 --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;com.alibaba&lt;/groupId&gt;
		&lt;artifactId&gt;druid&lt;/artifactId&gt;
		&lt;version&gt;1.0.2&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;!-- json資料 --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
		&lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
		&lt;version&gt;1.9.13&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;!-- 日誌檔案管理包 --&gt;
	&lt;!-- log start --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;log4j&lt;/groupId&gt;
		&lt;artifactId&gt;log4j&lt;/artifactId&gt;
		&lt;version&gt;${log4j.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
		&lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
		&lt;version&gt;${slf4j.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
		&lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
		&lt;version&gt;${slf4j.version}&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;!-- log end --&gt;
&lt;/dependencies&gt;
&lt;build&gt;
	&lt;finalName&gt;springmvc&lt;/finalName&gt;
&lt;/build&gt;

</project>


3. 配置資料庫連線屬性

conf/ jdbc.properties(jdbc配置檔案)


  
  1. jdbc_driverClassName=com.mysql.jdbc.Driver
  2. jdbc_url=jdbc:mysql: //localhost:3306/mydays?useUnicode=true&amp;characterEncoding=utf-8
  3. jdbc_username=root
  4. jdbc_password=root

4.  配置spring配置檔案

    conf/spring.xml(spring配置檔案的掃描)


[xml] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:context="http://www.springframework.org/schema/context"  
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.       http://www.springframework.org/schema/context  
  8.       http://www.springframework.org/schema/context/spring-context.xsd">  
  9.   
  10.     <!-- 引入jdbc配置檔案 -->  
  11.     <context:property-placeholder location="classpath:conf/jdbc.properties"/>  
  12.       
  13.     <!-- 掃描檔案(自動將servicec層注入) -->  
  14.     <context:component-scan base-package="cn.springmvc.service"/>  
  15. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
&lt;!-- 引入jdbc配置檔案 --&gt;
&lt;context:property-placeholder location="classpath:conf/jdbc.properties"/&gt;

&lt;!-- 掃描檔案(自動將servicec層注入) --&gt;
&lt;context:component-scan base-package="cn.springmvc.service"/&gt;

</beans>
  conf/spring-mybatis.xml(spring與mybatis連線屬性)

[xml] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xmlns:p="http://www.springframework.org/schema/p"  
  5.   xmlns:context="http://www.springframework.org/schema/context"  
  6.   xmlns:aop="http://www.springframework.org/schema/aop"  
  7.   xmlns:tx="http://www.springframework.org/schema/tx"  
  8.   xmlns:util="http://www.springframework.org/schema/util"  
  9.   xsi:schemaLocation="http://www.springframework.org/schema/beans  
  10.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  11.     http://www.springframework.org/schema/context  
  12.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  13.     http://www.springframework.org/schema/tx  
  14.     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  15.     http://www.springframework.org/schema/aop  
  16.     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  17.     http://www.springframework.org/schema/util   
  18.     http://www.springframework.org/schema/util/spring-util-3.2.xsd">  
  19.   
  20.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"  
  21.     destroy-method="close" >  
  22.     <property name="driverClassName">  
  23.       <value>${jdbc_driverClassName}</value>  
  24.     </property>  
  25.     <property name="url">  
  26.       <value>${jdbc_url}</value>  
  27.     </property>  
  28.     <property name="username">  
  29.       <value>${jdbc_username}</value>  
  30.     </property>  
  31.     <property name="password">  
  32.       <value>${jdbc_password}</value>  
  33.     </property>  
  34.     <!-- 連線池最大使用連線數 -->  
  35.     <property name="maxActive">  
  36.       <value>20</value>  
  37.     </property>  
  38.     <!-- 初始化連線大小 -->  
  39.     <property name="initialSize">  
  40.       <value>1</value>  
  41.     </property>  
  42.     <!-- 獲取連線最大等待時間 -->  
  43.     <property name="maxWait">  
  44.       <value>60000</value>  
  45.     </property>  
  46.     <!-- 連線池最大空閒 -->  
  47.     <property name="maxIdle">  
  48.       <