如何使用sqlsesson的getmapper获取接口的实例

原文地址:https://zhidao.baidu.com/question/922756385577154179.html?fr=iks&word=session.getMapper&ie=gbk
1,本文采用的Mybatis框架版本为: mybatis-3.0.4.jar
2, 以持久化一个简单的User bean为例
User实体类的代码如下:
Java代码  收藏代码
    package bean;  
       public class User {  
           private String name;  
           private Integer age;  
           public String getName() {  
              return name;  
           }  
           public void setName(String name) {  
              this.name = name;  
           }  
           public Integer getAge() {  
              return age;  
           }  
           public void setAge(Integer age) {  
              this.age = age;  
           }  
           public User(String name, Integer age) {  
              super();  
              this.name = name;  
              this.age = age;  
           }  
           public User() {  
              super();  
           }  //必须要有这个无参构造方法,不然根据UserMapper.xml中的配置,在查询数据库时,将不能反射构造出User实例  
       }   
User实体的映射器取名为UserMapper(接口),其代码如下
Java代码  收藏代码
    package Mapper;  
     import bean.User;  
     public interface UserMapper {  
         public void insertUser(User user);  
         public User getUser(String name);  
     }   
注意,该接口类似于Hibernate中的DAO层接口,唯一不同的是,MyBatis中,只声明该接口就可以,不需要去实现.
UserMapper映射器对应的配置文件如下(相当于Hibernate中的User.hbm.xml,当然这里面不是属性映射字段,而是直接是映射器中各种方法对应的sql语句):
      
         
         
         
             
              insert into vincent_user(name,age) values(#{name},#{age})  
                
             
             
             
         
MyBatis的配置文件这里取名为mybatis-config.xml其配置代码如下(相当与Hibernate的hibernate.cfg.xml):
Xml代码  收藏代码
      
          
          
              
                  
                  
                  
                  
              
              
                 
              
              
                 
                     
                     
                        
                        
                        
                        
                     
                 
              
              
                  
              
           
加载配置文件得到sqlSessionFactory的工具类
Java代码  收藏代码
    package Util;  
        import java.io.IOException;  
        import java.io.Reader;  
        import org.apache.ibatis.io.Resources;  
        import org.apache.ibatis.session.SqlSessionFactory;  
        import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
        public class MyBatisUtil  {  
            private  final static SqlSessionFactory sqlSessionFactory;  
            static {  
               String resource = "resource/mybatis-config.xml";  
               Reader reader = null;  
               try {  
                   reader = Resources.getResourceAsReader(resource);  
               } catch (IOException e) {  
                   System.out.println(e.getMessage());  
               }  
               sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);  
            }  
            public static SqlSessionFactory getSqlSessionFactory() {  
               return sqlSessionFactory;  
            }  
        }  
测试代码如下:
Java代码  收藏代码
    package TestMapper;  
       import org.apache.ibatis.session.SqlSession;  
       import org.apache.ibatis.session.SqlSessionFactory;  
       import org.junit.Test;  
       public class TestMapper {  
           static SqlSessionFactory sqlSessionFactory = null;  
           static {  
              sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();  
           }  
           @Test  
           public void testAdd() {  
              SqlSession sqlSession = sqlSessionFactory.openSession();  
              try {  
                  UserMapper userMapper = sqlSession.getMapper(UserMapper.class);  
                  User user = new User("tom",new Integer(5));  
                  userMapper.insertUser(user);  
                  sqlSession.commit();//这里一定要提交,不然数据进不去数据库中  
              } finally {  
                  sqlSession.close();  
              }  
           }  
           @Test  
           public void getUser() {  
              SqlSession sqlSession = sqlSessionFactory.openSession();  
              try {  
                  UserMapper userMapper = sqlSession.getMapper(UserMapper.class);  
                  User user = userMapper.getUser("jun");  
                  System.out.println("name: "+user.getName()+"|age: "+user.getAge());  
              } finally {  
                  sqlSession.close();  
              }  
           }  
       }

你可能感兴趣的:(java)