Mybatis连接MySQL或者ORALE 12C 入门实例。

Mybatis连接MySQL或者ORALE 12C 入门实例。

[step1] 建立四个java包及包里面源文件. 
com.learn.ch2.utils;
com.learn.ch2.main;
com.learn.ch2.pojo;
com.learn.ch2.mapper;
此外,在Eclipse中添加Mybatis相关jar包;数据库JDBC相关jar包。
======================
[step2] APP入口 
src/com/learn/ch2/main/Ch2Main.java
package com.learn.ch2.main;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import com.learn.ch2.mapper.RoleMapper;
import com.learn.ch2.pojo.Role;
import com.learn.ch2.utils.SqlSessionFactoryUtil;


public class Ch2Main {
public static void main(String[] args) throws IOException{
SqlSession mySqlSession = null;
        try{
                 mySqlSession = SqlSessionFactoryUtil.openSqlSession();
                 RoleMapper roleMapper = mySqlSession.getMapper(RoleMapper.class);
                 for(int i = 0; i<10000; i++){
                             Role role = new Role();
                             role.setId( (long)i );
                             role.setRoleName(" testroname " + i);
                             role.setNote(" testnote " + i);
                             roleMapper.insertRole(role);
                             mySqlSession.commit();
                             System.out.println("insertRole: i = " + i + ", commit");
            }
            /*
              Role role = roleMapper.getRole(1L);
              System.out.println("get_Role: id = " + role.getId() + ", name = " + role.getRoleName() + ", note = " + role.getNote());
            */ 
            /*
           Map map = new java.util.HashMap();
           map.put("role_name_key", "%");
           map.put("note_key", "%");
           List roleList = roleMapper.getRoleAll(map);  
           for(int i = 0; i < roleList.size(); i++){
           Role role = roleList.get(i);
           if(role != null){
           System.out.println("get_Role: i = " + i + ", id = " + role.getId() + ", name = " + role.getRoleName() + ", note = " + role.getNote());
           }
           }
           */
           /*
           Role role = new Role();
           role.setId( 20000L );
           role.setRoleName( "Update role name" );
           role.setNote( "update note" );
           roleMapper.updateRole(role);
           mySqlSession.commit();
           */
            /*
                      roleMapper.deleteRole( 20000L );
                         mySqlSession.commit();
                           System.out.println("deleteRole finish.");
           */

        } catch( Exception e){
                 e.printStackTrace();
                 if(null != mySqlSession){
                   mySqlSession.rollback();
                 }
        } finally {
             if(null != mySqlSession){
             mySqlSession.close();
             }
        }
   }
}


======================
[step3] src/com/learn/ch2/mapper/RoleMapper.java
package com.learn.ch2.mapper;
import java.util.List;
import java.util.Map;
import com.learn.ch2.pojo.Role;


public interface RoleMapper {
/*select*/
public Role getRole(Long id);
public List getRoleAll(Map map);
/*delete*/
public int deleteRole(Long id);
/*insert*/
public int insertRole(Role role);
/*update*/
public int updateRole(Role role);
}

======================
[step4] src/com/learn/ch2/mapper/mybatis配置文件(测试mysql)。



   
       
   

   
       
           
           
               
               
               
               
           

       

   

   
       
   




======================
[step5] src/com/learn/ch2/mapper/mybatis配置文件(测试oracle 12C PDB)。



 
       
 

 
   
     
     
       
       
       
       
     

   

 

 
     
 




======================
[step6] src/com/learn/ch2/mapper/mapper 文件。



   
       
       
       
   

   
   
   
        insert into T_ROLE(id, role_name, note) values (#{id}, #{roleName}, #{note})
   

   
        delete from T_ROLE
   
   
   
        update T_ROLE set
        role_name = #{roleName},
        note = #{note}
        where id = #{id}
   




======================
[step7] src/com/learn/ch2/pojo/Role.java
package com.learn.ch2.pojo;


public class Role {
private Long id;
private String roleName;
private String note;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}


======================
[step8]src/com/learn/ch2/utils/SqlSessionFactoryUtil.java
package com.learn.ch2.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
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 class SqlSessionFactoryUtil {
private static SqlSessionFactory  mySqlSessionFactory = null;
private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class;
private SqlSessionFactoryUtil() {}
public static  SqlSessionFactory   initSqlSessionFactory() {
         String config = "com/learn/ch2/mapper/mybatis-config-oracle.xml";
          //String config = "com/learn/ch2/mapper/mybatis-config-mysql.xml";
          InputStream inputstream = null;
         try{
                 inputstream = Resources.getResourceAsStream(config);
           }catch(IOException e){
                  Logger.getLogger(SqlSessionFactoryUtil.class.getName()).log(Level.SEVERE, null, e);
           }
            synchronized(CLASS_LOCK){
             if(mySqlSessionFactory == null){
                            mySqlSessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
              }
           }
           return mySqlSessionFactory;
        }


      public static  SqlSession openSqlSession() {
              if(null == mySqlSessionFactory ) {
                     initSqlSessionFactory();
               }
               return mySqlSessionFactory.openSession();
         }
}


---完毕---

你可能感兴趣的:(Mybatis连接MySQL或者ORALE 12C 入门实例。)