• Ibatis框架入门教程(对Student进行简单的增删改)


    在前面,我讲述了ibatis的三个配置文件和查询所有的学生,在下面的案例中,我会以增删改的形式完成Java层次的curd四个方法,主要的方法我会以中文的简短描述给出

        1.通过学生编号查询学生信息

        2.通过插入一个Student实例实现学生的增加

        3.通过学号实现Student的删除

        4.通过修改一个Student实例实现学生信息的更新

    注意:因为实例太多,代码无法展示,我会以图片的形式进行展示,在最后我会给出源码,图片的展示顺序1.Student.xml文件 / 2.dao /3.daoimpl/4.测试方法/5运行结果

          首先通过学号查找学生信息

        1.配置文件

        

         2.dao /3.daoimpl/

     

    测试代码

    源代码的展示

    1.Student.xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE sqlMap      
        PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
        "http://ibatis.apache.org/dtd/sql-map-2.dtd">
    
    <!-- 创建一个对象关系映射文件,命名空间为 -->
    <sqlMap namespace="Student">
            
            <!-- 1.该标签是给Student模型取一个别名
                 2.alias是别名的意思,可以随便取
                 3.type是Student类的真实路径
             -->
            <typeAlias alias="Student" type="com.itcast.Student"/>
            <!-- id的意思是代替整条SQL文 
                2默认的类型为Student类 -->
            <select id="selectAllStudent" resultClass="Student">
                        select * from student
            </select>
            
            <!-- 1.根据学生的编号查询学生的信息
                 2
             -->
             <select id="selectStudentBystunum" parameterClass="String"
                                                      resultClass="int">
                     select * 
                     from student
                     where stunum=#stunum#
             
             </select>
            
            <!--1.插入一条学生记录  -->
            <insert id="insertStudent" parameterClass="Student">
                 insert into  student
                 (stunum,stuname,password,age)
                 values
                   (#stunum#,#stuname#,#password#,#age#)
            </insert>
            
            <!-- 1.删除一条数据 -->
            <delete id="deleteStudentBystunum" parameterClass="string">
                delete 
                from student 
                where stunum=#stunum#
            </delete>
            
            <!-- 1.更新学生信息 -->
            <update id="updateStudentBystunum" parameterClass="Stduent">
                     update student
                         set 
                             stuname=#stuname#,
                             password=#password#,
                             age=#age#
                         where 
                             stunum=#stunum#
                             
            </update>
            
            
            
    
            
            
    </sqlMap>

    Dao层代码

     1 package com.itcast;
     2 
     3 import java.util.List;
     4 
     5 public interface StudentDao {
     6     /**
     7      * 查询所有学生
     8      * @return Lists
     9      * @throws Exception
    10      */
    11     public List<Student> selectAllStudent() throws Exception;
    12     /**
    13      * 
    14      * 根据ID查询该学生
    15      * @return Student
    16      * @throws Exception
    17      */
    18     public Student selectStudentBystunum(String stunum) 
    19                                             throws Exception;
    20     /**
    21      * 插入一个学生
    22      * @param stu
    23      * @return Student
    24      * @throws Exception
    25      */
    26     public Student insertStudent(Student stu) throws Exception;
    27     /**
    28      * 删除学生的记录
    29      * @param stunum
    30      * @return rows
    31      * @throws Exception
    32      */
    33     public int deletStudentBystunum(String stunum) throws Exception;
    34 
    35     /**
    36      * 修改学生信息
    37      * @param student
    38      * @return rows影响的行数
    39      * @throws Exception
    40      */
    41     public int updateStudentBystunum(Student student) throws Exception;
    42     
    43 }

    DaoImp代码

      1 package com.itcast;
      2 
      3 import java.io.IOException;
      4 import java.io.Reader;
      5 import java.util.ArrayList;
      6 import java.util.List;
      7 
      8 import com.ibatis.sqlmap.client.SqlMapClient;
      9 
     10 public class StudentDaoImpl implements StudentDao{
     11 
     12     //实例化一个SqlMapClient
     13     private static SqlMapClient sqlmapclient=null;
     14     private final static String file="com/itcast/SqlMapConfig.xml";
     15     static {
     16         try {
     17             //实例化一个reader类,用来读取配置文件的信息
     18             Reader reader=com.ibatis.common.resources.Resources.getResourceAsReader(file);
     19             //将读取到的配置信息赋给sqlmapclient实例
     20             sqlmapclient=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
     21         
     22              reader.close();
     23         } catch (IOException e) {
     24             // TODO Auto-generated catch block
     25             e.printStackTrace();
     26         }
     27     }
     28     /**
     29      * 查询所有学生信息
     30      */
     31     public List<Student> selectAllStudent() throws Exception {
     32         
     33         List<Student> list=new ArrayList<Student>();
     34         //1.查询到的信息是一个listֵ
     35         
     36         list=sqlmapclient.queryForList("selectAllStudent");
     37         
     38         return list;
     39     }
     40     
     41     /**
     42      * 根据学生的编号查询该学生
     43      */
     44     @Override
     45     public Student selectStudentBystunum(String stunum) 
     46                                         throws Exception {
     47         Student student=new Student();
     48         /*
     49          * 1.使用queryForObject方法查询学生信息,该方法返回一个对象
     50          * 2.selectStudentBystunum匹配Student.xml中的id值
     51          * 3.stunum匹配Student.xml 中 ID为selectStudentBystunum的传进的参数值
     52          */
     53         student=(Student) sqlmapclient.
     54                 queryForObject("selectStudentBystunum",stunum);
     55         return student;
     56     }
     57     
     58 
     59 
     60     /**
     61      * 1.插入一个学生
     62      * 2.参数为一个Student
     63      * 3.返回类型为一个Student
     64      */
     65     @Override
     66     public Student insertStudent(Student stu) throws Exception {
     67     
     68         sqlmapclient.insert("insertStudent",stu);
     69         //提交事务
     70         sqlmapclient.commitTransaction();
     71         return stu;
     72     }
     73 
     74     /**
     75      * 1.删除学生记录
     76      * 2.返回值表示影响的行数
     77      */
     78     @Override
     79     public int deletStudentBystunum(String stunum) throws Exception {
     80         //delete方法返回值是影响的行数,所以用整形类型来接收它
     81         int rows=sqlmapclient.delete("deleteStudentBystunum",stunum);
     82         //提交事务
     83         sqlmapclient.commitTransaction();
     84         return rows;
     85     
     86     }
     87 
     88     
     89     @Override
     90     public int updateStudentBystunum(Student student) throws Exception {
     91         //更新语句实际上是对一个Student类做了一次更新,所以传递的参数实际上是一个对象
     92         
     93         int rows=sqlmapclient.update("updateStudentBystunum", student);
     94         /*跟以上方法一样
     95          *1.除select方法之外,update,insert,delete方法都应该进行事务的提交 
     96          */
     97         sqlmapclient.commitTransaction();
     98         return rows;
     99     }
    100 
    101     public static void main(String[] args) throws Exception
    102     {
    103             StudentDao dao=new StudentDaoImpl();
    104             for(Student student: dao.selectAllStudent())
    105             {
    106                 System.out.println(student);
    107             }
    108     }
    109 
    110 }

    测试代码

    1.通过学生编号查询学生信息

     1 /**
     2  *1.该注释类型为文档注释,主要用来说明该程序的作者,版权,程序版本和程序功能
     3  *2.@author zheng
     4  *3.该程序主要用来测试StudentDaoImpl下的 方法
     5  */
     6 package com.JUnitTest;
     7 import static org.junit.Assert.*;
     8 
     9 import java.util.Scanner;
    10 
    11 import org.junit.Test;
    12 
    13 import com.itcast.Student;
    14 import com.itcast.StudentDao;
    15 import com.itcast.StudentDaoImpl;
    16 
    17 public class TestSelectStudentBystunum {
    18 
    19     @Test
    20     public void test() throws Exception {
    21         //为了简单,我没有使用Dao接口的形式,有兴趣的可以用Dao来试试
    22         //Dao示例如下 
    23         //StudentDao student=new StudentDaoImpl();
    24         
    25         StudentDaoImpl studentimpl=new StudentDaoImpl();
    26         
    27         Scanner scanner=new Scanner(System.in);
    28         Student student=new Student();
    29         System.out.println("请输入学生信息");    
    30         String stunum=scanner.next();
    31         
    32         student=studentimpl.selectStudentBystunum(stunum);
    33         
    34         
    35         if(!student.equals(null))
    36         {
    37             System.out.println(student);
    38         }
    39         
    40         
    41         
    42         
    43     }
    44 
    45 }

     2.通过插入一个Student实例实现学生的增加

     1 package com.JUnitTest;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Scanner;
     6 
     7 import org.junit.Test;
     8 
     9 import com.itcast.Student;
    10 import com.itcast.StudentDaoImpl;
    11 
    12 public class TestInsertStudent {
    13 
    14     @Test
    15     public void test() throws Exception {
    16         Student student=new Student();
    17         StudentDaoImpl studentdaoimpl=new StudentDaoImpl();
    18         Scanner scanner=new Scanner(System.in);
    19 //        设置相关的输入信息
    20         System.out.println("请输入学生编号");
    21         String stunum=scanner.next();
    22         System.out.println("请输入学生姓名");
    23         String stuname=scanner.next();
    24         System.out.println("请输入密码");
    25         String password=scanner.next();
    26         System.out.println("请输入学生年龄");
    27         int age=scanner.nextInt();
    28         student.setStunum(stunum);
    29         student.setStuname(stuname);
    30         student.setPassword(password);
    31         student.setAge(age);
    32         
    33         studentdaoimpl.insertStudent(student);
    34         
    35         
    36         
    37     }
    38 
    39 }
     1 package com.JUnitTest;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Scanner;
     6 
     7 import org.junit.Test;
     8 
     9 import com.itcast.Student;
    10 import com.itcast.StudentDaoImpl;
    11 
    12 public class TestInsertStudent {
    13 
    14     @Test
    15     public void test() throws Exception {
    16         Student student=new Student();
    17         StudentDaoImpl studentdaoimpl=new StudentDaoImpl();
    18         Scanner scanner=new Scanner(System.in);
    19 //        设置相关的输入信息
    20         System.out.println("请输入学生编号");
    21         String stunum=scanner.next();
    22         System.out.println("请输入学生姓名");
    23         String stuname=scanner.next();
    24         System.out.println("请输入密码");
    25         String password=scanner.next();
    26         System.out.println("请输入学生年龄");
    27         int age=scanner.nextInt();
    28         student.setStunum(stunum);
    29         student.setStuname(stuname);
    30         student.setPassword(password);
    31         student.setAge(age);
    32         
    33         studentdaoimpl.insertStudent(student);
    34         
    35         
    36         
    37     }
    38 
    39 }

    3.通过学号实现Student的删除

     1 package com.JUnitTest;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Scanner;
     6 
     7 import org.junit.Test;
     8 
     9 import com.itcast.StudentDaoImpl;
    10 
    11 public class TestDeleteStudent {
    12 
    13     @Test
    14     public void test() throws Exception {
    15       Scanner scanner=new Scanner(System.in);
    16       System.out.println("请输入要删除学生信息的学生编号");
    17       String stunum=scanner.next();
    18       StudentDaoImpl studentdaoimpl=new StudentDaoImpl();
    19      
    20      int rows= studentdaoimpl.deletStudentBystunum(stunum);
    21      
    22      if(rows==0)
    23      {
    24          System.out.println("删除失败");
    25      }
    26      else
    27      {
    28          System.out.println("删除成功");
    29      }
    30     
    31     }
    32 
    33 }

    4.通过修改一个Student实例实现学生信息的更新

     1 package com.JUnitTest;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Scanner;
     6 
     7 import javax.swing.plaf.synth.SynthSpinnerUI;
     8 
     9 import org.junit.Test;
    10 
    11 import com.itcast.Student;
    12 import com.itcast.StudentDaoImpl;
    13 
    14 public class TestUpdateStudent {
    15 
    16     @Test
    17     public void test() throws Exception {
    18         Student student=new Student();
    19         StudentDaoImpl studentdaoimpl=new StudentDaoImpl();
    20         Scanner scanner=new Scanner(System.in);
    21 //        设置相关的输入信息
    22         System.out.println("请输入更新的学生编号");
    23         String stunum=scanner.next();
    24         System.out.println("请输入更新的学生姓名");
    25         String stuname=scanner.next();
    26         System.out.println("请输入更新的密码");
    27         String password=scanner.next();
    28         System.out.println("请输入更新的学生年龄");
    29         int age=scanner.nextInt();
    30         student.setStunum(stunum);
    31         student.setStuname(stuname);
    32         student.setPassword(password);
    33         student.setAge(age);
    34         
    35         
    36         //调用更新方法
    37         int rows=studentdaoimpl.updateStudentBystunum(student);
    38         if(rows==0)
    39         {
    40             System.out.println("更新失败");
    41         }
    42         else
    43         {
    44             System.out.println("跟新成功");
    45             //打印更新的student的信息
    46             //打印信息实际上是对该学号又作了一次查询
    47             student=studentdaoimpl.selectStudentBystunum(stunum);
    48             System.out.println(student);
    49         }
    50         
    51         
    52     
    53         
    54     }
    55 
    56 }
  • 相关阅读:
    CF809D Hitchhiking in the Baltic States
    CF1188D Make Equal
    CF1137 Train Car Selection
    LOJ3215「PA 2019」Muzyka pop
    洛谷4455 [CQOI2018]社交网络 (有向图矩阵树定理)(学习笔记)
    洛谷3571 POI2014 SUP-Supercomputer (斜率优化)
    洛谷2805 [NOI2009]植物大战僵尸 (拓扑排序+最小割)
    洛谷2120 [ZJOI2007]仓库建设(斜率优化dp)
    洛谷2494 [SDOI2011]保密 (分数规划+最小割)
    洛谷3648 [APIO2014]序列分割(斜率优化+dp)
  • 原文地址:https://www.cnblogs.com/zhengzuozhanglina/p/6036990.html
Copyright © 2020-2023  润新知