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

最新文章

  1. Android中BroadcastReceiver的两种注册方式(静态和动态)详解
  2. Oracle学习笔记五 SQL命令(三):Group by、排序、连接查询、子查询、分页
  3. js中 javascript:void(0) 用法详解
  4. 数据库模型设计PowerDesigner
  5. invoke
  6. Windows下启动各服务命令
  7. 串口通信类,WPF
  8. makefile文件的技术
  9. CSS实现标题超出宽度省略号来表示
  10. 【BZOJ 3172】 [Tjoi2013]单词
  11. [cocos2d-x] 让精灵响应触摸 并把方向旋转到相对应的角度
  12. 腾讯QQ音乐网页版 音频初始化模块解压混淆js源码
  13. bzoj3504: [Cqoi2014]危桥 网络流
  14. 上海赛趣-top.mainFrame.tabAddHandler方法详解
  15. 最新城市二级联动json(2017-09)
  16. Oracle 12.2报错ORA-15032、ORA-15410或ORA-15411解决
  17. Java开发各层对象专用名词含义 PO,VO,DAO,BO,DTO,POJO, BYO,Entity,JavaBean,JavaBeans
  18. NOIP2018备考——DP专题练习
  19. 使用Setup安装Windows8 RTM方法
  20. JavaScript预编译详解

热门文章

  1. Install the Active Directory Administration Tools on Windows Server
  2. (转)Django常用命令
  3. [04] css 选择器
  4. Win7/8 绿色软件开机启动
  5. LABVIEW伺服电机测试平台
  6. AtCoder Regular Contest 082 E
  7. NYOJ 284 坦克大战 (广搜)
  8. 常见协议基础知识总结--DHCP协议
  9. STL各个数据结构特点
  10. 【bzoj4282】慎二的随机数列