![]() |
#2
sweet6hero2013-11-11 17:28
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-// Config 3.0//EN" "http:// <configuration> <typeAliases> <typeAlias alias="User" type="com.pojo.User" /> <typeAlias alias="Article" type="com.pojo.Article" /> </typeAliases> <!-- 与spring 集成之后,这些可以完全删除,数据库连接的管理交给 spring 去管理 --> <!-- <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8" /> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> --> <mappers> <mapper resource="com/pojo/UserMapper.xml" /> <mapper resource="com/pojo/CategoryMapper.xml" /> <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 --> <!--<package name="com.tiantian.mybatis.mapperinterface"/> --> <!-- 通过mapper元素的resource属性可以指定一个相对于类路径的Mapper.xml文件 --> <!--<mapper resource="com/tiantian/mybatis/mapper/UserMapper.xml"/> --> <!-- 通过mapper元素的url属性可以指定一个通过URL请求道的Mapper.xml文件 --> <!--<mapper url="file:///E:/UserMapper.xml"/> --> <!-- 通过mapper元素的class属性可以指定一个Mapper接口进行注册 --> <!--<mapper class="com.tiantian.mybatis.mapperinterface.UserMapper"/> --> </mappers> </configuration> |
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.
xmlns:xsi="http://www. xmlns:aop="http://www.
xmlns:tx="http://www. xmlns:context="http://www.
xsi:schemaLocation="
http://www. http://www.
http://www. http://www.
http://www. http://www.
http://www. http://www.
http://www. http://www.
default-autowire="byName" default-lazy-init="false">
<!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->
<bean id="dataSource" class="org.
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3309/mybatis?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池 -->
<property name="dataSource" ref="dataSource" />
<!--注册Mapper方式一:configLocation属性指定mybatis的核心配置文件 -->
<property name="configLocation" value="config/Configuration.xml" />
<!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名, 且在同一路径下,那么可以不配置该选项 -->
<!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件, mapperLocations和configLocation有一个即可,
当需要为实体类指定别名时,可指定configLocation属性, 再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
<!--<property name="mapperLocations" value="com/pojo/UseMapper.xml"/> -->
</bean>
<!-- 不须要有映射文件,需要IUserOperation接口 -->
<!-- 注册Mapper方式一:通过sqlSessionFactory获得IUserOperation接口,配置如下: -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象,所以不需要配置文件 -->
<property name="mapperInterface" value="dao.Interface.IUserOperation" />
</bean>
<bean id="CategoryMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
<property name="mapperInterface" value="dao.Interface.CategoryMapper" />
</bean>
<!-- 将获得的接口注入到ServiceImpl类 -->
<bean id="UserServiceImpl1" class="server.serverImpl.userserviceimp1">
<property name="userDao" ref="userMapper"></property>
</bean>
<!-- 不须要有映射文件,需要IUserOperation接口 -->
<!-- 需要IUserOperation接口接口中要用@注解的sql语句 -->
<!-- 注册Mapper方式二:使用自动扫描包的方式来注册各种Mapper ,获得IUserOperation接口,配置如下: -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao.Interface" />
</bean>
<bean id="UserServiceImpl2" class="server.serverImpl.userserviceimp1">
<property name="userDao" ref="IUserOperation"></property>
</bean>
<!--必须要mapper映射文件 -->
<!-- 注解mapper方式三:ServiceImpl类采用 sqlSessionTemplate或 者继承SqlSessionDaoSupport类,获取mapper映射文件中的方法 -->
<!-- 如果mapper映射文件中采用class则自动扫描接口中注解生成映射 -->
<!-- 如果mapper映射文件中采用resource则自动扫描映射文件生成映射 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="UserServiceImpl3" class="server.serverImpl.userserviceimp2">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="UserServiceImpl4" class="server.serverImpl.userserviceimp3">
<property name="sqlSession" ref="sqlSessionTemplate"></property>
</bean>
</beans>