練習ssm架構整合,做增刪改查操作
來源:互聯網
上載者:User
我的開發環境:Windows10系統 開發工具:MyEclipse10,JDK1.8,MySQL5.0,Tomcat7.0 ssm架構整合 在MyEclipse裡建一個web工程,然後 搭建環境,就是匯入jar包,我的jar包是管老師要的,裡邊有串連資料庫驅動的,有spring的,有spring-mvc的和mybatis的,總之很多。把這些jar包放到工程裡的WebRoot目錄下的WEB-INF目錄下的lib檔案夾下,總之記住jar包都放lib檔案夾下。然後依據我個人習慣,會先配置一個記錄檔在src目錄下建,這個記錄檔在網上找一個粘過來就可以了,記錄檔代碼如下: [html] view plain copy <span style="font-size:14px;">log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator</span> 然後在src目錄下建一個JDBC設定檔,這個檔案裡的內容可以寫在資料來源裡,但是都寫一起會顯得亂,我的JDBC代碼如下: [plain] view plain copy <span style="font-size:14px;">jdbc.driver=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=*** jdbc.url=jdbc:mysql://localhost:3306/*** jdbc.pool.maxldle=5 jdbc.pool.maxActive=40</span> password是資料庫密碼,我用***代替了,這雷根據自己的資料庫來寫,URL連的是MySQL資料庫,如果是其的資料庫就寫其他的URL(還有串連資料庫的驅動也是),3306是連接埠號碼,它後面寫資料庫名,這裡也用***代替了,下面是資料庫連接池的上下限,什麼等待時間長度,空閑什麼的沒設,就設了這兩值。 接下來在src目錄下建spring.xml設定檔,這個代碼裡都寫注釋了,最上面是一些約束。我用的是註解編程,這裡有些對註解的配置,我的spring.xml代碼如下: [html] view plain copy <span style="font-size:14px;"><?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:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="true"><!-- 屬性default-lazy-init是一個懶載入過程,設成true時,用到對象才執行個體化,不用時不執行個體化,false反之 --> <!-- 如果要使用自動注入等註解,需要一條一條配置,用下面這列標籤就不用配置了,它會自動識別註解。 向 spring容器註冊標籤 --> <context:annotation-config/> <!-- 註解掃描包,base-package裡是要掃描的路徑===========================================需要改的 --> <context:component-scan base-package="com"/> <!-- 配置資料來源,使用的是bonecp bonecp的效率,速度要高於c3p0 -->