zoukankan      html  css  js  c++  java
  • 原始DAO开发

    1,pojo

    package com.songyan.dao;
    
    import com.songyan.pojo.Student;
    
    public interface StudentDao {
        public void insertStudent(Student student);
        public void deleteStudent(String id);
        public void updateStudent(Student student);
        public Student selectStudent(Integer id);
    }
    package com.songyan.dao;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    
    import com.songyan.pojo.Student;
    
    public class StudentDaoImpl implements StudentDao{
        private SqlSessionFactory sqlSessionFactory;
        
        
    
        public StudentDaoImpl(SqlSessionFactory sqlSessionFactory) {
            super();
            this.sqlSessionFactory = sqlSessionFactory;
        }
    
        public void insertStudent(Student student) {
            
        }
    
        public void deleteStudent(String id) {
            
        }
    
        public void updateStudent(Student student) {
            
        }
    
        public Student selectStudent(Integer id) {
            SqlSession session=sqlSessionFactory.openSession();
            Student student=session.selectOne("test.findStudentById",10);
            return student;
        }
        
    
    }

    2,mapper

    <?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="findStudentById" parameterType="Integer"
            resultType="com.songyan.pojo.Student">
            select * from student where STU_ID= #{value}
        </select>
    </mapper>

    3,核心配置

    <?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>
    
        <!--配置环境,默认的环境id为oracle -->
        <environments default="oracle">
            <!-- 配置环境为oracle的环境 -->
            <environment id="oracle">
                <!--使用JDBC的事务处理 -->
                <transactionManager type="JDBC" />
                <!--数据库连接池 -->
                <dataSource type="POOLED">
                    <property name="driver" value="oracle.jdbc.driver.OracleDriver"></property>
                    <property name="url" value="jdbc:oracle:thin:@localhost:1521:inspur"></property>
                    <property name="username" value="scott"></property>
                    <property name="password" value="tiger"></property>
                </dataSource>
            </environment>
        </environments>
        <!--配置mapper的位置 -->
        <mappers>
            <mapper resource="com/songyan/dao/test.xml" />
            
        </mappers>
    </configuration>

    4,测试

    package com.songyan.client;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.songyan.dao.StudentDao;
    import com.songyan.dao.StudentDaoImpl;
    import com.songyan.pojo.Student;
    
    public class Test1 {
        SqlSessionFactory sqlSessionFactory;
        
        @Before
        public void test1() throws IOException
        {
            String resource="applicationContext.xml";
            InputStream in=Resources.getResourceAsStream(resource);
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
        }
        
        @Test
        public void test()
        {
            StudentDao studentDao=new StudentDaoImpl(sqlSessionFactory);
            Student student=studentDao.selectStudent(1);
            System.out.println(student);
        }
    }

     

  • 相关阅读:
    2018-2019-1 20165307 20165327 20165332 实验一 开发环境的熟悉
    2018-2019-1 20165332 《信息安全系统设计基础》第4周学习总结
    2018-2019-2 20165329 《网络对抗技术》Exp0 Kali安装 Week1
    20165311 20165329 20165334 实验五 通讯协议设计-1
    2018-2019-1 20165329 《信息安全系统设计基础》第九周学习总结
    20165329 20165311 20165334 实验四外设驱动程序设计
    20165329 mybash的实现
    20165329 实现mypwd
    2018-2019-1 20165329 《信息安全系统设计基础》第七周学习总结
    2018-2019-1 20165329 《信息安全系统设计基础》第6周学习总结
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9138999.html
Copyright © 2011-2022 走看看