mybatis-mybatis与spring整合的基本配置

来源:互联网 发布:电脑写小说的软件 编辑:程序博客网 时间:2024/06/05 08:38
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 数据源,使用dbcp --><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=stdb1"></property><property name="username" value="sa"></property><property name="password" value="hyj84884824"></property><property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property></bean><!-- sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 加载mybatis的配置文件 --><property name="configLocation" value="mybatis/SqlMapConfig.xml"/><!-- 数据源 --><property name="dataSource" ref="dataSource"/></bean><!-- <bean id="UserDao" class="com.itcast.ssm.dao.UserDao"><property name="sqlSessionFactory" ref="sqlSessionFactory"/></bean> --> <!-- mapper配置  <bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  mapperInterface指定mapper接口  <property name="mapperInterface" value="com.itcast.ssm.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>--><!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并在spring容器中注册 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 指定扫描的包名 --><property name="basePackage" value="com.itcast.ssm.mapper"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean></beans>
SqlMapConfig.xml:
<?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><mappers><mapper resource="sqlmap/User.xml"/><!-- <mapper class="com.itcast.ssm.mapper.UserMapper"/>  --></mappers></configuration>


原创粉丝点击