欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis实现表与对象的关联关系_动力节点Java学院整理

程序员文章站 2024-02-27 16:37:51
所需要用到的其他工具或技术: 项目管理工具 : maven 测试运行工具 : junit 数据库 : derby maven dependen...

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

项目管理工具 : 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学院整理,希望对大家有所帮助