0

I recently studied about how to use MyBatis + Struts2. I create a table in MSSQL named Players. There are 4 columns, ID, PlayerNo, Team, PlayerName. PlayerName is the only column that I can't show on the page successfully.

Hope someone can help to read my code and give some advice, thanks. Image: enter image description here

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";
    }
}
  • The getter/setter names should be `getPlayerName`/`setPlayerName`, but they actually are `getName`/`setName`. Could this be the reason? – ave Mar 24 '20 at 19:00
  • https://stackoverflow.com/a/35488593/573032 – Roman C Mar 24 '20 at 23:31
  • @ave It's just a method name. I change it to getPlayerName/ setPlayerName, still not working. – Josh Chiang Mar 25 '20 at 02:50
  • @RomanC Thanks for the reference. But I think I did all the getter and setter. As the image I post, I can get all columns but PlayerName. Not sure where the problem is. – Josh Chiang Mar 25 '20 at 03:04
  • 1
    @JoshChiang Please enable TRACE level [logging](https://mybatis.org/mybatis-3/logging.html) and add the output (statement, parameters and results) to the question. – ave Mar 25 '20 at 05:32
  • It is not a problem with column name but the one with name of the column. Explicit naming and/or aliasing might change the behaviour of database queries. Make sure you have enabled the mapper logging at least to debug level to see SQL query statements while executing. The best practice is to run the queries in the SQL editor before coding. – Roman C Mar 25 '20 at 23:00
  • @RomanC Thanks for your advice. The mapper seems right and it's ok now after I re-write my code. Still not sure what cause this missing, but still appreciate for your suggestions. – Josh Chiang Mar 26 '20 at 06:07
  • I asked you to show us the log output because it's unclear if the issue is 1) in the data retrieval or 2) in the rendering. What is output if you add `System.out.println("PlayerName: " + players.get(0).getPlayerName());` to the `PlayerAction#execute()` method? – ave Mar 27 '20 at 10:24

0 Answers0