mybatis-spring整合

1,applicationContent.xml配置

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.	    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
5.	  
6.	    <!--配置数据源属性文件  -->  
7.	    <bean id="propertyConfigurer"  
8.	        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
9.	        <property name="location">  
10.	            <value>/WEB-INF/classes/mysql.properties</value>  
11.	        </property>  
12.	    </bean>  
13.	  
14.	    <!--配置数据源  -->  
15.	    <bean id="dataSource"  
16.	        class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
17.	        <property name="driverClassName">  
18.	            <value>${jdbc.driver}</value>  
19.	        </property>  
20.	        <property name="url">  
21.	            <value>${jdbc.url}</value>  
22.	        </property>  
23.	        <property name="username">  
24.	            <value>${jdbc.user}</value>  
25.	        </property>  
26.	        <property name="password">  
27.	            <value>${jdbc.pwd}</value>  
28.	        </property>  
29.	    </bean>  
30.	  
31.	 <!-- 创建SqlSessionFactory,同时指定数据源-->
32.	    <bean id="sqlSessionFactory"  
33.	        class="org.mybatis.spring.SqlSessionFactoryBean">  
34.	        <property name="configLocation"  
35.	            value="classpath:org/cfsdc/sqlMapper/mybatis_config.xml" />  
36.	        <property name="dataSource" ref="dataSource" />  
37.	    </bean>  
38.	  <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->  
39.	      
40.	   <!--创建数据映射器,数据映射器必须为接口-->  
41.	    <!-- test example @xiaoliu-->  
42.	    <bean id="testDao"  
43.	        class="org.mybatis.spring.mapper.MapperFactoryBean">  
44.	        <property name="mapperInterface" value="org.cfsdc.dao.ITestDao" />  
45.	        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
46.	    </bean>  
47.	    <bean id="testAction" class="org.cfsdc.action.TestAction">  
48.	        <property name="testDao" ref="testDao"></property>  
49.	    </bean>  

  

 

mybatis配置文件

2,mybatis_config.xml

1.	<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
2.	<configuration>  
3.	    <!-- settings -->  
4.	    <settings>  
5.	        <setting name="cacheEnabled" value="true" />  
6.	    </settings>  
7.	  
8.	    
9.	    <typeAliases>  
10.	        <typeAlias alias="CfsdcTestBean"  
11.	            type="org.cfsdc.entity.CfsdcTestBean" />  
12.	  
13.	     
14.	  
15.	    </typeAliases>  
16.	  
17.	  
18.	    <mappers>  
19.	        <mapper resource="org/cfsdc/sqlMapper/mapper/testMapper.xml" /><mappers> 
20.	  <configuration>  

 

你可能感兴趣的:(mybatis)