<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 使用 Stripes 的 Java webapp,MyBatis,& Derby 原文:http://zetcode.com/java/stripesmybatisderby/ 在本教程中,我們使用 Stripes,MyBatis 和 Derby 創建一個 Java Web 應用。 我們使用 NetBeans 來構建應用。 Apache Tomcat 用作 JSP 和 servlet 容器。 可從作者的 Github [倉庫](https://github.com/janbodnar/Stripes-MyBatis-Derby)中獲得項目源。 Stripes 是一個開源的輕量級 Java Web 應用框架。 Stripes 的目標是使 Java 中基于 Servlet/JSP 的 Web 開發變得簡單,直觀和直接。 Stripes 是基于動作的 MVC(模型視圖控制器)框架。 它運行在 JEE Web 容器中,使用最少的配置文件,并具有靈活和簡單的參數綁定。 MyBatis 是 Java 持久性框架,使用 XML 描述符或注釋將對象與存儲過程或 SQL 語句耦合。 與 ORM 框架不同,MyBatis 不會將 Java 對象映射到數據庫表,而是將 Java 方法映射到 SQL 語句。 MyBatis 允許使用所有數據庫功能,例如存儲過程,視圖,任何復雜性和供應商專有功能的查詢。 Derby 是用 Java 編寫的關系數據庫管理系統。 Oracle 以 Java DB 的名義分發相同的二進制文件。 Derby 的占用空間很小,約為 2MB。 Derby 使用的數據庫格式是可移植的且與平臺無關。 ## 圖書應用 我們在 NetBeans 中創建一個新的 Web 應用。 在應用中,我們將能夠將新書添加到數據庫中,通過它們的 ID 選擇單個書,然后選擇表中的所有書。 該項目需要 Stripes,MyBatis 和 JSTL 庫。 前三個 JAR 是 MyBatis 庫,后三個是 Stripes 的庫。 我們必須從他們的項目頁面中刪除他們。 JSTL JAR 隨 NebBeans 一起提供。 ![Server and Settings](https://img.kancloud.cn/8d/40/8d40813c0ced3d3b22d3fe1305aa3e2b_299x198.jpg) 圖:項目庫 在“NetBeans 服務”選項卡中,我們展開“數據庫”節點,然后右鍵單擊 Java DB 節點,然后選擇“創建數據庫”選項。 數據庫名稱將為`books`,用戶名和密碼為`app`和`app`。 `books.sql` ```java CREATE TABLE Books(Id INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), Author VARCHAR(30), Title VARCHAR(60), Published INTEGER, Remark VARCHAR(150)); INSERT INTO Books(Author, Title, Published, Remark) VALUES ('Leo Tolstoy', 'War and Peace', 1869, 'Napoleonic wars'); INSERT INTO Books(Author, Title, Published, Remark) VALUES ('Leo Tolstoy', 'Anna Karenina', 1878, 'Greatest book of love'); INSERT INTO Books(Author, Title, Published, Remark) VALUES ('Jeff Prosise', 'Programming Windows with MFC', 1999, 'Classic book about MFC'); INSERT INTO Books(Author, Title, Published, Remark) VALUES ('Tom Marrs', 'JBoss at Work', 2005, 'JBoss practical guide'); INSERT INTO Books(Author, Title, Published, Remark) VALUES ('Debu Panda', 'EJB3 in Action', 2007, 'Introduction to Enterprice Java Beans'); ``` 我們創建一個與創建的數據庫的新數據庫連接,并執行此 SQL 代碼。 我們有一個`Books`表,其中包含幾本書。 `web.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <display-name>Stripes Filter</display-name> <filter-name>StripesFilter</filter-name> <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class> <init-param> <param-name>ActionResolver.Packages</param-name> <param-value>com.zetcode.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>StripesFilter</filter-name> <url-pattern>*.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter-mapping> <filter-name>StripesFilter</filter-name> <servlet-name>StripesDispatcher</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping> <servlet> <servlet-name>StripesDispatcher</servlet-name> <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StripesDispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ``` 在`web.xml`部署描述符中,我們設置了 Stripes 框架。 `index.jsp`文件是默認的主頁文件。 在`com.zetcode.action`中,有`ActionBeans`。 ### `resources` 在`resources`目錄中,我們有三個文件。 通過右鍵單擊項目文件并選擇“屬性”來創建`resources`目錄。 在源類別中,我們添加一個新的源包文件夾。 ![Resources](https://img.kancloud.cn/28/a8/28a80338c9a6f934698f9021f6070215_296x89.jpg) 圖:資源 `BookMapper.xml`和`mybatis-config.xml`是 MyBatis 使用的 XML 文件。 `StripesResources.properties`是 Stripes 框架的默認資源束文件。 它包含應用的錯誤消息和標簽。 它位于 Stripes 下載文件的`lib`子目錄中。 ### 表示層 表示層由六個 JSP 頁面組成。 `index.jsp`是應用的默認主頁。 `addBook.jsp`是用于向數據庫添加新書的頁面,`findBook.jsp`是用于通過其 ID 查找書的頁面。 將書籍成功插入數據庫后,`bookAdded.jsp`顯示一條消息,`showOneBook.jsp`顯示選定的書籍,`showAllBooks.jsp`顯示數據庫中的所有書籍。 `index.jsp` ```java <%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome page</title> </head> <body> <stripes:link href="addBook.jsp"> Add a new book </stripes:link> <stripes:link href="findBook.jsp"> Find one book </stripes:link> <stripes:link beanclass="com.zetcode.action.SelectAllBooksActionBean"> Show all books </stripes:link> </body> </html> ``` `index.jsp`包含指向兩個 JSP 頁面的 Stripes 鏈接,以添加一本新書并查找一本書,以及一個指向`ActionBean`的鏈接以顯示所有書。 `<%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%>`聲明 Stripes 使用的標簽,包括`<stripes:link>`。 `findBook.jsp` ```java <%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Find a book</title> </head> <body> <stripes:form beanclass="com.zetcode.action.SelectOneBookActionBean"> <stripes:errors/> Book ID: <stripes:text name="bookId"/><br> <stripes:submit name="save" value="Submit"/> </stripes:form> </body> </html> ``` 在`findBook.jsp`中,我們有一個表格,可通過其 ID 查找圖書。 該表單包含一個文本字段和一個“提交”按鈕。 插入值已驗證; 如果用戶添加了無效值,則應用將返回錯誤消息。 `addBook.jsp` ```java <%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Add new book</title> </head> <body> <stripes:form beanclass="com.zetcode.action.AddBookActionBean"> <stripes:errors/> Author: <stripes:text name="author"/><br> Title: <stripes:text name="title"/><br> Year of publishing: <stripes:text name="published"/><br> Remark <stripes:text name="remark"/><br> <stripes:submit name="save" value="Submit"/> </stripes:form> </body> </html> ``` `addBook.jsp`將一本新書添加到數據庫中。 它包含一個帶有四個文本字段的表單,用于書籍對象。 該 ID 由 Derby 數據庫自動生成。 `bookAdded.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Book added</title> </head> <body> <h3>Book added to database</h3> </body> </html> ``` 將新書添加到數據庫后,應用返回`bookAdded.jsp`,其中包含一條簡單消息。 `showAllBooks.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Show all books</title> </head> <body> <h3>All books</h3> <table> <thead> <tr> <th>Id</th> <th>Author</th> <th>Title</th> <th>Published</th> <th>Remark</th> </tr> </thead> <c:forEach items="${actionBean.books}" var='book'> <tr> <td> <c:out value="${book.id}"/> </td> <td> <c:out value="${book.author}"/> </td> <td> <c:out value="${book.title}"/> </td> <td> <c:out value="${book.published}"/> </td> <td> <c:out value="${book.remark}"/> </td> </tr> </c:forEach> </table> </body> </html> ``` `showAllBooks.jsp`顯示從數據庫檢索到的所有書籍。 `<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>`指令聲明了 JSTL 核心標記,包括`<c:forEach>`和`<c:out>`。 可以使用`${actionBean}`表達式訪問返回的數據,其中`actionBean`是轉發視圖(即此 JSP 頁面)的操作 bean。 在這種情況下,操作 bean 是`SelectAllBooksActionBean`。 `showOneBook.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Show one book</title> </head> <body> <h3>A book</h3> <table> <thead> <tr> <th>Id</th> <th>Author</th> <th>Title</th> <th>Published</th> <th>Remark</th> </tr> </thead> <tr> <td> <c:out value="${actionBean.book.id}"/> </td> <td> <c:out value="${actionBean.book.author}"/> </td> <td> <c:out value="${actionBean.book.title}"/> </td> <td> <c:out value="${actionBean.book.published}"/> </td> <td> <c:out value="${actionBean.book.remark}"/> </td> </tr> </table> </body> </html> ``` `showOneBook.jsp`顯示一本書,我們通過其 ID 找到了它。 ### `Book` bean `Book` bean 是一個 Java 類,代表我們應用的域對象-一本書。 `Book.java` ```java package com.zetcode.bean; public class Book { private Long id; private String author; private String title; private int yearPublished; private String remark; public Book() {}; public Book(String author, String title, int published, String remark) { this.author = author; this.title = title; this.yearPublished = published; this.remark = remark; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPublished() { return yearPublished; } public void setPublished(int published) { this.yearPublished = published; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } } ``` Bean 具有必要的屬性以及獲取器和設置器方法。 請注意,還必須添加一個空的構造器。 ### `ActionBean` Stripes 的`ActionBean`是一個對象,該對象接收在請求中提交的數據并處理用戶的輸入。 它既定義了表單的屬性,又定義了表單的處理邏輯。 最后,它向用戶返回一個視圖。 在我們的應用中,我們有三個動作 bean:`AddBookActionBean`,`SelectAllBooksActionBean`和`SelectOneBookActionBean`。 它們每個代表在應用中要執行的某些操作。 ![Action beans](https://img.kancloud.cn/af/51/af51531b8d7ceebaadbc04bd3bb1a335_273x76.jpg) 圖:動作 Bean 我們將動作 bean 放入`com.zetcode.action`包中。 `AddBookActionBean.java` ```java package com.zetcode.action; import com.zetcode.bean.Book; import com.zetcode.service.BookService; import net.sourceforge.stripes.action.ActionBean; import net.sourceforge.stripes.action.ActionBeanContext; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.Resolution; import net.sourceforge.stripes.validation.Validate; public class AddBookActionBean implements ActionBean { private static final String VIEW = "/bookAdded.jsp"; private ActionBeanContext context; @Validate(required = true) private String author; @Validate(required = true) private String title; @Validate(required = true) private int yearPublished; @Validate(required = true) private String remark; public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getYearPublished() { return yearPublished; } public void setYearPublished(int yearPublished) { this.yearPublished = yearPublished; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @DefaultHandler public Resolution addBook() { Book book = new Book(this.author, this.title, this.yearPublished, this.remark); BookService.saveBook(book); return new ForwardResolution(VIEW); } @Override public void setContext(ActionBeanContext context) { this.context = context; } @Override public ActionBeanContext getContext() { return context; } } ``` 在我們填寫表格以添加新書后,將調用`AddBookActionBean`。 動作 Bean 自動將請求屬性綁定到其自己的屬性。 ```java private static final String VIEW = "/bookAdded.jsp"; ``` 書籍成功保存到數據庫后,`AddBookActionBean`返回`bookAdded.jsp`頁面。 ```java @Validate(required=true) private String author; @Validate(required=true) private String title; ... ``` 使用`@Validate`注解,我們為 HTML 字段提供了驗證服務。 這些字段不能為空,并且必須與正確的數據類型匹配。 ![Validation](https://img.kancloud.cn/b1/6c/b16c419651227209ca476cf906104b51_577x336.jpg) 圖:驗證 如果發布年份具有非整數字符,則“提交”操作將失敗。 ```java @DefaultHandler public Resolution addBook() { Book book = new Book(this.author, this.title, this.yearPublished, this.remark); BookService.saveBook(book); return new ForwardResolution(VIEW); } ``` `@DefaultHandler`注解指定此操作 bean 的默認處理器方法。 (可以定義多個處理器。)處理器創建`Book` bean,調用`BookService.saveBook()`并轉發到適當的視圖。 `SelectOneBookActionBean.java` ```java package com.zetcode.action; import com.zetcode.bean.Book; import com.zetcode.service.BookService; import java.io.IOException; import net.sourceforge.stripes.action.ActionBean; import net.sourceforge.stripes.action.ActionBeanContext; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.Resolution; import net.sourceforge.stripes.validation.Validate; public class SelectOneBookActionBean implements ActionBean { private static final String VIEW = "/showOneBook.jsp"; private ActionBeanContext context; private Book book; @Validate(required=true) private Long bookId; public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public Long getBookId() { return bookId; } public void setBookId(Long bookId) { this.bookId = bookId; } @DefaultHandler public Resolution showOneBook() throws IOException { this.book = BookService.getBook(bookId); return new ForwardResolution(VIEW); } @Override public void setContext(ActionBeanContext context) { this.context = context; } @Override public ActionBeanContext getContext() { return context; } } ``` 單擊`findBook.jsp`中的“提交”按鈕后,將調用`SelectOneBookActionBean`。 ```java @DefaultHandler public Resolution showOneBook() throws IOException { this.book = BookService.getBook(bookId); return new ForwardResolution(VIEW); } ``` 默認處理器調用`BookService.getBook()`方法,然后轉發到視圖。 `SelectAllBooksActionBean.java` ```java package com.zetcode.action; import com.zetcode.bean.Book; import com.zetcode.service.BookService; import java.util.List; import net.sourceforge.stripes.action.ActionBean; import net.sourceforge.stripes.action.ActionBeanContext; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.Resolution; public class SelectAllBooksActionBean implements ActionBean { private static final String VIEW = "/showAllBooks.jsp"; private ActionBeanContext context; private List<Book> books; public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } @DefaultHandler public Resolution showAll() { this.books = BookService.getAllBooks(); return new ForwardResolution(VIEW); } @Override public void setContext(ActionBeanContext context) { this.context = context; } @Override public ActionBeanContext getContext() { return context; } } ``` `SelectAllBooksActionBean`負責從數據庫中選擇所有書籍。 ```java @DefaultHandler public Resolution showAll() { this.books = BookService.getAllBooks(); return new ForwardResolution(VIEW); } ``` 調用`BookService.getAllBooks()`完成該工作。 ### 服務 `BookService`包含與數據庫通信的方法。 `BookService.java` ```java package com.zetcode.service; import com.zetcode.bean.Book; import com.zetcode.persistence.MyBatisDAO; import java.util.List; public class BookService { public static void saveBook(Book book) { MyBatisDAO mbd = new MyBatisDAO(); mbd.saveBook(book); } public static List<Book> getAllBooks() { MyBatisDAO mbd = new MyBatisDAO(); List<Book> books = mbd.findAll(); return books; } public static Book getBook(Long id) { MyBatisDAO mbd = new MyBatisDAO(); Book book = mbd.findBook(id); return book; } } ``` DAO 模式用于使示例更易于移植。 ```java public static List<Book> getAllBooks() { MyBatisDAO mbd = new MyBatisDAO(); List<Book> books = mbd.findAll(); return books; } ``` `getAllBooks()`方法創建`MyBatisDAO`并調用其`findAll()`方法。 它返回檢索到的書的列表。 ### DAO 數據訪問對象(DAO) 模式用于將底層數據訪問 API 或操作與高層業務服務分開。 `BookDAO.java` ```java package com.zetcode.persistence; import com.zetcode.bean.Book; import java.util.List; public interface BookDAO { public void saveBook(Book book); public Book findBook(Long id); public List<Book> findAll(); } ``` 訪問數據的方法在`BookDAO`接口中定義。 當我們根據該接口進行編程時,代碼的耦合較少。 `MyBatisDAO.java` ```java package com.zetcode.persistence; import com.zetcode.bean.Book; import com.zetcode.util.ServiceLocator; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; public class MyBatisDAO implements BookDAO { @Override public void saveBook(Book book) { SqlSession session = null; try { SqlSessionFactory factory = ServiceLocator.getSessionFactory(); session = factory.openSession(); session.insert("insertBook", book); session.commit(); } finally { if (session != null) { session.close(); } } } @Override public Book findBook(Long id) { SqlSession session = null; Book book = null; try { SqlSessionFactory factory = ServiceLocator.getSessionFactory(); session = factory.openSession(); book = session.selectOne("selectBook", id); } finally { if (session != null) { session.close(); } } return book; } @Override public List<Book> findAll() { SqlSession session = null; List<Book> retrieveList = null; try { SqlSessionFactory factory = ServiceLocator.getSessionFactory(); session = factory.openSession(); retrieveList = session.selectList("selectAllBooks"); } finally { if (session != null) { session.close(); } } return retrieveList; } } ``` `MyBatisDAO`是`BookDAO`接口的具體實現。 如果基礎數據源發生更改,我們可以輕松地切換到新的 DAO 實現。 ```java @Override public void saveBook(Book book) { SqlSession session = null; try { SqlSessionFactory factory = ServiceLocator.getSessionFactory(); session = factory.openSession(); session.insert("insertBook", book); session.commit(); } finally { if (session != null) { session.close(); } } } ``` `saveBook()`方法將一本新書保存到數據庫中。 創建一個`SqlSessionFactory`來產生一個`SqlSession`,這是使用 MyBatis 的主要 Java 接口。 工廠的創建委托給`ServiceLocator`。 會話的`insert()`方法使用給定的參數對象執行插入語句。 `commit()`方法將更改提交到數據庫。 ```java SqlSessionFactory factory = ServiceLocator.getSessionFactory(); session = factory.openSession(); book = session.selectOne("selectBook", id); ``` 為了選擇一本書,我們將`id`參數傳遞給會話的`selectOne()`方法。 ```java SqlSessionFactory factory = ServiceLocator.getSessionFactory(); session = factory.openSession(); retrieveList = session.selectList("selectAllBooks"); ``` `selectList()`方法返回書籍對象列表。 `ServiceLocator.java` ```java package com.zetcode.util; import java.io.IOException; import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class ServiceLocator { public static SqlSessionFactory getSessionFactory() { InputStream inputStream = null; SqlSessionFactory sqlSessionFactory = null; try { String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException ex) { Logger.getLogger(ServiceLocator.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException ex) { Logger.getLogger(ServiceLocator.class.getName()).log(Level.WARNING, null, ex); } } return sqlSessionFactory; } } ``` `ServiceLocator`從提供的配置文件中構建`SqlSessionFactory`。 該工廠隨后用于生產`SqlSession`,這是與 MyBatis 進行通信的主要接口。 `mybatis-config.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="Book" type="com.zetcode.bean.Book"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="org.apache.derby.jdbc.ClientDriver"/> <property name="url" value="jdbc:derby://localhost:1527/books"/> <property name="username" value="app"/> <property name="password" value="app"/> </dataSource> </environment> </environments> <mappers> <mapper resource="BookMapper.xml"/> </mappers> </configuration> ``` 在`mybatis-config.xml`文件中,我們創建一本新書`Book`類型,定義 Derby 的數據源,并指定映射器文件。 `BookMapper.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zetcode"> <select id="selectAllBooks" resultType="Book"> SELECT * FROM Books </select> <select id="selectBook" parameterType="long" resultType="Book"> SELECT * FROM Books WHERE Id = #{id} </select> <insert id="insertBook" parameterType="Book" statementType="PREPARED"> INSERT INTO Books(Author, Title, Published, Remark) VALUES (#{author}, #{title}, #{published}, #{remark}) </insert> </mapper> ``` 在`BookMapper.xml`中,我們有應用中使用的 SQL 命令。 它具有兩個選擇和一個插入命令。 ```java <select id="selectAllBooks" resultType="Book"> SELECT * FROM Books </select> ``` 注意,對于結果類型,我們不定義集合,而是定義集合項的類型。 ![Displaying all books](https://img.kancloud.cn/43/f3/43f303065c77fbe8528016ca8d962f37_693x336.jpg) 圖:顯示所有書籍 屏幕截圖顯示了從示例數據庫中選擇的所有書籍。 在本教程中,我們使用 Stripes,MyBatis 和 Derby 創建了一個 Web 應用。 我們使用了三層 DAO 軟件模式。 NetBeans 用于構建應用。 ZetCode 具有以下相關教程: [Derby 教程](/db/apachederbytutorial/), [Java 教程](/lang/java/), [Stripes 教程](/java/stripes/)和 [EJB 簡介](/java/ejb/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看