如何利用MyBatis創(chuàng)建一個存儲過程?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于成都做網站、成都網站建設、云州網絡推廣、微信平臺小程序開發(fā)、云州網絡營銷、云州企業(yè)策劃、云州品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供云州建站搭建服務,24小時服務熱線:028-86922220,官方網址:www.cdcxhl.com
一. 在Derby中創(chuàng)建存儲過程
在Eclipse中創(chuàng)建一個新的普通Java項目命名為Test_Store_Procedure
在com.bjpowernode.practice包下創(chuàng)建一個Class命名為StoreProcedureOperationClass.class
package com.bjpowernode.practice; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; /** * * 存儲過程類 * * @author Freud * */ public class StoreProcedureOperationClass { /** * * 執(zhí)行插入的存儲過程 * * @param id * @param username * @param password * @param nickname * @throws SQLException */ public static void insertData(int id, String username, String password, String nickname) throws SQLException { Connection connection = DriverManager.getConnection("jdbc:default:connection"); PreparedStatement p = connection.prepareStatement("INSERT INTO USER_TEST_TB(ID,USERNAME,PASSWORD,NICKNAME) VALUES(?,?,?,?)"); p.setInt(1, id); p.setString(2, username); p.setString(3, password); p.setString(4, nickname); System.out.println("INSERT VALUES (id=" + id + ",username=" + username + ",password=" + password + ",nickname=" + nickname + ")"); p.executeUpdate(); p.close(); connection.close(); } }
3. 利用jar命令或者Eclipse工具導出到C:\freud\Test_Store_Procedure.jar
4. 在ij命令行中聲明存儲過程
CREATE PROCEDURE FREUD.INSERT_USER(IN THE_ID INTEGER, IN THE_USERNAME VARCHAR(20), IN THE_PASSWORD VARCHAR(20), IN THE_NICKNAME VARCHAR(20)) PARAMETER STYLE JAVA MODIFIES SQL DATA LANGUAGE JAVA EXTERNAL NAME 'com.bjpowernode.practice.StoreProcedureOperationClass.insertData'; 在 ij 控制臺中調用call sqlj.install_jar('C:\freud\Test_Store_Procedure.jar', 'FREUD.TEST_SPROCEDURE', 0);將 jar 包導入到 FREUD模式中,并命名為TEST_SPROCEDURE。
6. 在 ij 控制臺中調用call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.database.classpath', 'FREUD.TEST_SPROCEDURE');
將 jar 包加入到數據庫 classpath 搜索路徑中。
這樣,Derby的存儲過程就算創(chuàng)建完成了
二.在Mybatis中調用存儲過程
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
Mybatis配置文件 src/main/resource源目錄下
test-mybatis-configuration.xml <?xml version="1.0" encoding="UTF-8" ?>
User.java對象類(src/main/java/com/freud/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" ?>CALL FREUD.INSERT_USER(#{id},#{username},#{password},#{nickname})
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); }
測試類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運行環(huán)境 */ source = "test-mybatis-configuration.xml"; inputStream = TestMyBatis.class.getClassLoader().getResourceAsStream(source); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @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", "FREUD_INS_PASS", "FREUD_INS_NICK")); // 提交事務 session.commit(); // 顯示插入之后User信息 System.out.println("\r\nAfter insert"); this.printUsers(userMapper.getUser()); System.out.println("Test insert finished..."); } catch (Exception e) { e.printStackTrace(); } } /** * * 組裝一個User對象 * * @return */ public User mockUser(String username, String password, String nickname) { User user = new User(); user.setId(50); user.setUsername(username); user.setPassword(password); user.setNickname(nickname); return user; } /** * * 打印用戶信息到控制臺 * * @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()); } } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。