2.MyBatis有代理增删改

时间:2021-10-29 22:11:22

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

 create user holly identified by sys;
grant dba to holly;
conn holly/sys
create table teacher(id number primary key,tname varchar2(20),pwd varchar2(20));
create sequence seq_teacher;
insert into teacher values(seq_teacher.nextval,'holly','');
insert into teacher values(seq_teacher.nextval,'石头','');
insert into teacher values(seq_teacher.nextval,'朱亚楠','');
insert into teacher values(seq_teacher.nextval,'陈明','');
commit;

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

 <?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>
<!-- 路径别名配置 -->
<typeAliases>
<typeAlias type="com.entity.Dept" alias="Dept"/>
</typeAliases> <!-- 链接数据库的环境配置,development开发模式 -->
<environments default="development">
<environment id="development">
<!-- jdbc管理事务 -->
<transactionManager type="jdbc"/>
<!-- 数据源配置 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</dataSource> </environment>
</environments>
<!-- 注册映射文件 -->
<mappers>
<mapper resource="com/mapper/DeptMapper.xml"/>
</mappers> </configuration>

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

 package com.entity;

 public class Teacher {
private int id;
private String tname;
private String pwd;
public Teacher() {
} public Teacher(String tname, String pwd) {
this.tname = tname;
this.pwd = pwd;
} public Teacher(int id, String tname, String pwd) {
this.id = id;
this.tname = tname;
this.pwd = pwd;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTname() {
return tname;
} public void setTname(String tname) {
this.tname = tname;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} @Override
public String toString() {
return "Teacher [id=" + id + ", pwd=" + pwd + ", tname=" + tname + "]";
}
}

Teacher.java

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

 package com.mapper;

 import java.util.List;

 import com.entity.Teacher;

 public interface TeacherMapper {
//1.查所有
List<Teacher> findAll();
//2.根据id查询
Teacher findById(int id);
//3.添加
int saveTeacher(Teacher teacher);
//4.修改
int updateTeacher(Teacher teacher);
//5.删除
int deleteTeacher(int id); }

TeacherMapper.java

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

 <?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" >
<!-- 如果是有代理,则namespace的值必须是映射接口的全路径 -->
<mapper namespace="com.mapper.TeacherMapper">
<!-- 1.查询所有 -->
<select id="findAll" resultType="Teacher">
select * from teacher
</select> <!-- 2.根据id查询所有 -->
<select id="findById" resultType="Teacher" parameterType="int">
select * from teacher where id=#{id}
</select> <!-- 3.添加 -->
<insert id="saveTeacher" parameterType="Teacher">
insert into teacher values(seq_teacher.nextval,#{tname},#{pwd})
</insert> <!-- 4.修改 -->
<update id="updateTeacher" parameterType="Teacher">
update teacher set tname=#{tname},pwd=#{pwd}
</update> <!-- 5.删除 -->
<delete id="deleteTeacher" parameterType="int">
delete from teacher where id=#{id}
</delete>
</mapper>

TeacherMapper.xml

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

 package com.mapper.impl;

 import java.util.List;

 import org.apache.ibatis.session.SqlSession;

 import com.entity.Teacher;
import com.mapper.TeacherMapper;
import com.util.MyBatisUtil; public class TeacherMapperImpl implements TeacherMapper {
/**
*1.查询所有
* */
public List<Teacher> findAll() {
SqlSession sqlsession=MyBatisUtil.getSqlSession(false);
//利用反射产生代理对象(动态代理)
TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);
try {
List<Teacher> list=mapper.findAll();
sqlsession.close();
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
} }
/**
* 2.查询单条
*/
public Teacher findById(int id) {
SqlSession sqlsession=MyBatisUtil.getSqlSession(false);
TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);
Teacher teacher=null;
try {
teacher = mapper.findById(id);
sqlsession.close();
return teacher;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 添加
*/
public int saveTeacher(Teacher teacher) {
SqlSession sqlsession=MyBatisUtil.getSqlSession(true);
TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);
try {
int num=mapper.saveTeacher(teacher);
sqlsession.close();
return num;
} catch (Exception e) {
e.printStackTrace(); return 0;
}
}
/**
* 4.修改
*/
public int updateTeacher(Teacher teacher) {
SqlSession sqlsession=MyBatisUtil.getSqlSession(false);
TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);
try {
int num=mapper.updateTeacher(teacher);
sqlsession.close();
return num;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 5.根据id删除
*/
public int deleteTeacher(int id) {
SqlSession sqlsession=MyBatisUtil.getSqlSession(false);
TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);
try {
int num=mapper.deleteTeacher(id);
sqlsession.close();
return num;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
} }

TeacherMapperImpl.java