mybatis入门_动力节点Java学院整理
本文为大家分享了mybatis入门学习资料,供大家参考,具体内容如下
所需要用到的其他工具或技术:
项目管理工具: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建表及数据插入:
CREATETABLEUSER_TEST_TB( IDINTPRIMARYKEY, USERNAMEVARCHAR(20)NOTNULL, PASSWORDVARCHAR(20)NOTNULL, NICKNAMEVARCHAR(20)NOTNULL ); INSERTINTOUSER_TEST_TBVALUES(1,'1st','111','Jack'); INSERTINTOUSER_TEST_TBVALUES(2,'2nd','222','Rose'); INSERTINTOUSER_TEST_TBVALUES(3,'3rd','333','Will');
Mybatis配置文件src/main/resource源目录下
test-mybatis-configuration.xml
UserMapper.xml Mapper文件(src/main/java/com.freud.practice目录下)
select* fromUSER_TEST_TB
User.Java对象类(src/main/java/com.freud.practice目录下)
packagecom.freud.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.javaMapper类(src/main/java/com.freud.practice目录下)
packagecom.freud.practice; importjava.util.List; importorg.apache.ibatis.annotations.Select; publicinterfaceUserMapper { //@Select("SELECT*FROMUSER_TEST_TB") publicListgetUser(); }
测试类TestMyBatis.java(src/test/java/com.freud.practice目录下)
packagecom.freud.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() { source="test-mybatis-configuration.xml"; } @Test publicvoidtestXMLConfingureSessionFactory() { try { inputStream=TestMyBatis.class.getClassLoader().getResourceAsStream(source); sqlSessionFactory=newSqlSessionFactoryBuilder().build(inputStream); SqlSessionsession=sqlSessionFactory.openSession(); Listusers=(List)session.selectList("com.freud.practice.UserMapper.getUser"); System.out.println("QuerybyXMLconfiguration..."); this.printUsers(users); UserMapperuserMapper=session.getMapper(UserMapper.class); System.out.println("\r\nQuerybyannotationconfiguration..."); this.printUsers(userMapper.getUser()); } catch(Exceptione) { e.printStackTrace(); } } privatevoidprintUsers(finalList users) { 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()); } } }
以上是使用XML作为Mapper配置文件的一个入门,其实Mybatis还支持Annotation的方式,具体操作如下:
1.打开UserMapper.java中的注释。
2.打开test-mybatis-configuration.xml中的注释,并注释掉现有的Mapper
3.删除UserMapper.xml文件
这种是Mybatis的Annotation方式的Mapper配置。个人比较偏向于XML方式,所以后续的教程会更倾向于XML配置的方式来写。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。