中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用mybatis怎么將表與對象進行關聯

發布時間:2020-12-03 16:46:06 來源:億速云 閱讀:290 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關利用mybatis怎么將表與對象進行關聯,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

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

項目管理工具 : Maven

測試運行工具 : Junit

數據庫 : Derby

Maven Dependencies:

<dependencies> 
    <dependency> 
      <groupId>org.mybatis</groupId> 
      <artifactId>mybatis</artifactId> 
      <version>3.2.7</version> 
    </dependency> 
    <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.9</version> 
      <scope>test</scope> 
    </dependency> 
    <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derby</artifactId> 
      <version>10.10.2.0</version> 
    </dependency> 
    <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derbyclient</artifactId> 
      <version>10.10.2.0</version> 
    </dependency> 
  </dependencies> 

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

<&#63;xml version="1.0" encoding="UTF-8" &#63;>  
<!DOCTYPE configuration  
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
  <properties> 
    <property name="driver" value="org.apache.derby.jdbc.ClientDriver" /> 
    <property name="url"       value="jdbc:derby://localhost:1527/bjpowernode;create=true" /> 
  </properties> 
  <environments default="development"> 
    <environment id="development"> 
      <transactionManager type="JDBC" /> 
      <dataSource type="POOLED"> 
        <property name="driver" value="${driver}" /> 
        <property name="url" value="${url}" /> 
      </dataSource> 
    </environment> 
  </environments> 
  <mappers> 
    <mapper resource="com/bjpowernode/practice/UserMapper.xml" />
  </mappers> 
</configuration>  

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目錄下)

<&#63;xml version="1.0" encoding="UTF-8" &#63;>  
<!DOCTYPE mapper  
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.bjpowernode.practice.UserMapper">  
  <!-- 對象屬性和表字段的對應 --> 
  <resultMap id="userResultMap" type="com.bjpowernode.practice.User"> 
    <result property="id" column="USER_ID"/> 
    <result property="username" column="USER_USERNAME"/> 
    <result property="password" column="USER_PASSWORD"/> 
    <result property="nickname" column="USER_NICKNAME"/> 
  </resultMap>    
  <select id="getUser" resultMap="userResultMap"> 
    select * 
    from USER_TEST_TB_RELATIONSHIP 
  </select> 
</mapper> 

UserMapper.java Mapper類(src/main/java/com.bjpowernode.practice目錄下)

package com.bjpowernode.practice;  
import java.util.List;  
public interface UserMapper 
{ 
  public List<User> 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<User> 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怎么將表與對象進行關聯,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

略阳县| 鸡西市| 松原市| 太原市| 兖州市| 游戏| 康乐县| 蚌埠市| 常州市| 军事| 武冈市| 威宁| 吴江市| 绥江县| 南平市| 江油市| 定兴县| 磐安县| 新丰县| 九寨沟县| 泾川县| 交口县| 宁波市| 平果县| 丹棱县| 西平县| 闽侯县| 霍林郭勒市| 凉山| 石屏县| 花垣县| 池州市| 永泰县| 舒城县| 灵丘县| 运城市| 合川市| 富阳市| 达孜县| 襄汾县| 咸丰县|