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" );
         }
    }
}



目录
相关文章
|
6月前
|
Java 数据库 Android开发
不同主题增删改查系统【纯控制台】(Java课设)
不同主题增删改查系统【纯控制台】(Java课设)
32 1
|
6月前
|
存储 Java 数据库连接
MyBatis-Plus 基础操作指南:实现高效的增删改查
MyBatis-Plus 基础操作指南:实现高效的增删改查
576 0
|
6月前
|
SQL Java 数据库连接
初识MyBatis(搭建MyBatis、简单增删改查、核心配置文件讲解及获取参数值)
初识MyBatis(搭建MyBatis、简单增删改查、核心配置文件讲解及获取参数值)
110 0
|
6月前
|
SQL Java 数据库连接
用springboot mybatis写一个增删改查
用springboot mybatis写一个增删改查
62 0
|
6月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
|
5月前
|
SQL 存储 Java
基于MyBatis的增删改查操作
基于MyBatis的增删改查操作
41 1
|
2月前
|
SQL XML Java
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
文章介绍了MyBatis的简单增删改查操作,包括创建数据表、实体类、配置文件、Mapper接口及其XML文件,并解释了`#{}`预编译参数和`@Param`注解的使用。同时,还涵盖了resultType与resultMap的区别,并提供了完整的代码实例和测试用例。
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
|
5月前
|
SQL 存储 Java
基于MyBatis的增删改查的基础操作
基于MyBatis的增删改查的基础操作
35 0
|
2月前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
44 1
|
4月前
|
Java 数据库连接 数据库
mybatis plus 中增删改查及Wrapper的使用
mybatis plus 中增删改查及Wrapper的使用
190 3