1. 项目搭建
2. 导入项目整合jar包
mybatis-spring-1.2.4.jar
commons-dbcp2-2.1.1.jar
commons-pool2-2.4.2.jar
3. 在applicationContex.xml配置数据源dataSource、配置SqlSessionFactory、配置SqlSessionTemplate(可省略)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 1. 获取数据源配置文件db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2. 配置数据源 -->
<bean ></property>
注意:若mapper.xml在单独的文件夹内,需要声明位置
如:<property name="mapperLocation" value="com/neuedu/mapper/xml/*.xml"></property>
</bean>
<!-- 3.1 配置SqlSessionTemplate(可省略) -->
<bean ></constructor-arg>
</bean>
</beans>
4.在类路径下配置db.properties数据库配置信息
jdbc.driverclassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username = scott
jdbc.password = tiger
5. 创建dao层实现EmpMapper接口、EmpMapper.xml映射文件
//----------------------------------------EmpMapper接口--------------------------------------------------------------
public interface EmpMapper {
//<select >
public Emp getEmpByEmpno(int empno);
}
//-------------------------------------EmpMapper.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">
<mapper namespace="com.neuedu.dao.mapper.EmpMapper">
<select );
Emp emp = dao.getEmpByEmpno(7499);
System.out.println(emp);
}