利用mybatis怎么将表与对象进行关联

本篇文章给大家分享的是有关利用mybatis怎么将表与对象进行关联,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都网站设计、咸丰网络推广、小程序定制开发、咸丰网络营销、咸丰企业策划、咸丰品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供咸丰建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

所需要用到的其他工具或技术:

项目管理工具 : Maven

测试运行工具 : Junit

数据库 : Derby

Maven Dependencies:

 
     
      org.mybatis 
      mybatis 
      3.2.7 
     
     
      junit 
      junit 
      4.9 
      test 
     
     
      org.apache.derby 
      derby 
      10.10.2.0 
     
     
      org.apache.derby 
      derbyclient 
      10.10.2.0 
     
   

SQL 建表及数据插入(新建一个USER_TEST_TB_RELATIONSHIP表,然后CopyUSER_TEST_TB的数据,Derby不支持表结构中列名的修改,只能这么做了):

CREATE TABLE USER_TEST_TB_RELATIONSHIP( 
USER_ID INT PRIMARY KEY,   
USER_USERNAME VARCHAR(20) NOT NULL,   
USER_PASSWORD VARCHAR(20) NOT NULL,   
USER_NICKNAME VARCHAR(20) NOT NULL   
); 
 INSERT INTO USER_TEST_TB_RELATIONSHIP(USER_ID,USER_USERNAME,USER_PASSWORD,USER_NICKNAME) SELECT ID,USERNAME,PASSWORD,NICKNAME FROM USER_TEST_TB; 

Mybatis配置文件 src/main/resource源目录下

test-mybatis-configuration.xml

<?xml version="1.0" encoding="UTF-8" ?>  
 
 
   
     
     
   
   
     
       
       
         
         
       
     
   
   
    
   
  

User.java对象类(src/main/java/com/bjpowernode/practice目录下)

package com.bjpowernode.practice; 
public class User 
{ 
  private Integer id; 
  private String username; 
  private String password; 
  private String nickname; 
  public Integer getId() 
  { 
    return id; 
  } 
  public void setId(Integer id) 
  { 
    this.id = id; 
  } 
  public String getUsername() 
  { 
    return username; 
  } 
  public void setUsername(String username) 
  { 
    this.username = username; 
  } 
  public String getPassword() 
  { 
    return password; 
  } 
  public void setPassword(String password) 
  { 
    this.password = password; 
  } 
  public String getNickname() 
  { 
    return nickname; 
  } 
  public void setNickname(String nickname) 
  { 
    this.nickname = nickname; 
  } 
} 

UserMapper.xml  Mapper文件(src/main/java/com.bjpowernode.practice目录下)

<?xml version="1.0" encoding="UTF-8" ?>  
 
  
   
   
     
     
     
     
      
   
 

UserMapper.java Mapper类(src/main/java/com.bjpowernode.practice目录下)

package com.bjpowernode.practice;  
import java.util.List;  
public interface UserMapper 
{ 
  public List getUser(); 
} 

测试类TestMyBatis.Java(src/test/java/com.bjpowernode.practice目录下)

package com.bjpowernode.practice; 
import java.io.InputStream; 
import java.text.MessageFormat; 
import java.util.List; 
import org.apache.ibatis.session.SqlSession; 
import org.apache.ibatis.session.SqlSessionFactory; 
import org.apache.ibatis.session.SqlSessionFactoryBuilder; 
import org.junit.Before; 
import org.junit.Test; 
public class TestMyBatis 
{ 
  private String source; 
  private InputStream inputStream; 
  private SqlSessionFactory sqlSessionFactory; 
  @Before 
  public void setUp() 
  { 
    /** 
     * 准备Mybatis运行环境 
     */ 
    source = "test-mybatis-configuration.xml"; 
    inputStream = TestMyBatis.class.getClassLoader().getResourceAsStream(source); 
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 
  } 
  @Test 
  public void testGet() 
  { 
    try 
    { 
      // 获取Session连接 
      SqlSession session = sqlSessionFactory.openSession(); 
      // 获取Mapper 
      UserMapper userMapper = session.getMapper(UserMapper.class); 
      // 显示User信息 
      System.out.println("Test Get start..."); 
      this.printUsers(userMapper.getUser()); 
      System.out.println("Test Get finished..."); 
    } 
    catch (Exception e) 
    { 
      e.printStackTrace(); 
    } 
  } 
  /** 
   * 
   * 打印用户信息到控制台 
   * 
   * @param users 
   */ 
  private void printUsers(final List users) 
  { 
    int count = 0; 
    for (User user : users) 
    { 
      System.out.println(MessageFormat.format("==User[{0}]=================", ++count)); 
      System.out.println("User Id: " + user.getId()); 
      System.out.println("User UserName: " + user.getUsername()); 
      System.out.println("User Password: " + user.getPassword()); 
      System.out.println("User nickname: " + user.getNickname()); 
    } 
  } 
}

以上就是利用mybatis怎么将表与对象进行关联,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网站名称:利用mybatis怎么将表与对象进行关联
转载来源:http://pwwzsj.com/article/ihecci.html