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 id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverclassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<!-- 3.1 配置SqlSessionTemplate(可省略) -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"></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层实现IEmpDao接口、EmpDaoImpl接口实现类、EmpMapper.xml映射文件
//----------------------------------------IEmpDao接口--------------------------------------------------------------
public interface IEmpDao {
public Emp getEmpByEmpno(int empno);
}
//------------------------------EmpDaoImpl接口实现类:继承SqlSessionDaoSupport接口--------------------------------------------------------------
public class EmpDaoImpl extends SqlSessionDaoSupport implements IEmpDao {
@Override
public Emp getEmpByEmpno(int empno) {
SqlSession session = this.getSqlSession();
Emp emp = (Emp)session.selectOne("getEmpByEmpno", empno);
return emp;
}
}
//-------------------------------------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="test">
<select id="getEmpByEmpno" parameterType="int" resultType="com.neuedu.model.Emp">
select * from emp where empno = #{empno}
</select>
</mapper>
6.在applicationContext.xml中管理dao层bean
<!-- 4.配置dao(原始dao开发方式) -->
<bean id="empDao" class="com.neuedu.dao.EmpDaoImpl">
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
7.在sqlMapConfig.xml中配置XXXMapper.xml映射文件
<mappers>
<mapper resource="com/neuedu/dao/EmpMapper.xml"/>
</mappers>
8.测 试
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
IEmpDao dao = (IEmpDao)factory.getBean("empDao");
Emp emp = dao.getEmpByEmpno(7499);
System.out.println(emp);
}