1、配置POM.xml文件,配置項目所需jar(下載jar包,尋找依賴,熱部署)
pom從本地拿文件:(遇到中央倉庫授權問題)
<dependency>
<groupId>cn.outofmemory</groupId>
<artifactId>my-tools</artifactId>
<version>2.5.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${basedir}/lib/mylib1.jar</systemPath>
</dependency>
2、配置web.xml
頭部換成2.4版本:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID" version="2.4">
創建配置文件application-context.xml
<?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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 把config文件中所有的配置文件都自動加載進來 -->
<import resource="config/*.xml" />
</beans>
web.xml中配置創建好的文件:
<context-param>
<param-name>con</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
這是web默認加載配置文件
web.xml配置spring監聽器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在src/main/resources下建立config文件夾用於存放配置文件
config目錄下配置anotation.xml用於配置註解(掃描,自動裝配)
src/main/java中創建基本的項目目錄結構
src/main/java/core核心包
src/main/java/utils公用包
4、anotation.xml中只配置spring掃描上述包中的service包:
<!-- 配置spring掃描 @service -->
<context:component-scan base-package="cn.zy">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 自動裝配 -->
<!-- 自動配置依賴注入,不需要再set進去 -->
<context:annotation-config/>
5、配置c3p0連接池
(可以連接多個數據庫,不止一個;連接斷開會自動連接)
事物也要單獨配置由容器進行管理
創建資源文件properties存放jdbc.properties
資源文件中配置數據庫基本信息:
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8
user=root
password=123456
新建jdbc.xml在config目錄下配置c3p0連接池:
<!-- 配置c3p0 4個值從properties資源文件中取-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}" />
<property name="password" value="${password}" />
</bean>
6、新建並配置property.xml文件,讀取jdbc.properties:
PropertyPlaceholderConfigurer (spring中配置資源文件的讀取)
spring配置資源文件的讀取:
<!-- 讀取JDBC配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- jdbc的配置 -->
<value>classpath:properties/jdbc.properties</value>
<!-- memcached的配置 -->
<!-- 再用value屬性對接下來的資源文件進行配置 -->
</list>
</property>
</bean>
7、創建並配置mybatis.xml文件:
<!-- mybatis org.mybatis.spring.SqlSessionFactoryBean配置,數據源的配置 -->
<!-- mybatis org.mybatis.spring.SqlSessionFactoryBean配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置數據源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置導入映射文件的目錄 -->
<property name="mapperLocations" value="classpath:cn/zy/core/dao/*.xml"></property>
<!-- 設置包的別名 -->
<property name="typeAliasesPackage" value="cn.zy.core.bean"></property>
</bean>
<!-- 配置mybatis對dao包的掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.zy.core.dao"></property>
</bean>
SqlsessionFactoryBean(產生session,可以進行增刪改查)
配置時id不能亂寫!因爲是自動裝配
在cn.zy.core.bean下創建一個UserInfo實體類,用於mybatis做映射
src/main/resource下創建core/dao的mybatis相對於實體的映射配置文件
UserInfoDAO.xml
對sql語句進行註冊
設置包的別名:mybatis.xml文件中:<property name="typeAliasesPackage" value="cn.zy.core.bean"></property>
設置完別名之後UserInfoDAO.xml中就可以不設置包名,直接用對象名註冊sql
配置mybatis對dao包的掃描(類似於spring配置對service包的掃描)
8、事務管理:transation.xml 以註解的方式
spring 聲明式事務
使用mybatis或者hibernate時,不需要手動編寫進行提交事務的代碼
DataSourceTransactionManager
<!-- spring 事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 開啓註解事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
---------------------------------------------------------------
測試環境是否搭建成功:
UserInfoDAO.xml中namespace爲dao下的UserInfoDAO接口目錄
UserInfoDAO接口中的方法名要和UserInfoDAO.xml中的id要對應上
編寫好註冊的sql ,insert
mybatis中#{}代表預編譯,相當於?
service中編寫IUserInfoService編寫添加方法
service目錄下的impl下創建IUserInfoService的實現類
創建實現類IUerInfoServiceImpl過程中add interface中,加入IUserInfoService這個接口
在實現類IUerInfoServiceImpl添加註解@Service
定義private UserInfoDAO userdao;並加註解@Autowired
在測試類中UserTest進行測試
註解的方式引入測試環境和配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:application-context.xml")
run as --> Junit Test --> 成功,數據庫中成功添加數據
如何要讓事務發揮作用:
在接口方法中添加異常拋出,
public void addUser(UserInfo user) throws Exception;
實現類中也添加異常拋出
測試類添加try catch
run as --> Junit Test --> 捕捉到了異常,但是數據還是插入到了數據庫,所以這裏要進行事務的控制!
運行時異常 throw new RuntimeException("運行時異常");
也能插入到數據庫
應該設置 : 事務回滾,報錯時數據庫數據添加不成功
操作:因爲配置文件中開啓了註解事務,所以在接口的上方添加事務管理的註解即可
銀行轉賬的時候:要麼就一起成功,要麼就一起回滾.