mybatis实现表与对象的关联关系_动力节点Java学院整理
所需要用到的其他工具或技术:
项目管理工具:Maven
测试运行工具:Junit
数据库 :Derby
MavenDependencies:
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不支持表结构中列名的修改,只能这么做了):
CREATETABLEUSER_TEST_TB_RELATIONSHIP( USER_IDINTPRIMARYKEY, USER_USERNAMEVARCHAR(20)NOTNULL, USER_PASSWORDVARCHAR(20)NOTNULL, USER_NICKNAMEVARCHAR(20)NOTNULL ); INSERTINTOUSER_TEST_TB_RELATIONSHIP(USER_ID,USER_USERNAME,USER_PASSWORD,USER_NICKNAME)SELECTID,USERNAME,PASSWORD,NICKNAMEFROMUSER_TEST_TB;
Mybatis配置文件src/main/resource源目录下
test-mybatis-configuration.xml
User.java对象类(src/main/java/com/bjpowernode/practice目录下)
packagecom.bjpowernode.practice; publicclassUser { privateIntegerid; privateStringusername; privateStringpassword; privateStringnickname; publicIntegergetId() { returnid; } publicvoidsetId(Integerid) { this.id=id; } publicStringgetUsername() { returnusername; } publicvoidsetUsername(Stringusername) { this.username=username; } publicStringgetPassword() { returnpassword; } publicvoidsetPassword(Stringpassword) { this.password=password; } publicStringgetNickname() { returnnickname; } publicvoidsetNickname(Stringnickname) { this.nickname=nickname; } }
UserMapper.xml Mapper文件(src/main/java/com.bjpowernode.practice目录下)
select* fromUSER_TEST_TB_RELATIONSHIP
UserMapper.javaMapper类(src/main/java/com.bjpowernode.practice目录下)
packagecom.bjpowernode.practice; importjava.util.List; publicinterfaceUserMapper { publicListgetUser(); }
测试类TestMyBatis.Java(src/test/java/com.bjpowernode.practice目录下)
packagecom.bjpowernode.practice; importjava.io.InputStream; importjava.text.MessageFormat; importjava.util.List; importorg.apache.ibatis.session.SqlSession; importorg.apache.ibatis.session.SqlSessionFactory; importorg.apache.ibatis.session.SqlSessionFactoryBuilder; importorg.junit.Before; importorg.junit.Test; publicclassTestMyBatis { privateStringsource; privateInputStreaminputStream; privateSqlSessionFactorysqlSessionFactory; @Before publicvoidsetUp() { /** *准备Mybatis运行环境 */ source="test-mybatis-configuration.xml"; inputStream=TestMyBatis.class.getClassLoader().getResourceAsStream(source); sqlSessionFactory=newSqlSessionFactoryBuilder().build(inputStream); } @Test publicvoidtestGet() { try { //获取Session连接 SqlSessionsession=sqlSessionFactory.openSession(); //获取Mapper UserMapperuserMapper=session.getMapper(UserMapper.class); //显示User信息 System.out.println("TestGetstart..."); this.printUsers(userMapper.getUser()); System.out.println("TestGetfinished..."); } catch(Exceptione) { e.printStackTrace(); } } /** * *打印用户信息到控制台 * *@paramusers */ privatevoidprintUsers(finalListusers) { intcount=0; for(Useruser:users) { System.out.println(MessageFormat.format("==User[{0}]=================",++count)); System.out.println("UserId:"+user.getId()); System.out.println("UserUserName:"+user.getUsername()); System.out.println("UserPassword:"+user.getPassword()); System.out.println("Usernickname:"+user.getNickname()); } } }
总结
以上所述是小编给大家介绍的mybatis实现表与对象的关联关系_动力节点Java学院整理,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!