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建表及数据插入(沿用前两节中的数据库表及数据):
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
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 insertinto USER_TEST_TB values(#{id},#{username},#{password},#{nickname}) updateUSER_TEST_TBset USERNAME=#{username}, PASSWORD=#{password}, NICKNAME=#{nickname} whereID=#{id} deletefromUSER_TEST_TBWHEREID=#{id}
UserMapper.javaMapper类(src/main/java/com.bjpowernode.practice目录下)
packagecom.bjpowernode.practice; importjava.util.List; publicinterfaceUserMapper { publicListgetUser(); publicvoidinsertUser(Useruser); publicvoidupdateUser(Useruser); publicvoiddeleteUser(intuserId); }
测试类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(); } } @Test publicvoidtestInsert() { try { //获取Session连接 SqlSessionsession=sqlSessionFactory.openSession(); //获取Mapper UserMapperuserMapper=session.getMapper(UserMapper.class); System.out.println("Testinsertstart..."); //显示插入之前User信息 System.out.println("Beforeinsert"); this.printUsers(userMapper.getUser()); //执行插入 userMapper.insertUser(this.mockUser("FREU_INS_USER","BJPOWERNODE_INS_PASS","BJPOWERNODE_INS_NICK")); //提交事务 session.commit(); //显示插入之后User信息 System.out.println("\r\nAfterinsert"); this.printUsers(userMapper.getUser()); System.out.println("Testinsertfinished..."); } catch(Exceptione) { e.printStackTrace(); } } @Test publicvoidtestUpdate() { try { //获取Session连接 SqlSessionsession=sqlSessionFactory.openSession(); //获取Mapper UserMapperuserMapper=session.getMapper(UserMapper.class); System.out.println("Testupdatestart..."); //显示更新之前User信息 System.out.println("Beforeupdate"); this.printUsers(userMapper.getUser()); //执行更新 userMapper.updateUser(this.mockUser("FREU_UPD_USER","BJPOWERNODE_UPD_PASS","BJPOWERNODE_UPD_NICK")); //提交事务 session.commit(); //显示更新之后User信息 System.out.println("\r\nAfterupdate"); this.printUsers(userMapper.getUser()); System.out.println("Testupdatefinished..."); } catch(Exceptione) { e.printStackTrace(); } } @Test publicvoidtestDelete() { try { //获取Session连接 SqlSessionsession=sqlSessionFactory.openSession(); //获取Mapper UserMapperuserMapper=session.getMapper(UserMapper.class); System.out.println("Testdeletestart..."); //显示删除之前User信息 System.out.println("Beforedelete"); this.printUsers(userMapper.getUser()); //执行删除 userMapper.deleteUser(this.mockUser(null,null,null).getId()); //提交事务 session.commit(); //显示删除之后User信息 System.out.println("\r\nAfterdelete"); this.printUsers(userMapper.getUser()); System.out.println("Testdeletefinished..."); } catch(Exceptione) { e.printStackTrace(); } } /** * *组装一个User对象 * *@return */ publicUsermockUser(Stringusername,Stringpassword,Stringnickname) { Useruser=newUser(); user.setId(10); user.setUsername(username); user.setPassword(password); user.setNickname(nickname); returnuser; } /** * *打印用户信息到控制台 * *@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学院整理,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!