在GameDetailImageMapper中添加代码
package spring;
import entities.GameDetailImage;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class GameDetailImageMapper implements RowMapper
{
public GameDetailImage mapRow(ResultSet rs,int rowNum) throws SQLException
{
GameDetailImage gDetailImage = new GameDetailImage();
System.out.println("++++++++++++"+rs.toString());
gDetailImage.getGameDetail().setGameid( rs.getInt("game_id"));
gDetailImage.getGameDetail().setTitle( rs.getString("title"));
gDetailImage.getGameDetail().setDeveloper( rs.getString("developer"));
gDetailImage.getGameDetail().setPublisher( rs.getString("publisher"));
gDetailImage.getGameDetail().setGenre( rs.getString("genre"));
gDetailImage.getGameDetail().setPlatform( rs.getString("platform"));
gDetailImage.getGameDetail().setGameDescription( rs.getString("game_description"));
gDetailImage.getGameDetail().setDistribution( rs.getString("distribution"));
gDetailImage.getGameDetail().setReleaseDate( rs.getDate("release_date"));
gDetailImage.getGameDetail().setCreateDate( rs.getDate("create_date"));
gDetailImage.getGameDetail().setScore( rs.getInt("score"));
gDetailImage.getGameImage().setBanner_link( rs.getString("banner_link"));
gDetailImage.getGameImage().setBanner_title( rs.getString("banner_title"));
gDetailImage.getGameImage().setHero_link( rs.getString("hero_link"));
gDetailImage.getGameImage().setHero_title( rs.getString("hero_title"));
gDetailImage.getGameImage().setThumbnail_link( rs.getString("thumbnail_link"));
gDetailImage.getGameImage().setThumbnail_title( rs.getString("thumbnail_title"));
gDetailImage.getGameImage().setContent_image_link( rs.getString("content_image_link"));
gDetailImage.getGameImage().setContent_image_title( rs.getString("content_image_title"));
return gDetailImage;
}
}
现在,我已将上下文配置指向data.xml文件,我得到了以下异常。不知道为什么会这样,因为查询从我的数据库返回了正确的数据。我在映射器中使用了一个复合对象(复合物中包含两个对象)。可能是引起问题的原因吗?
Stacktrace:] with root cause
java.lang.NullPointerException
at spring.GameDetailImageMapper.mapRow(GameDetailImageMapper.java:23)
at spring.GameDetailImageMapper.mapRow(GameDetailImageMapper.java:17)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:455)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:400)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:466)
我的简单Spring应用程序遇到以下异常。考虑一下,因为我没有为jdbcTemplate使用正确的弹簧注入。我使用没有Maven的spring,所以没有pom.xml。在Tomcat上运行。
Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate spring.GameDetailImageManagement.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
data.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/GameDisplay" />
<property name="username" value= />
<property name="password" value= />
<property name="initialSize" value="2" />
<property name="maxActive" value="5" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg type="javax.sql.DataSource" name="dataSource" ref="dataSource"/>
</bean>
<context:annotation-config />
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!--Handle @Autowired-->
<context:annotation-config />
</beans>
`GameDetailImageController:
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;
import spring.GameDetailImageManagement;
@Controller
public class GameDetailImageController
{
@Autowired
private GameDetailImageManagement gdm;
@RequestMapping("/index")
protected ModelAndView listGameDetailImages
(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView("index");
//ModelAndView mv = new ModelAndView("index", "gameDetailImages", dao.GameDetailImageDAO.getGameDetailImages());
mv.addObject("gameDetailImages", gdm.getGameDetailImages());
return mv;
}
}
GameDetailImageManagement
package spring;
import entities.GameDetailImage;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class GameDetailImageManagement
{
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource)
{
this.jdbcTemplate.setDataSource(dataSource);
}
/*public void setJdbcTemplate(JdbcTemplate template)
{
this.jdbcTemplate = template;
}*/
public List getGameDetailImages()
{
//List templist;
//templist = jdbcTemplate.query("select * from GameDetail Inner Join GameImage on GameDetail.game_id = GameImage.game_id Order By GameDetail.create_date", new GameDetailImageMapper());
//System.out.println("++++===++++"+templist.toString());
return this.jdbcTemplate.query("select * from GameDetail Inner Join GameImage on GameDetail.game_id = GameImage.game_id Order By GameDetail.create_date", new GameDetailImageMapper());
}
}
答案 0 :(得分:0)
感谢将配置文件指向我的data.xml,这是第一步。下一步是我没有正确使用映射器。我不使用getGameDetail()和getGameImage()将吸气剂和吸气剂直接添加到GameDetailImageMapper中。