SSM——Spring+Mybtis整合(代理【mapper】开发模式)

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>

注意:若mapper.xml在单独的文件夹内,需要声明位置

  如:<property name="mapperLocation" value="com/neuedu/mapper/xml/*.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层实现EmpMapper接口、EmpMapper.xml映射文件
//----------------------------------------EmpMapper接口--------------------------------------------------------------

public interface EmpMapper {
//<select id="getEmpByEmpno" parameterType="int" resultType="com.neuedu.model.Emp">
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 id="getEmpByEmpno" parameterType="int" resultType="com.neuedu.model.Emp">
select * from emp where empno = #{empno}
</select>
</mapper>

6.在applicationContext.xml中管理dao层bean
<!-- 4.1 配置dao(mapper代理方式)-->
<!-- <bean id="empMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
配置接口
<property name="mapperInterface" value="com.neuedu.dao.mapper.EmpMapper"></property>
</bean>
<bean id="deptMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
配置接口
<property name="mapperInterface" value="com.neuedu.dao.mapper.DeptMapper"></property>
</bean> -->

<!-- 4.2 可整合并替换4.1部分所有配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置mapper接口所在包 -->
<property name="basePackage" value="com.neuedu.dao.mapper"></property>
<!-- 配置sqlSessionFactory(可省略,会自动注入) -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

7.在sqlMapConfig.xml中配置XXXMapper.xml映射文件
<mappers>
<!-- 基于mapper配置方法 -->
<!-- <mapper resource="com/neuedu/dao/mapper/EmpMapper.xml"/>
<mapper class="com.neuedu.dao.mapper.EmpMapper"/> -->
<package name="com.neuedu.dao.mapper"/>
</mappers>

8.测 试
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

EmpMapper dao = (EmpMapper)factory.getBean("empMapper");

Emp emp = dao.getEmpByEmpno(7499);

System.out.println(emp);
}

posted @ 2016-12-16 11:15  龙之天族  阅读(1730)  评论(0编辑  收藏  举报