mybatis實現表與物件的關聯關係_動力節點Java學院整理

NO IMAGE

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

專案管理工具 : 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


<?xml version="1.0" encoding="UTF-8" ?>  
<!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目錄下)


<?xml version="1.0" encoding="UTF-8" ?>  
<!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實現表與物件的關聯關係_動力節點Java學院整理,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對指令碼之家網站的支援!

您可能感興趣的文章:

Mybatis 一對多和多對一關聯查詢問題Mybatis多表關聯查詢的實現(DEMO)深入淺出MyBatis中對映檔案和實體類的關聯性例項講解Java的MyBatis框架對MySQL中資料的關聯查詢MyBatis學習教程(五)-實現關聯表查詢方法詳解MyBatis實踐之動態SQL及關聯查詢MyBatis學習筆記(二)之關聯關係