package com.jlins.dao;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.jlins.IbatisClient;
import com.jlins.bean.Student;
import com.jlins.bean.Teacher;
public class IbatisDAO implements IStudentDAO{
SqlMapClient client = null;
public void addStudent(Student student) {
client=new IbatisClient().getSqlMapInstance();
try {
client.insert("insertStudent",student);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addStudentBySequence(Student student) {
//oracle创建序列的方法 create sequence studentPKSequence start with 1 increment by 1;
//1.从数据库序列中获取主键值
//2.往student表中插入记录
client=new IbatisClient().getSqlMapInstance();
try {
client.insert("insertStudentBySquence",student);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteStudentById(int id) {
client=new IbatisClient().getSqlMapInstance();
try {
System.out.println(client.delete("deleteStudentById", 6));
} catch (SQLException e) {
e.printStackTrace();
}
}
public Student queryStudentById(int id) {
client=new IbatisClient().getSqlMapInstance();
Student student =null;
try {
student =(Student) client.queryForObject("selectStudentById", 1);
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
public List<Student> queryAllStudent() {
List<Student> studentlist=null;
client=new IbatisClient().getSqlMapInstance();
try {
studentlist=client.queryForList("selectAllStudent");
System.out.println(studentlist.size());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return studentlist;
}
public List<Student> queryStudentByName(String name) {
List<Student> studentlist=null;
client=new IbatisClient().getSqlMapInstance();
try {
studentlist =client.queryForList("selectStudentByName",name);
System.out.println(studentlist.size());
} catch (SQLException e) {
e.printStackTrace();
}
return studentlist;
}
public void updateStudent(Student student) {
client=new IbatisClient().getSqlMapInstance();
try {
client.update("updateStudent", student);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void batchOneToMany() {
try {
long start =System.currentTimeMillis();
//只有添加了事物之后 ibatis批处理才会执行
//哈哈 一对多的问题成功的解决了插入
//批处理关联一万条才一秒多 哈哈
client.startTransaction();
client.startBatch();
Student student =new Student();
student.setBirth(new Date());
student.setId(6);
student.setMajor("i love it");
student.setScore(99);
student.setSname("jlins6");
client.insert("insertStudentBySquence",student);
for(int i=0;i<15555;i++){
Teacher teacher=new Teacher();
teacher.setId(i);
teacher.setStudent(student);
client.insert("insertTeacher",teacher);
}
client.executeBatch();
client.commitTransaction();
client.endTransaction();
long time=System.currentTimeMillis()-start;
System.out.println(time);
} catch (SQLException e) {
e.printStackTrace();
}finally{
}
}
public List<Teacher> queryTeacherById(int id) {
List<Teacher> teacherlist=null;
client=new IbatisClient().getSqlMapInstance();
try {
long start=System.currentTimeMillis();
// teacherlist=client.queryForList("selectTeacherById", id);
//查询teacher关联student查询用接近20秒
// teacherlist=client.queryForList("selectAllTeacher");
//只查询teacher不关联查询 2万条记录只用2秒多
// teacherlist=client.queryForList("selectAllTeacherOnly");
//使用左查询链接 只需要 不到3秒就可以
teacherlist=client.queryForList("selectAllTeacherLeft");
System.out.println(teacherlist.size());
System.out.println(System.currentTimeMillis()-start);
} catch (SQLException e) {
e.printStackTrace();
}
return teacherlist;
}
public static void main(String args[]){
IStudentDAO dao =new IbatisDAO();
//1.
/* for(Student student:dao.queryAllStudent()){
System.out.println(student.getId());
System.out.println(student.getMajor());
System.out.println(student.getSname());
System.out.println(student.getBirth());
System.out.println(student.getScore());
}*/
//2.
/* Student student =dao.queryStudentById(1);
System.out.println(student.getId());
System.out.println(student.getMajor());
System.out.println(student.getSname());
System.out.println(student.getBirth());
System.out.println(student.getScore());*/
//3.
/* Student student =new Student();
student.setBirth(new Date());
student.setId(6);
student.setMajor("i love it");
student.setScore(99);
student.setSname("jlins6");
dao.addStudent(student);*/
//4.
/* dao.deleteStudentById(6);*/
//5.
/* Student student =new Student();
student.setBirth(new Date());
student.setId(6);
student.setMajor("i love that");
student.setScore(999);
student.setSname("jlins6_update");
dao.updateStudent(student);*/
//6.
/* for(Student student:dao.queryStudentByName("jlins")){
System.out.println(student.getId());
System.out.println(student.getMajor());
System.out.println(student.getSname());
System.out.println(student.getBirth());
System.out.println(student.getScore());
}
*/
//7.
/* Student student =new Student();
student.setBirth(new Date());
student.setId(6);
student.setMajor("i love it");
student.setScore(99);
student.setSname("jlins6");
dao.addStudentBySequence(student);
//打印出来的是更新以后的id很是个不错
System.out.println(student.getId());*/
//一对多关系先存储一的一端,然后批处理存储多的一端
//?? 问题来了 一的一段和多的一段都采用数据库自增的方式,批处理如何能正确的确定id
//8.
// dao.batchOneToMany();
// dao.queryTeacherById(50);
for(Teacher teacher:dao.queryTeacherById(50)){
System.out.println(teacher.getId());
System.out.println(teacher.getStudent().getMajor());
System.out.println(teacher.getStudent().getSname());
System.out.println(teacher.getStudent().getBirth());
System.out.println(teacher.getStudent().getScore());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
ibatis 完美例子 一对多 批处理 事务 和 spring struts2集成
共69个文件
xml:19个
java:13个
class:13个
4星 · 超过85%的资源 需积分: 9 130 下载量 124 浏览量
2009-09-10
21:50:14
上传
评论 2
收藏 51KB RAR 举报
温馨提示
ibatis 完美例子 一对多 批处理 事务 和 spring struts2集成 ,一朵多 插入1万条数据,不到2秒,备注不包含类库
资源推荐
资源详情
资源评论
收起资源包目录
ibatis完美例子(增删改查、批处理、事务、与spring和struts2集成、备注 没有放类库).rar (69个子文件)
ibatis
src
com
jlins
dao
IbatisDAO.java 6KB
IStudentDAO.java 1KB
orm
bean
Student.java 794B
Teacher.java 322B
IbatisClient.java 682B
SqlMapConfig.xml 1KB
Student.xml 3KB
SqlMap.properties 113B
bin
com
jlins
dao
IbatisDAO.class 5KB
IStudentDAO.class 706B
IbatisClient.class 1KB
orm
bean
Teacher.class 746B
Student.class 1KB
SqlMapConfig.xml 1KB
Student.xml 3KB
SqlMap.properties 113B
.classpath 407B
.settings
org.eclipse.core.resources.prefs 88B
.project 382B
SSIbatis
.springBeans 420B
src
sql-map-2.dtd 14KB
init.properties 253B
com
jlins
manager
IbatisManager.java 489B
dao
IbatisDAO.java 6KB
IStudentDAO.java 1KB
action
IbatisAction.java 1KB
bean
Student.java 794B
Teacher.java 322B
User.java 56B
interceptor
AuthorityIntercepter.java 729B
applicationContext-manager.xml 794B
applicationContext-common.xml 3KB
struts.xml 1KB
oscache加速你的应用程序 3KB
log4j.properties 1KB
ibatis-sql-map-config.xml 334B
oscache.properties 5KB
applicationContext-action.xml 799B
Student.xml 3KB
sql-map-config-2.dtd 2KB
applicationContext-dao.xml 782B
.classpath 3KB
.mymetadata 297B
.settings
org.eclipse.jdt.core.prefs 330B
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
index.jsp 825B
WEB-INF
classes
sql-map-2.dtd 14KB
init.properties 253B
com
jlins
manager
IbatisManager.class 818B
dao
IbatisDAO.class 4KB
IStudentDAO.class 706B
action
IbatisAction.class 2KB
bean
User.class 267B
Teacher.class 746B
Student.class 1KB
interceptor
AuthorityIntercepter.class 1KB
applicationContext-manager.xml 794B
applicationContext-common.xml 3KB
struts.xml 1KB
oscache加速你的应用程序 3KB
log4j.properties 1KB
ibatis-sql-map-config.xml 334B
oscache.properties 5KB
applicationContext-action.xml 799B
Student.xml 3KB
sql-map-config-2.dtd 2KB
applicationContext-dao.xml 782B
lib
web.xml 2KB
共 69 条
- 1
资源评论
- zqy10301632042013-06-05不错 很好用
- yeren01432014-10-04很好,不错的资源
- JVM2013-07-25我想专门了解一对多和多对多的配置,但是其中没有啊
- MAX2013-05-14很好找到我需要的关键配置了
- andy84539968392012-12-12只有代码,没有表,数据都没有,怎么体现出查询速度的价值
Jlins
- 粉丝: 5882
- 资源: 173
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- SSCMS登录模块需要的JS文件
- JSP网络购物中心毕业设计(源代码+论文).rar
- 白盒测试报告.docx
- 基于LM5117芯片评估开发板硬件参考设计(原理图+PCB)+中英文数据手册资料.zip
- 照片批量重命名软件(文件批量修改图片文件名)
- app.apk
- 人工智能(AI)是计算机科学的一个分支,旨在开发和应用能够模拟、延伸和扩展人类智能的理论、方法和技术,包括机器人、语言识别、图像
- 嵌入式与物联网开发是当今信息技术领域的两大重要分支,它们相互交织,共同推动着智能化时代的进步 嵌入式开发主要关注在嵌入式操作
- 网络安全,这一看似高深莫测的领域,实则与我们每个人的生活息息相关
- 毕业设计基于深度学习的视觉问答系统源码+文档说明+答辩PPT.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功