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 34 5 6 8 9 107 11 24 2512 13 2314 15 16 21 2217 18 19 20 26 28 29 30 3127
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 }
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 ListfindAll();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 }
2.5在src下的com.mapper包下创建TeacherMapper.xml映射配置文件
1 2 3 45 6 9 10 11 14 15 16 17 insert into teacher values(seq_teacher.nextval,#{tname},#{pwd})18 19 20 2122 update teacher set tname=#{tname},pwd=#{pwd}23 24 25 2627 delete from teacher where id=#{id}28 29
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 ListfindAll() {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 }