博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.MyBatis有代理增删改
阅读量:5282 次
发布时间:2019-06-14

本文共 5861 字,大约阅读时间需要 19 分钟。

2.1 创建一个javaweb项目MyBatis_Part1,并创建如下sql脚本

1 create user holly identified by sys; 2 grant dba to holly; 3 conn holly/sys 4 create table teacher(id number primary key,tname varchar2(20),pwd varchar2(20)); 5 create sequence seq_teacher; 6 insert into teacher values(seq_teacher.nextval,'holly','123'); 7 insert into teacher values(seq_teacher.nextval,'石头','123'); 8 insert into teacher values(seq_teacher.nextval,'朱亚楠','123'); 9 insert into teacher values(seq_teacher.nextval,'陈明','123');10 commit;

 

2.2 在项目的src下创建Configuration.xml主配置文

1 
2 3
4
5
6
7
8 9
10
11
12
13
14
15
16
17
18
19
20
21 22
23
24
25
26
27
28 29 30 31

2.3 在src下的com.entity包下创建Teacher.java实体类

1 package com.entity; 2  3 public class Teacher { 4     private int id; 5     private String tname; 6     private String pwd; 7     public Teacher() { 8     } 9     10     public Teacher(String tname, String pwd) {11         this.tname = tname;12         this.pwd = pwd;13     }14 15     public Teacher(int id, String tname, String pwd) {16         this.id = id;17         this.tname = tname;18         this.pwd = pwd;19     }20 21     public int getId() {22         return id;23     }24 25     public void setId(int id) {26         this.id = id;27     }28 29     public String getTname() {30         return tname;31     }32 33     public void setTname(String tname) {34         this.tname = tname;35     }36 37     public String getPwd() {38         return pwd;39     }40 41     public void setPwd(String pwd) {42         this.pwd = pwd;43     }44 45     @Override46     public String toString() {47         return "Teacher [id=" + id + ", pwd=" + pwd + ", tname=" + tname + "]";48     }49 }
Teacher.java

2.4 在src下的com.mapper包下创建TeacherMapper.java映射接口

1 package com.mapper; 2  3 import java.util.List; 4  5 import com.entity.Teacher; 6  7 public interface TeacherMapper { 8     //1.查所有 9     List
findAll();10 //2.根据id查询11 Teacher findById(int id);12 //3.添加13 int saveTeacher(Teacher teacher);14 //4.修改15 int updateTeacher(Teacher teacher);16 //5.删除17 int deleteTeacher(int id);18 19 20 }
TeacherMapper.java

2.5在src下的com.mapper包下创建TeacherMapper.xml映射配置文件

1 
2 3
4
5
6
9 10
11
14 15
16
17 insert into teacher values(seq_teacher.nextval,#{tname},#{pwd})18
19 20
21
22 update teacher set tname=#{tname},pwd=#{pwd}23
24 25
26
27 delete from teacher where id=#{id}28
29
TeacherMapper.xml

2.6在src下的com.mapper.impl包下创建TeacherMapperImpl.java映射实现类文件

1 package com.mapper.impl; 2  3 import java.util.List; 4  5 import org.apache.ibatis.session.SqlSession; 6  7 import com.entity.Teacher; 8 import com.mapper.TeacherMapper; 9 import com.util.MyBatisUtil;10 11 public class TeacherMapperImpl implements TeacherMapper {12     /**13      *1.查询所有14      *     */15     public List
findAll() {16 SqlSession sqlsession=MyBatisUtil.getSqlSession(false);17 //利用反射产生代理对象(动态代理)18 TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);19 try {20 List
list=mapper.findAll();21 sqlsession.close();22 return list;23 } catch (Exception e) {24 e.printStackTrace();25 return null;26 }27 28 }29 /**30 * 2.查询单条31 */32 public Teacher findById(int id) {33 SqlSession sqlsession=MyBatisUtil.getSqlSession(false);34 TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);35 Teacher teacher=null;36 try {37 teacher = mapper.findById(id);38 sqlsession.close();39 return teacher;40 } catch (Exception e) {41 e.printStackTrace();42 return null;43 }44 }45 /**46 * 添加47 */48 public int saveTeacher(Teacher teacher) {49 SqlSession sqlsession=MyBatisUtil.getSqlSession(true);50 TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);51 try {52 int num=mapper.saveTeacher(teacher);53 sqlsession.close();54 return num;55 } catch (Exception e) {56 e.printStackTrace();57 58 return 0;59 }60 }61 /**62 * 4.修改63 */64 public int updateTeacher(Teacher teacher) {65 SqlSession sqlsession=MyBatisUtil.getSqlSession(false);66 TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);67 try {68 int num=mapper.updateTeacher(teacher);69 sqlsession.close();70 return num;71 } catch (Exception e) {72 e.printStackTrace();73 return 0;74 }75 }76 /**77 * 5.根据id删除78 */79 public int deleteTeacher(int id) {80 SqlSession sqlsession=MyBatisUtil.getSqlSession(false);81 TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);82 try {83 int num=mapper.deleteTeacher(id);84 sqlsession.close();85 return num;86 } catch (Exception e) {87 e.printStackTrace();88 return 0;89 }90 }91 92 }
TeacherMapperImpl.java

 

转载于:https://www.cnblogs.com/holly8/p/5791723.html

你可能感兴趣的文章
如何将数据库中的表导入到PowerDesigner中(转)
查看>>
汇编总结一
查看>>
html5-表单常见操作
查看>>
String = ""和String = null的区别
查看>>
C#测试题若干,都是基础阿
查看>>
NetWork——关于TCP协议的三次握手和四次挥手
查看>>
An easy problem
查看>>
MauiMETA工具的使用(一)
查看>>
LeetCode: Anagrams 解题报告
查看>>
用cookie登录慕课网络教学中心刷评论
查看>>
Qt 中获取本机IP地址
查看>>
基本数据类型(int, bool, str)
查看>>
070102_赌博设计:概率的基本概念,古典概型
查看>>
IT人生的价值和意义 感觉真的有了
查看>>
Linux命令之df
查看>>
JS DOM对象
查看>>
python正则表达式
查看>>
OGR – Merging Multiple SHP files
查看>>
创业公司该不该被收购?(转)
查看>>
sqlserver 行转列、列转行[转]
查看>>