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

原始DAO开发的更多相关文章

  1. MyBatis原始dao开发及问题总结(五)

    一.MyBatis原始Dao开发方式 1.原始dao开发需要程序员编写dao接口和dao接口实现类 编写UserDao接口:UserDao.java package codeRose.dao; pub ...

  2. Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...

  3. Mybatis学习(2)原始dao开发和使用mapper接口代理开发

    基础知识: 1).SqlSessionFactoryBuilder: 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory.将SqlSessionFact ...

  4. mybatis由浅入深day01_5mybatis开发dao的方法(5.1SqlSession使用范围_5.2原始dao开发方法)

    5 mybatis开发dao的方法 5.1 SqlSession使用范围 5.1.1 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂 ...

  5. Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合

    Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...

  6. 【MyBatis学习03】原始dao开发方法及其弊端

    上一篇博文总结了一下mybatis的入门,接下来就要开发dao方法了,这篇博文主要总结一下mybatis中原始dao开发的方法,最后并总结一下原始dao开发方法的弊端.mybatis中dao开发应该使 ...

  7. MyBatis开发Dao的原始Dao开发和Mapper动态代理开发

    目录 咳咳...初学者看文字(Mapper接口开发四个规范)属实有点费劲,博主我就废了点劲做了如下图,方便理解: 原始Dao开发方式 1. 编写映射文件 3.编写Dao实现类 4.编写Dao测试 Ma ...

  8. Mybatis的原始dao开发方法

    在进入主题之前先提一下sqlSession.sqlSession是一个面向用户(程序员)的接口. sqlSession中提供了很多操作数据库的方法,如: selectOne(返回单个对象).selec ...

  9. Mybatis 和Spring整合之原始dao开发

    F:\Aziliao\mybatis\代码\31.mybatis与spring整合-开发原始dao 1.1. SqlMapConfig.xml <?xml version="1.0&q ...

  10. Mybatis笔记 - 原始Dao开发方法

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.原始Dao的开发方式是基于入门程序的基础上,对 控制程序 进行分层开发,程序员需要 编写 Dao接口 和 ...

随机推荐

  1. kafka.common.ConsumerRebalanceFailedException异常解决

    kafka.common.ConsumerRebalanceFailedException: group_dd-1446432618163-2746a209 can't rebalance after ...

  2. 异常message:There is no database named cloudera_manager_metastore_canary_test_db_hive_hivemetastore

    NoSuchObjectException(message:There is no database named cloudera_manager_metastore_canary_test_db_h ...

  3. 更改win10和mint双系统默认启动顺序

    更改win7 & Linuxmint双系统安装后更改默认启动顺序 1.打开一个term,编辑/etc/default/grub,即sudo nano /etc/default/grub,把se ...

  4. keydown

    <!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>无标 ...

  5. php getimagesize()函数获取图片宽度高度

    //php自带函数 getimagesize() $img_info = getimagesize('tomener.jpg'); echo '<pre>'; print_r($img_i ...

  6. BigDecimal与Long、int之间的互换

    在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigDecimal 数据类型 //int 转换成 big ...

  7. 计算代码行数Demo源码

    源码下载:04-计算代码行数.zip24.1 KB////  main.m//  计算代码行数////  Created by apple on 13-8-12.//技术博客http://www.cn ...

  8. [object-c 2.0 程序设计]object-c file handle (二)

    // // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. Al ...

  9. Selenium tutorial/overview

    copy from: http://www.jroller.com/selenium/ Selenium tutorial/overview 1. Selenium Introduction 2. S ...

  10. 教你从头到尾利用DQN自动玩flappy bird(全程命令提示,GPU+CPU版)【转】

    转自:http://blog.csdn.net/v_JULY_v/article/details/52810219?locationNum=3&fps=1 目录(?)[-] 教你从头到尾利用D ...