我是myBatis和JSP的新手。我正处于学习阶段,想要知道一件简单的事情。我在mySQL中有一本表书,可以使用myBatis对它进行基本操作。但我希望数据显示在JSP页面中。
以下是代码:
Book.java
package ibatis;
public class Book {
private int id;
private String author;
private String title;
private String year;
private String remark;
//Getters and Setters
}
是book.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>
Main.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页面的任何步骤都将受到赞赏。
答案 0 :(得分:0)
您在jsp页面与ibatis / mybatis之间建立链接的框架是什么?
可能(非详尽)选项包括:
希望有所帮助