原始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);
}
}
原始DAO开发的更多相关文章
- MyBatis原始dao开发及问题总结(五)
一.MyBatis原始Dao开发方式 1.原始dao开发需要程序员编写dao接口和dao接口实现类 编写UserDao接口:UserDao.java package codeRose.dao; pub ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- Mybatis学习(2)原始dao开发和使用mapper接口代理开发
基础知识: 1).SqlSessionFactoryBuilder: 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory.将SqlSessionFact ...
- mybatis由浅入深day01_5mybatis开发dao的方法(5.1SqlSession使用范围_5.2原始dao开发方法)
5 mybatis开发dao的方法 5.1 SqlSession使用范围 5.1.1 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂 ...
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...
- 【MyBatis学习03】原始dao开发方法及其弊端
上一篇博文总结了一下mybatis的入门,接下来就要开发dao方法了,这篇博文主要总结一下mybatis中原始dao开发的方法,最后并总结一下原始dao开发方法的弊端.mybatis中dao开发应该使 ...
- MyBatis开发Dao的原始Dao开发和Mapper动态代理开发
目录 咳咳...初学者看文字(Mapper接口开发四个规范)属实有点费劲,博主我就废了点劲做了如下图,方便理解: 原始Dao开发方式 1. 编写映射文件 3.编写Dao实现类 4.编写Dao测试 Ma ...
- Mybatis的原始dao开发方法
在进入主题之前先提一下sqlSession.sqlSession是一个面向用户(程序员)的接口. sqlSession中提供了很多操作数据库的方法,如: selectOne(返回单个对象).selec ...
- Mybatis 和Spring整合之原始dao开发
F:\Aziliao\mybatis\代码\31.mybatis与spring整合-开发原始dao 1.1. SqlMapConfig.xml <?xml version="1.0&q ...
- Mybatis笔记 - 原始Dao开发方法
使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.原始Dao的开发方式是基于入门程序的基础上,对 控制程序 进行分层开发,程序员需要 编写 Dao接口 和 ...
随机推荐
- canvas压缩图片变模糊问题
canvas 画图图片变模糊问题 问题描述 在使用 canvas 对图片进行编辑导出图片之后发现图片和原图相比变得模糊了 canvas 画图线条变粗 问题产生原因 该问题在 PC 下面并不会产生,原因 ...
- UVa10288概率
题意: 每张彩票上印有一张图案,要集齐n个不同的图案才能获奖.输入n,求要获奖购买彩票张数的期望(假设获得每个图案的概率相同). 分析: 假设现在已经有k种图案,令s = k/n,得到一个新图案需要t ...
- 51nod 1254 最大子段和 V2 ——单调栈
N个整数组成的序列a[1],a[2],a[3],…,a[n],你可以对数组中的一对元素进行交换,并且交换后求a[1]至a[n]的最大子段和,所能得到的结果是所有交换中最大的.当所给的整数均为负数时和为 ...
- Codeforces Round #299 Div2 解题报告
这场比赛并没有打现场,昨天晚上做了ABCD四道题,今天做掉了E题 以前还没有过切完一场比赛的所有题呢~爽~ A. Tavas and Nafas Today Tavas got his test ...
- NGINX: 限制连接的实践 (Defense DDOS)
参考: [ nginx防止DDOS攻击配置 ] 关于限制用户连接,Nginx 提供的模块: [ ngx_http_limit_req_module ] [ ngx_http_limit_conn_mo ...
- JavaScript DOM编程艺术 读书笔记
2. JavaScript语法 2.1 注释 HTML允许使用"<!--"注释跨越多个行,但JavaScript要求这种注释的每行都必须在开头加上"< ...
- 链接加载文件gcc __attribute__ section
在阅读源代码的过程中,发现一个头文件有引用: /** The address of the first device table entry. */ extern device_t devices[] ...
- HDU1217 (Floyd简单变形)
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- js反混淆工具
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
- servlet+forward和direct区别
Servlet:是用于 java 编写的服务器端程序,其使用 java servlet API,当客户机发送请求到服务器时,服务器可以将请求信息发送给 servlet,并让 servlet 建立起服务 ...