我对 myBatis 和 JSP 还很陌生。我正处于学习阶段,想知道一件简单的事情。我在 mySQL 中有一个表书,并且能够使用 myBatis 对其执行基本操作。但我希望数据显示在 JSP 页面中。
这是代码:
图书.java
package ibatis;
public class Book {
private int id;
private String author;
private String title;
private String year;
private String remark;
//Getters and Setters
}
书本.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Book">
<typeAlias alias="Book" type="ibatis.Book"/>
<select id="selectAllBooks" resultClass="ibatis.Book">
select * from books
</select>
</sqlMap>
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost:3306/books"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value=""/>
</dataSource>
</transactionManager>
<sqlMap resource="ibatis/Book.xml"/>
</sqlMapConfig>
主.java
package ibatis;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import org.apache.ibatis.io.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class Main {
public static void main(String[] args)
throws IOException, SQLException {
Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
System.out.println("List of all Books");
@SuppressWarnings("unchecked")
List<Book> books = (List<Book>)sqlMap.queryForList("selectAllBooks");
for (Book a : books) {
System.out.println(a.getAuthor() + " : " + a.getTitle());
}
}
}
这是一个基本的 myBatis Java 程序。任何构建 jsp 页面的步骤将不胜感激。