Я недавно изучал, как использовать MyBatis + Struts2. Я создаю таблицу в MS SQL с именем Players. Есть 4 столбца, ID, PlayerNo, Team, PlayerName. PlayerName - единственный столбец, который я не могу успешно отобразить на странице.
Надеюсь, кто-нибудь может помочь прочитать мой код и дать несколько советов, спасибо. Изображение: введите описание изображения здесь
Player.java
public class Player {
private int id;
private int playerNo;
private String team;
private String playerName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPlayerNo() {
return playerNo;
}
public void setPlayerNo(int playerno) {
this.playerNo = playerno;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String getName() {
return playerName;
}
public void setName(String name) {
this.playerName = name;
}
@Override
public String toString() {
return String.format("%-2d %-2d\t %s %s", id, playerNo, team, playerName);
}
player. xml
<?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="Player">
<resultMap id="result" type="Player">
<result property="id" column="ID"/>
<result property="playerNo" column="PlayerNo"/>
<result property="team" column="Team"/>
<result property="playerName" column="PlayerName"/>
</resultMap>
<select id="selectAll" resultMap="result">
SELECT * FROM players;
</select>
<select id="selectById" resultMap="result">
SELECT * FROM players WHERE id = #{id}
</select>
<select id="selectByPlayerNo" parameterType="int" resultMap="result">
SELECT * FROM players WHERE playerno = #{playerNo}
</select>
<insert id="insert" parameterType="Player">
INSERT INTO players (playerno, team, playername) VALUES (#{playerNo}, #{team}, #{playerName});
</insert>
<update id="update" parameterType="Player">
UPDATE players
SET playerno = #{playerNo}, team = #{team}, playername = #{playerName}
WHERE id = #{id}
</update>
<delete id="delete" parameterType="int">
DELETE from players WHERE id = #{id}
</delete>
</mapper>
player_get. jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
<head>
<title>Players</title>
</head>
<body>
<h3 align = "center">List</h3>
</body>
<table align = "center">
<tr>
<td>ID</td>
<td>PlayerNo</td>
<td>Team</td>
<td>PlayerName</td>
</tr>
<s:iterator value = "players">
<tr>
<td><s:property value = "id"/></td>
<td><s:property value = "playerNo"/></td>
<td><s:property value = "team"/></td>
<td><s:property value = "playerName"/></td>
</tr>
</s:iterator>
</table>
</html>
PlayerAction . java
package com.macletek.struts2;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.macletek.sql.dao.*;
import com.macletek.sql.model.*;
import com.macletek.sql.mybatis.ConnectionFactory;
public class PlayerAction {
List<Player> players = new ArrayList<Player>();
public List<Player> getPlayers(){
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
public String execute() throws IOException{
PlayerDao playerDao = new PlayerDao(ConnectionFactory.getSqlSessionFactory());
players = playerDao.selectAll();
return "success";
}
}