這篇文章將為大家詳細(xì)講解有關(guān)利用mybatis怎么對(duì)數(shù)據(jù)庫進(jìn)行增刪改查操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
十多年的順城網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整順城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“順城網(wǎng)站設(shè)計(jì)”,“順城網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
所需要用到的其他工具或技術(shù):
項(xiàng)目管理工具 : Maven
測(cè)試運(yùn)行工具 : Junit
數(shù)據(jù)庫 : Derby
Maven Dependencies:
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 建表及數(shù)據(jù)插入(沿用前兩節(jié)中的數(shù)據(jù)庫表及數(shù)據(jù)):
CREATE TABLE USER_TEST_TB( ID INT PRIMARY KEY, USERNAME VARCHAR(20) NOT NULL, PASSWORD VARCHAR(20) NOT NULL, NICKNAME VARCHAR(20) NOT NULL ); INSERT INTO USER_TEST_TB VALUES(1,'1st','111','Jack'); INSERT INTO USER_TEST_TB VALUES(2,'2nd','222','Rose'); INSERT INTO USER_TEST_TB VALUES(3,'3rd','333','Will');
Mybatis配置文件 src/main/resource源目錄下
test-mybatis-configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
User.java對(duì)象類(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" ?>insert into USER_TEST_TB values(#{id},#{username},#{password},#{nickname}) update USER_TEST_TB set USERNAME = #{username}, PASSWORD = #{password}, NICKNAME = #{nickname} where ID = #{id} delete from USER_TEST_TB WHERE ID=#{id}
UserMapper.java Mapper類(src/main/java/com.bjpowernode.practice目錄下)
package com.bjpowernode.practice; import java.util.List; public interface UserMapper { public ListgetUser(); public void insertUser(User user); public void updateUser(User user); public void deleteUser(int userId); }
測(cè)試類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() { /** * 準(zhǔn)備Mybatis運(yùn)行環(huán)境 */ 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(); } } @Test public void testInsert() { try { // 獲取Session連接 SqlSession session = sqlSessionFactory.openSession(); // 獲取Mapper UserMapper userMapper = session.getMapper(UserMapper.class); System.out.println("Test insert start..."); // 顯示插入之前User信息 System.out.println("Before insert"); this.printUsers(userMapper.getUser()); // 執(zhí)行插入 userMapper.insertUser(this.mockUser("FREU_INS_USER", "BJPOWERNODE_INS_PASS", "BJPOWERNODE_INS_NICK")); // 提交事務(wù) session.commit(); // 顯示插入之后User信息 System.out.println("\r\nAfter insert"); this.printUsers(userMapper.getUser()); System.out.println("Test insert finished..."); } catch (Exception e) { e.printStackTrace(); } } @Test public void testUpdate() { try { // 獲取Session連接 SqlSession session = sqlSessionFactory.openSession(); // 獲取Mapper UserMapper userMapper = session.getMapper(UserMapper.class); System.out.println("Test update start..."); // 顯示更新之前User信息 System.out.println("Before update"); this.printUsers(userMapper.getUser()); // 執(zhí)行更新 userMapper.updateUser(this.mockUser("FREU_UPD_USER", "BJPOWERNODE_UPD_PASS", "BJPOWERNODE_UPD_NICK")); // 提交事務(wù) session.commit(); // 顯示更新之后User信息 System.out.println("\r\nAfter update"); this.printUsers(userMapper.getUser()); System.out.println("Test update finished..."); } catch (Exception e) { e.printStackTrace(); } } @Test public void testDelete() { try { // 獲取Session連接 SqlSession session = sqlSessionFactory.openSession(); // 獲取Mapper UserMapper userMapper = session.getMapper(UserMapper.class); System.out.println("Test delete start..."); // 顯示刪除之前User信息 System.out.println("Before delete"); this.printUsers(userMapper.getUser()); // 執(zhí)行刪除 userMapper.deleteUser(this.mockUser(null, null, null).getId()); // 提交事務(wù) session.commit(); // 顯示刪除之后User信息 System.out.println("\r\nAfter delete"); this.printUsers(userMapper.getUser()); System.out.println("Test delete finished..."); } catch (Exception e) { e.printStackTrace(); } } /** * * 組裝一個(gè)User對(duì)象 * * @return */ public User mockUser(String username, String password, String nickname) { User user = new User(); user.setId(10); user.setUsername(username); user.setPassword(password); user.setNickname(nickname); return user; } /** * * 打印用戶信息到控制臺(tái) * * @param users */ private void printUsers(final Listusers) { 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()); } } }
關(guān)于利用mybatis怎么對(duì)數(shù)據(jù)庫進(jìn)行增刪改查操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。