mybatis 双表增删改查(控制台版)



一、mybatis 所需相关jar 文件:
     

二、项目包结构


     

<!--   Mybatis     -->
< ? xml   version = "1.0"   encoding = "UTF-8"  ? >      
<! DOCTYPE   configuration      
     PUBLIC   "-//mybatis.org//DTD Config 3.0//EN"      
< configuration >
     <!--     便 mapper   使   -->
     < typeAliases >
          < typeAlias   type = "mybatis.entity.Student"   alias = "stu"  / >
          < typeAlias   type = "mybatis.entity.Grade"   alias = "grade"  / >
     < / typeAliases >
     < environments   default = "development" >
          < environment   id = "development" >
               < transactionManager   type = "JDBC"  / >
               < dataSource   type = "POOLED" >
                    <!--   Oracle   -->
                    < property   name = "driver"   value = "oracle.jdbc.driver.OracleDriver"  / >
                    <!--   Oracle URL   -->
                    < property   name = "url"   value = "jdbc:oracle:thin:@localhost:1521:qxgora"  / >
                    <!--     -->
                    < property   name = "username"   value = "xiaoqiu"  / >
                    <!--     -->
                    < property   name = "password"   value = "xiaoqiu"  / >
               < / dataSource >
          < / environment >
     < / environments >
     < mappers >
          <!--   Mapper . xml     mapper   -->
          < mapper   resource = "mybatis/entity/StudentMapper.xml"  / >
     < / mappers >
< / configuration >
//Mybatis session 
package   mybatis . util ;
import   java . io . IOException ;
import   org . apache . ibatis . io . Resources ;
import   org . apache . ibatis . session . SqlSession ;
import   org . apache . ibatis . session . SqlSessionFactory ;
import   org . apache . ibatis . session . SqlSessionFactoryBuilder ;
public   final   class   MybatisUtil  {
     //2.Session (sqlSession)
     //3.Mapper.xml
     //4.
     //session
     private   static   SqlSessionFactory   factory   =   null ;
     //session
     private   static   SqlSession   session   =   null ;
     // mybatis 
     private   static   String   resouces   =   "mybatis-config.xml" ;
     /**
     * session
     *  @return
     *  @throws  IOException
     */
     public   static   SqlSession   getSqlSession ()  throws   IOException  {
               factory   =   new   SqlSessionFactoryBuilder (). build ( Resources . getResourceAsReader ( resouces ));
               if  ( null   ==   session ) {
                    session   =   factory . openSession ();
              }
               return   session ;
    }
     /**
     * session
     */
     public   static    void   closeSession (){
          session . close ();
    }
}
// student
package   mybatis . entity ;
import   java . util . Date ;
public   class   Student  {
     private   int   stuId
     private   String   stuName
     private   int   stuAge
     private   Date   stuBirthDay
     private   String   stuPwd ;
     private   int   stuGid ;
     //()
     private   Grade   grade ;
     public   int   getStuGid () {
          return   stuGid ;
    }
     public   void   setStuGid ( int   stuGid ) {
          this . stuGid   =   stuGid ;
    }
     public   int   getStuId () {
          return   stuId ;
    }
     public   void   setStuId ( int   stuId ) {
          this . stuId   =   stuId ;
    }
     public   String   getStuName () {
          return   stuName ;
    }
     public   void   setStuName ( String   stuName ) {
          this . stuName   =   stuName ;
    }
     public   int   getStuAge () {
          return   stuAge ;
    }
     public   void   setStuAge ( int   stuAge ) {
          this . stuAge   =   stuAge ;
    }
     public   Date   getStuBirthDay () {
          return   stuBirthDay ;
    }
     public   void   setStuBirthDay ( Date   stuBirthDay ) {
          this . stuBirthDay   =   stuBirthDay ;
    }
     public   String   getStuPwd () {
          return   stuPwd ;
    }
     public   void   setStuPwd ( String   stuPwd ) {
          this . stuPwd   =   stuPwd ;
    }
     public   Grade   getGrade () {
          return   grade ;
    }
     public   void   setGrade ( Grade   grade ) {
          this . grade   =   grade ;
    }
    @ Override
     public   String   toString () {
          return   "Student [="   +   stuId   +   ", ="   +   stuName   +   ", ="
                    +   stuAge   +   ", ="   +   stuBirthDay   +   ", ="
                    +   stuPwd   +   ", ="   +   grade . getgName ()  +   "]" ;
    }
    
    
}
// grade
package   mybatis . entity ;
public   class   Grade  {
     private   int   gId
     private   String   gName ;
     public   int   getgId () {
          return   gId ;
    }
     public   void   setgId ( int   gId ) {
          this . gId   =   gId ;
    }
     public   String   getgName () {
          return   gName ;
    }
     public   void   setgName ( String   gName ) {
          this . gName   =   gName ;
    }
    
}
// DAO 
package   mybatis . dao ;
import   java . util . List ;
import   mybatis . entity . Student ;
public   interface   IStudentDao  {
     /**
     * 
     *  @param  stuName 
     *  @return  
     */
     public   abstract   List < Student >   selectStudentByLike ( String   stuName );
     /**
     * 
     *  @return
     */
     public   abstract   List < Student >   selectStudents ();
     /**
     * 
     *  @param  stu 
     *  @return  
     */
     public   abstract   int   insertStudent ( Student   stu );
     /**
     * 
     *  @param  stu 
     *  @return  
     */
     public   abstract   int   updateStudent ( Student   stu );
     /**
     * 
     *  @param  stuId (Id)
     *  @return  
     */
     public   abstract   int   deleteStudent ( int   stuId );
}
< ? xml   version = "1.0"   encoding = "UTF-8"  ? >
<! DOCTYPE   mapper
   PUBLIC   "-//mybatis.org//DTD Mapper 3.0//EN"
