1. 程式人生 > >spring+springMVC+ibatis基本配置檔案

spring+springMVC+ibatis基本配置檔案

1.建立動態web程式,並且修改編譯路徑改為WEB-INFO目錄下
buildpath->source標籤 中修改
2.新增所有jar包在lib目錄下方,並且在buildpath中也新增。防止lib中的jar包出現錯誤

3.建立mvc.xml和ibatis的配置檔案

mvc.xml中對一個controller的註解進行掃描,並且配置springmvc的檢視渲染

<!-- SpringMVC配置 -->
	
	<!-- 通過component-scan 讓Spring掃描org.swinglife.controller下的所有的類,讓Spring的程式碼註解生效 --
> <context:component-scan base-package="controller"></context:component-scan> <!-- 配置SpringMVC的檢視渲染器, 讓其字首為:/ 字尾為.jsp 將檢視渲染到/page/<method返回值>.jsp中 --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/page/" p:suffix=".jsp"
> </beans:bean>

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"  "http://www.ibatis.com/dtd/sql-map-config-2.dtd">  
<sqlMapConfig> 
    <sqlMap resource="sqlMap_User.xml" />  //載入對映檔案
</sqlMapConfig>

對映檔案

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">  

<sqlMap namespace="Test">  
 //寫自己的sql操作
    <statement id="select_all_user"  
        resultClass="com.lu.pojo.User">  
        select * from user 
        where is_delete = 0
        order by id  
    </statement>  
</sqlMap>

4.建立spring的root-context.xml

建立spring的datasource,而不使用ibatis的datasource,將連線資料庫的驅動資訊等放入db.properties內,在spring的配置檔案中讀取。
spring的service層註解配置在當前配置檔案中

<!--載入配置檔案  -->
 	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyPlaceholderConfigurer">
 		<property name="locations">
 		<list>
 			<value>classpath:db.properties</value>
 		</list>
 		</property>
 	</bean>
 	
 	<!-- 建立spring的datasource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean>

連線資料庫檔案db.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ibatis_demo?characterEncoding=utf8
username=root
password=root

總的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:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd
		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/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://cxf.apache.org/jaxws 
		http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- Root Context: defines shared resources visible to all other web components -->
	<context:component-scan base-package="serviceimpl"></context:component-scan>
	<context:component-scan base-package="ws"></context:component-scan>
	
	<!--載入配置檔案  -->
 	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyPlaceholderConfigurer">
 		<property name="locations">
 		<list>
 			<value>classpath:db.properties</value>
 		</list>
 		</property>
 	</bean>
 	<!-- 建立spring的datasource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	
	<!-- spring載入ibatis的配置檔案 -->
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean class="org.springframework.orm.ibatis.SqlMapClientTemplate" id="sqlMapClientTemplate">
		<property name="sqlMapClient" ref="sqlMapClient"></property>
	</bean>
</beans>

5.修改web.xml

需要在web.xml中配置mvc的DispatcherServlet攔截相應的請求

<!-- 配置Spring MVC DispatcherServlet -->
	<servlet>
		<servlet-name>MVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 初始化引數 -->
		<init-param>
			<!-- 載入SpringMVC的xml到 spring的上下文容器中 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:mvc.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 配置DispatcherServlet所需要攔截的 url -->
	<servlet-mapping>
		<servlet-name>MVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

第二步配置spring配置檔案的路徑

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:root-context.xml</param-value>
	</context-param>

第三步配置監聽,監聽spring上下文容器

<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

總的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>springMVC_firstdemo_20181123</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 監聽spring上下文容器 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	<!-- 載入spring的xml配置檔案到 spring的上下文容器中 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:root-context.xml</param-value>
	</context-param>
  <!-- 配置Spring MVC DispatcherServlet -->
	<servlet>
		<servlet-name>MVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 初始化引數 -->
		<init-param>
			<!-- 載入SpringMVC的xml到 spring的上下文容器中 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:mvc.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<!-- 配置DispatcherServlet所需要攔截的 url -->
	<servlet-mapping>
		<servlet-name>MVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

6.spring中常使用的註解

@Controller 配置在mvc的配置檔案中
@Service 配置在service層類上
引數注入註解能使用三種
//三種注入bean的方法
1.Resource 按變數名字注入和bean的名字一樣
2.Autowire 自動注入,依據變數型別將 bean注入,但是同種型別只能有一種
3.Qulifira(”注入bean的名字”)