• 2.MyBatis有代理增删改


    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 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
     3 <configuration>
     4    <!-- 路径别名配置 -->
     5    <typeAliases>
     6      <typeAlias type="com.entity.Dept" alias="Dept"/>
     7    </typeAliases>
     8    
     9    <!-- 链接数据库的环境配置,development开发模式 -->
    10    <environments default="development">
    11       <environment id="development">
    12         <!-- jdbc管理事务 -->
    13         <transactionManager type="jdbc"/>
    14         <!-- 数据源配置 -->
    15         <dataSource type="POOLED">
    16            <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
    17            <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
    18            <property name="username" value="scott"/>
    19            <property name="password" value="tiger"/>
    20         </dataSource>
    21         
    22       </environment>
    23    </environments>
    24    <!-- 注册映射文件 -->
    25    <mappers>
    26      <mapper resource="com/mapper/DeptMapper.xml"/>
    27    </mappers>
    28 
    29    
    30  
    31 </configuration>

    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     @Override
    46     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<Teacher> 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 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <!-- 如果是有代理,则namespace的值必须是映射接口的全路径 -->
     4 <mapper namespace="com.mapper.TeacherMapper">
     5   <!-- 1.查询所有 -->
     6   <select id="findAll" resultType="Teacher">
     7     select * from teacher
     8   </select>
     9   
    10   <!-- 2.根据id查询所有 -->
    11   <select id="findById" resultType="Teacher" parameterType="int">
    12     select * from teacher where id=#{id}
    13   </select>
    14   
    15   <!-- 3.添加 -->
    16   <insert id="saveTeacher" parameterType="Teacher">
    17     insert into teacher values(seq_teacher.nextval,#{tname},#{pwd})
    18   </insert>
    19   
    20   <!-- 4.修改 -->
    21   <update id="updateTeacher" parameterType="Teacher">
    22     update teacher set tname=#{tname},pwd=#{pwd}
    23   </update>
    24   
    25   <!-- 5.删除 -->
    26   <delete id="deleteTeacher" parameterType="int">
    27     delete from teacher where id=#{id}
    28   </delete>
    29 </mapper>
    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<Teacher> findAll() {
    16         SqlSession sqlsession=MyBatisUtil.getSqlSession(false);
    17         //利用反射产生代理对象(动态代理)
    18         TeacherMapper mapper=sqlsession.getMapper(TeacherMapper.class);
    19         try {
    20             List<Teacher> 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
  • 相关阅读:
    无向图的邻接表
    tensorflow安装问题:ImportError:DLL load failed找不到指定模块
    《梦断代码》阅读笔记(三)
    《梦断代码》阅读笔记(二)
    《梦断代码》阅读笔记(一)
    个人总结
    第十六周总结
    团队开发冲刺第二阶段(最终日总结)
    第十五周总结
    "百度输入法"评测
  • 原文地址:https://www.cnblogs.com/holly8/p/5791723.html
Copyright © 2020-2023  润新知