• SSM——Spring+Mybatis整合(原始dao开发模式)


    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);
    }

  • 相关阅读:
    node03- FS内置模块
    node03- CommonJS
    删除当前目录下的所有文件夹和文件
    解决 idea 项目中Error:java: 无效的标记
    Raid0,Raid1,Raid5,Raid10 总结
    Tcpdump命令
    ClassNotFoundException 和 NoClassDefFoundError 区别
    Dart-List里面常用的属性和方法
    CSS实现等分布局的4种方式
    iOS项目添加CocoaPods
  • 原文地址:https://www.cnblogs.com/ccw95/p/6185901.html
Copyright © 2020-2023  润新知