Mybatis(数据访问层)
数据访问的方式为:controller-->service-->Dao接口-->Mybatis的xml文件;只是负责在Mybatis的xml中写sql语句,不用知道是如何调用的,只需和Dao接口一一对应的建立,例如:UserDao.java------UserDao.xml.
1、Mybatis配置文件
在src下面创建:mybatis-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.wy.model" />
</typeAliases>
</configuration>
2、此时,我们需要去applicationContext.xml中添加mybatis的配置。这样mybatis才会调用。
在之前的applicationContext.xml中添加以下配置:
<!-- 配置 MyBatis的工厂 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBatis的核心配置文件所在位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 接口开发,扫描 com.wy.dao包 ,写在此包下的接口即可被扫描到 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wy.dao" />
</bean>
3、配置完成后,书写代码进行测试。