天天看點

SSM架構整合之配置檔案的定義SSM架構整合之配置檔案的定義

SSM架構整合之配置檔案的定義

一、mybatis所需要的配置檔案

1,mybatis映射檔案

<strong><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="接口的全限定性名稱">

	<insert id="對應的方法名稱">
                 <!-- sql語句-->
		insert into student(name,age) values(#{name},#{age})
	</insert>
	
</mapper></strong>
           

2,mybatis主配置檔案

<strong><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 為實體類指定别名 -->
	<typeAliases>
		<package name="com.bjpowernode.beans"/>
	</typeAliases>
	
	<!-- 注冊映射檔案 -->
	<mappers>
		<!-- 映射檔案的名字和實體類的名字相同的時候才能這樣寫
				要是不同,就要用 mapper标簽-->
		<package name="com.bjpowernode.dao"/>
	</mappers>
	
</configuration></strong>
           

二、Spring架構所需配置檔案

1,Spring-db

<strong><?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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <!-- 注冊C3P0資料源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        	<property name="driverClass" value="${jdbc.driver}"/>
        	<property name="jdbcUrl" value="${jdbc.url}"/>
        	<property name="user" value="${jdbc.user}"/>
        	<property name="password" value="${jdbc.password}"/>
        </bean>
        
        <!-- 注冊屬性檔案 -->
        <context:property-placeholder location="classpath:resources/jdbc.properties"/>
        
</beans></strong>
           

2,Spring-mybatis

<strong><?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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <!-- 注冊SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        	<property name="configLocation" value="classpath:resources/mybatis.xml"/>
        	<property name="dataSource" ref="dataSource"/>
<!-- 當mybatis的xml檔案和mapper接口不在相同包下時,需要用mapperLocations屬性指定xml檔案的路徑. *是個通配符,代表所有的檔案,**代表所有目錄下 -->   
          <property name="mapperLocations" value="classpath:com/wj/mapper/*.xml" /> 
        </bean>
        
        <!-- 生成Dao的代理對象 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        	<property name="basePackage" value="com.bjpowernode.dao"/>
        </bean>
</beans></strong>
           

3,Spring-Service

<strong><?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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <!-- 注冊Service -->
        <bean id="studentService" class="com.bjpowernode.service.StudentServiceImpl">
        	<property name="dao" ref="IStudentDao"/>
        </bean>
        
</beans>
</strong>
           

4,Spring-tx

<strong><?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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        
        <!-- 注冊切面 -->
        <bean id="myAspect" class="com.bjpowernode.aspect.MyAspect"/>
        
        <!-- 注冊目标對象 -->
        <bean id="studentService" class="com.bjpowernode.service.StudentServiceImpl"/>
 
        <!-- 注冊事務管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource"/>
        </bean>
        
        <!-- 注冊事務通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
        	<tx:attributes>
        		<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
        	</tx:attributes>
        </tx:advice>
        
        <!-- AOP配置 -->
        <aop:config>
        	<aop:pointcut expression="execution(* *..service.*.*(..))" id="studentPointcut"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="studentPointcut"/>
        	
			<aop:aspect ref="myAspect">
				<aop:before method="myBefore" pointcut-ref="studentPointcut"/>
			</aop:aspect>
			
        </aop:config>
</beans></strong>
           

三、SpringMVC所需配置

<strong><?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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <!-- 注冊處理器 -->
        <bean id="/test/register.do" class="com.bjpowernode.handlers.StudentController">
        	<property name="service" ref="studentService"/>
        </bean>
        
</beans></strong>
           

四、其他配置檔案

1,jdbc.properties

<strong>jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.user=root
jdbc.password=161360238</strong>
           

繼續閱讀