< mapper   namespace = "mybatis.dao.IStudentDao" >
     <!--   Map     id   -->
     < resultMap   type = "mybatis.entity.Student"   id = "stuMap" >
          <!--     property   column     -->
          < result   property = "stuId"   column = "STUID"  / >
          < result   property = "stuName"   column = "STUNAME"  / >
          < result   property = "stuAge"   column = "STUAGE"  / >
          < result   property = "stuBirthDay"   column = "STUBIRTHDAY"  / >
          < result   property = "stuPwd"   column = "STUPWD"  / >
          <!--     -->
          < association   property = "grade"   javaType = "mybatis.entity.Grade"
               column = "STUGID" >
               < result   property = "gId"   column = "GID"  / >
               < result   property = "gName"   column = "GNAME"  / >
          < / association >
     < / resultMap >
     <!--   ,   -->
     < sql   id = "stus" >
          stuId , stuName , stuAge , stuBirthDay , stuPwd , stuGid
     < / sql >
     < select   id = "selectStudentByLike"   resultMap = "stuMap"   parameterType = "java.lang.String" >
          select   *
          from
          student   s
          INNER   JOIN   GRADE   g   ON   g . gId =   s . stugId
          WHERE   stuName   LIKE  #{ stuName }
     < / select >
     <!--     resultType     List < Student >  
               
          resultMap   Map   -->
     < select   id = "selectStudents"   resultType = "stu"   resultMap = "stuMap" >
          select   *
          from
          student   s
          INNER   JOIN   GRADE   g   ON   g . gId =   s . stugId
     < / select >
     <!--     parameterType       insert     -->
     < insert   id = "insertStudent"   parameterType = "stu" >
          <!--     便   -->
          < selectKey   resultType = "int"   order = "BEFORE"   keyProperty = "stuId" >
               <! [ CDATA [
                    select   seq_student . nextval   from   dual  
              ]] >
          < / selectKey >
          <! [ CDATA [
               insert   into   student
               values (#{ stuId },#{ stuName },#{ stuAge },#{ stuBirthDay },#{ stuPwd },#{ stuGid })
         ]] >
     < / insert >
     <!--     -->
     < update   id = "updateStudent"   parameterType = "stu" >
          <! [ CDATA [
               update   student
               set  
                    stuName   =  #{ stuName },
                    stuAge   =  #{ stuAge },
                    stuBirthDay   =  #{ stuBirthDay },
                    stuPwd   =  #{ stuPwd },
                    stuGid   =  #{ stuGid }
               where   stuId   =  #{ stuId }
         ]] >
     < / update >
     <!--   ID (  )    -->
     < delete   id = "deleteStudent"   parameterType = "int" >
          <! [ CDATA [
               delete
                    from   student
               where
                    stuId   =  #{ stuId }
         ]] >
     < / delete >
< / mapper >
// 
package   mybatis . dao . impl ;
import   java . io . IOException ;
import   java . text . ParseException ;
import   java . text . SimpleDateFormat ;
import   java . util . List ;
import   org . apache . ibatis . session . SqlSession ;
import   mybatis . dao . IStudentDao ;
import   mybatis . entity . Grade ;
import   mybatis . entity . Student ;
import   mybatis . util . MybatisUtil ;
@ SuppressWarnings ( "unused" )
public   class   StudentDaoImpl   implements   IStudentDao  {
     /**
     * 
     */
    @ Override
     public   List < Student >   selectStudents () {
          try  {
               SqlSession   session   =   MybatisUtil . getSqlSession ();
               return   session . selectList ( "mybatis.dao.IStudentDao.selectStudents" );
               //return session.getMapper(IStudentDao.class).selectStudents();
         }  catch  ( IOException   e ) {
               e . printStackTrace ();
         }  finally  {
         }
          return   null ;
    }
     /**
     * 
     */
    @ Override
     public   int   insertStudent ( Student   stu ) {
          int   res   =   -1 ;
          SqlSession   session   =   null ;
          try  {
               session   =   MybatisUtil . getSqlSession ();
               res   =   session . getMapper ( IStudentDao . class ). insertStudent ( stu );
               session . commit ();
         }  catch  ( Exception   e ) {
               session . rollback ();
         }  finally  {
               MybatisUtil . closeSession ();
         }
          return   res ;
    }
     /**
     * 
     */
    @ Override
     public   int   updateStudent ( Student   stu ) {
          int   res   =   -1 ;
          SqlSession   session   =   null ;
          try  {
               session   =   MybatisUtil . getSqlSession ();
               res   =   session . getMapper ( IStudentDao . class ). updateStudent ( stu );
               session . commit ();
         }  catch  ( Exception   e ) {
               session . rollback ();
         }  finally  {
               MybatisUtil . closeSession ();
         }
          return   res ;
    }
     /**
     * 
     *  @param  stuId
     *  @return
     */
    @ Override
     public   int   deleteStudent ( int   stuId ) {
          int   res   =   -1 ;
          SqlSession   session   =   null ;
          try  {
               session   =   MybatisUtil . getSqlSession ();
               res   =   session . getMapper ( IStudentDao . class ). deleteStudent ( stuId );
               session . commit ();
         }  catch  ( Exception   e ) {
         }
          return   res ;
    }
    @ Override
     public   List < Student >   selectStudentByLike ( String   stuName ) {
          try  {
               SqlSession   session   =   MybatisUtil . getSqlSession ();
               return   session . selectList ( "mybatis.dao.IStudentDao.selectStudentByLike" , stuName + "%" );
//          return session.getMapper(IStudentDao.class).selectStudentByLike("%"+stuName+"%");
         }  catch  ( IOException   e ) {
               e . printStackTrace ();
         }  finally  {
         }
          return   null ;
    }
}
//
package   mybatis . test ;
import   java . text . ParseException ;
import   java . text . SimpleDateFormat ;
import   mybatis . dao . IStudentDao ;
import   mybatis . dao . impl . StudentDaoImpl ;
import   mybatis . entity . Student ;
public   class   MyBatisTest  {
     //DAO 
     private   static   IStudentDao   stuDao   =   new   StudentDaoImpl ();
     public   static   IStudentDao   getStuDao () {
          return   stuDao ;
    }
     public   static   void   setStuDao ( IStudentDao   stuDao ) {
          MyBatisTest . stuDao   =   stuDao ;
    }
     /**
     * 
     *  @param  args
     *  @throws  ParseException
     */
     public   static   void   main ( String []  args throws   ParseException  {
          selectStuByLike ();
          // 
          //selectStus();
          // 
          // insertStu();
          // 
          // updateStu();
          // 
          //deleteStu();
         
    }
     //
     private   static   void   selectStuByLike () {
          String   stuName   = "" ;
          for  ( Student   stu  :  stuDao . selectStudentByLike ( stuName )) {
               System . out . println ( stu );
         }
    }
     //
     private   static   void   selectStus () {
          for  ( Student   s  :  stuDao . selectStudents ()) {
               System . out . println ( s );
         }
    }
     //
     private   static   void   deleteStu () {
          int   stuId   =   0 ;
          int   res   =   stuDao . deleteStudent ( stuId );
          if  ( res   >   0 ) {
               System . out . println ( "OK" );
         }  else  {
               System . out . println ( "NO" );
         }
    }
     //
     private   static   void   updateStu ()  throws   ParseException  {
          Student   stu   =   new   Student ();
          stu . setStuName ( "" );
          stu . setStuAge ( 25 );
          stu . setStuPwd ( "456789" );
          stu . setStuBirthDay ( new   SimpleDateFormat ( "yyyy-MM-dd" ). parse ( "2010-09-09" ));
          stu . setStuGid ( 3 );
          stu . setStuId ( 24 );
          int   res   =   new   StudentDaoImpl (). updateStudent ( stu );
          if  ( res   >   0 ) {
               System . out . println ( "OK" );
         }  else  {
               System . out . println ( "NO" );
         }
    }
     //
     private   static   void   insertStu ()  throws   ParseException  {
          Student   stu   =   new   Student ();
          stu . setStuName ( "hhh" );
          stu . setStuAge ( 20 );
          stu . setStuPwd ( "12345" );
          stu . setStuBirthDay ( new   SimpleDateFormat ( "yyyy-MM-dd" ). parse ( "2012-09-09" ));
          stu . setStuGid ( 3 );
          int   res   =   new   StudentDaoImpl (). insertStudent ( stu );
          if  ( res   >   0 ) {
               System . out . println ( "OK" );
         }  else  {
               System . out . println ( "NO" );
         }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值