Mybatis(数据访问层)

Sping+SpingMVC+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、配置完成后,书写代码进行测试。

相关文章:

  • 2021-04-11
  • 2022-12-23
  • 2021-07-18
  • 2021-04-15
  • 2021-11-16
  • 2021-06-21
  • 2022-12-23
猜你喜欢
  • 2021-06-18
  • 2021-10-24
  • 2021-05-04
  • 2021-11-28
相关资源
相似解决方案