上次已经搭建了基础框架所需的基础中的基础,下面写会话工厂mybatis
只负责在xml中写sql语句,不用知道如何调用的,和dao接口一一对应
配置
在applicationContext.xml中添加mybatis配置
<!-- 配置 MyBatis的工厂 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBatis的核心配置文件所在位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 接口开发,扫描 com.liu.dao包 ,写在此包下的接口即可被扫描到 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zq.dao" />
</bean>
新建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.liu.model" />
</typeAliases>
</configuration>
新建UserDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--命名空间。XML文件和dao接口对应起来 -->
<mapper namespace="com.liu.dao.UserDao">
<!-- 查询列表 -->
<select id="list" parameterType = "user" resultType="user">
select * from user
where username = #{username} and pwd = #{pwd}
</select>
</mapper>