1.创建mybatis-config.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>
<!--别名-->
<typeAliases>
<package name="com.bdqn.entity"/>
</typeAliases>
</configuration>
2.创建applicationcontext.xml
a.连接数据库
<!--配置dataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
</bean>
b.导入映射文件
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据源组件-->
<!--ref指引用对象-->
<property name="dataSource" ref="dataSource"/>
<!--引用MyBatis配置文件中的配置-->
<property name="configLocation" value="classpath:mybatis_config.xml"/>
<!--配置SQL映射文件信息-->
<property name="mapperLocations">
<list>
<value>classpath:com/bdqn/mapper/*.xml</value>
</list>
</property>
</bean>
c.引入properties文件
<!--引入properties文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:db.properties</value>
</property>
</bean>
d.Dao层和service层
<bean id="providerDao" class="com.bdqn.dao.Impl.ProviderDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
<bean id="providerService" class="com.bdqn.service.Impl.ProviderServiceImpl">
<property name="providerDao" ref="providerDao"/>
</bean>
3.创建实体类
a.dao层
b.mapper映射文件
c.service层
d.测试类Test
最终的运行结果如下