Return MyBatis 中的一个列表<MyClass>
Return a List<MyClass> in MyBatis
我使用 MyBatis 创建了映射器(Java 接口 + XML):
JAVA 界面:
@Mapper
@Repository
@Primary
public interface DirezioneRegionaleMapper {
List<DirezioneRegionale> getAllDirRegs();
}
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="com.listgroup.piu.dao.DirezioneRegionaleMapper">
<resultMap id="DirezioneRegionaleList" type="com.listgroup.piu.implementation.DirezioneRegionale">
<id column="ORG_LIV4_UO" jdbcType="NUMERIC" property="ID" />
<result column="ORG_LIV4_DES" jdbcType="VARCHAR" property="description" />
</resultMap>
<select id="getAllDirRegs" resultMap="DirezioneRegionaleList">
SELECT DISTINCT t.ORG_LIV4_UO, t.ORG_LIV4_DES
FROM DMM_DIM_STRUTTURA_ORG_HIS_GEN t
</select>
</mapper>
服务CLASS:
@Service
@Transactional
public class DirezioneRegionaleService {
@Autowired
private DirezioneRegionaleMapper direzioneRegionaleMapper;
@Transactional(readOnly = true)
public List<DirezioneRegionale> getAllDirRegs() {
return this.direzioneRegionaleMapper.getAllDirRegs();
}
}
和 DirezioneRegionale class:
public class DirezioneRegionale {
private final int ID;
private final String description;
private List<DirezioneCommerciale> direzioniCommerciali;
public DirezioneRegionale(int ID, String description) {
this.ID = ID;
this.description = description;
this.direzioniCommerciali = null;
}
public void setDirezioniCommerciali(List<DirezioneCommerciale> direzioniCommerciali) {
this.direzioniCommerciali = direzioniCommerciali;
}
public int getID() {
return this.ID;
}
public String getDescription() {
return this.description;
}
public List<DirezioneCommerciale> getDirezioniCommerciali() {
return this.direzioniCommerciali;
}
}
我想做这样的事情:在我的 main 中,我想创建一个服务实例 class 然后调用方法 "getAllDirRegs()",我想获得一个列表,为此,我已经在 XML 文件中创建了 resultMap,但是有些东西不起作用,因为当 运行 程序时,我有一个 NullPointerException。
这是我的主要内容:
public static void main(String[] args) {
SpringApplication.run(PiuApplication.class, args);
DirezioneRegionaleService service = new DirezioneRegionaleService();
List<DirezioneRegionale> dirRegs = service.getAllDirRegs();
System.out.println(dirRegs);
}
编辑:
这是我的堆栈跟踪:
java.lang.NullPointerException
at com.listgroup.piu.service.DirezioneRegionaleService.getAllDirRegs(DirezioneRegionaleService.java:20)
at com.listgroup.piu.PiuApplication.main(PiuApplication.java:28)
这是我的日志:
2020-02-25 21:08:27,687 INFO default [main] com.listgroup.piu.PiuApplication logStarting [StartupInfoLogger.java:48] Starting PiuApplication on IT586ADIMAGGI1L with PID 15572 (C:\Users\Antonino\Documents\isp.piuv2\target\classes started by Antonino in C:\Users\Antonino\Documents\isp.piuv2)
2020-02-25 21:08:27,689 DEBUG default [main] com.listgroup.piu.PiuApplication logStarting [StartupInfoLogger.java:51] Running with Spring Boot v1.5.19.RELEASE, Spring v4.3.22.RELEASE
2020-02-25 21:08:27,689 INFO default [main] com.listgroup.piu.PiuApplication logStartupProfileInfo [SpringApplication.java:597] The following profiles are active: dblistto
2020-02-25 21:08:27,717 INFO default [main] org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext prepareRefresh [AbstractApplicationContext.java:583] Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5c33f1a9: startup date [Tue Feb 25 21:08:27 CET 2020]; root of context hierarchy
2020-02-25 21:08:27,776 INFO default [background-preinit] org.hibernate.validator.internal.util.Version <clinit> [Version.java:30] HV000001: Hibernate Validator 5.3.6.Final
2020-02-25 21:08:28,421 INFO default [main] org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer initialize [TomcatEmbeddedServletContainer.java:92] Tomcat initialized with port(s): 8081 (http)
2020-02-25 21:08:28,428 INFO default [main] org.apache.coyote.http11.Http11NioProtocol log [DirectJDKLog.java:183] Initializing ProtocolHandler ["http-nio-8081"]
2020-02-25 21:08:28,431 INFO default [main] org.apache.catalina.core.StandardService log [DirectJDKLog.java:183] Starting service Tomcat
2020-02-25 21:08:28,431 INFO default [main] org.apache.catalina.core.StandardEngine log [DirectJDKLog.java:183] Starting Servlet Engine: Apache Tomcat/7.0.59
2020-02-25 21:08:28,519 INFO default [localhost-startStop-1] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] log [DirectJDKLog.java:183] Initializing Spring embedded WebApplicationContext
2020-02-25 21:08:28,519 INFO default [localhost-startStop-1] org.springframework.web.context.ContextLoader prepareEmbeddedWebApplicationContext [EmbeddedWebApplicationContext.java:287] Root WebApplicationContext: initialization completed in 802 ms
2020-02-25 21:08:28,625 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.ServletRegistrationBean onStartup [ServletRegistrationBean.java:191] Mapping servlet: 'dispatcherServlet' to [/]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'metricsFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'characterEncodingFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'requestContextFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'webRequestLoggingFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'requestLoggingFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'applicationContextIdFilter' to: [/*]
2020-02-25 21:08:28,634 DEBUG default [localhost-startStop-1] org.springframework.web.filter.CommonsRequestLoggingFilter init [GenericFilterBean.java:208] Initializing filter 'requestLoggingFilter'
2020-02-25 21:08:28,634 DEBUG default [localhost-startStop-1] org.springframework.web.filter.CommonsRequestLoggingFilter init [GenericFilterBean.java:239] Filter 'requestLoggingFilter' configured successfully
2020-02-25 21:08:28,997 INFO default [main] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache [RequestMappingHandlerAdapter.java:556] Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5c33f1a9: startup date [Tue Feb 25 21:08:27 CET 2020]; root of context hierarchy
2020-02-25 21:08:29,027 INFO default [main] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-02-25 21:08:29,028 INFO default [main] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-02-25 21:08:29,044 INFO default [main] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler [AbstractUrlHandlerMapping.java:362] Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-25 21:08:29,044 INFO default [main] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler [AbstractUrlHandlerMapping.java:362] Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-25 21:08:29,062 INFO default [main] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler [AbstractUrlHandlerMapping.java:362] Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-25 21:08:29,206 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,207 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2020-02-25 21:08:29,207 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,208 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
2020-02-25 21:08:29,208 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
2020-02-25 21:08:29,208 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,209 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
2020-02-25 21:08:29,209 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,210 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,210 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,211 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,211 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2020-02-25 21:08:29,211 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,306 INFO default [main] org.springframework.jmx.export.annotation.AnnotationMBeanExporter afterSingletonsInstantiated [MBeanExporter.java:431] Registering beans for JMX exposure on startup
2020-02-25 21:08:29,309 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter afterSingletonsInstantiated [MBeanExporter.java:431] Registering beans for JMX exposure on startup
2020-02-25 21:08:29,311 INFO default [main] org.springframework.context.support.DefaultLifecycleProcessor start [DefaultLifecycleProcessor.java:345] Starting beans in phase 0
2020-02-25 21:08:29,312 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'auditEventsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=auditEventsEndpoint]
2020-02-25 21:08:29,319 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'requestMappingEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=requestMappingEndpoint]
2020-02-25 21:08:29,324 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'environmentEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=environmentEndpoint]
2020-02-25 21:08:29,326 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'healthEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=healthEndpoint]
2020-02-25 21:08:29,327 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'beansEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=beansEndpoint]
2020-02-25 21:08:29,329 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'infoEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=infoEndpoint]
2020-02-25 21:08:29,330 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'loggersEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=loggersEndpoint]
2020-02-25 21:08:29,333 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'metricsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=metricsEndpoint]
2020-02-25 21:08:29,334 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'traceEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=traceEndpoint]
2020-02-25 21:08:29,336 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'dumpEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=dumpEndpoint]
2020-02-25 21:08:29,337 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'autoConfigurationReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=autoConfigurationReportEndpoint]
2020-02-25 21:08:29,338 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'configurationPropertiesReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint]
2020-02-25 21:08:29,346 INFO default [main] org.apache.coyote.http11.Http11NioProtocol log [DirectJDKLog.java:183] Starting ProtocolHandler ["http-nio-8081"]
2020-02-25 21:08:29,352 INFO default [main] org.apache.tomcat.util.net.NioSelectorPool log [DirectJDKLog.java:183] Using a shared selector for servlet write/read
2020-02-25 21:08:29,359 INFO default [main] org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer start [TomcatEmbeddedServletContainer.java:216] Tomcat started on port(s): 8081 (http)
2020-02-25 21:08:29,362 INFO default [main] com.listgroup.piu.PiuApplication logStarted [StartupInfoLogger.java:57] Started PiuApplication in 1.836 seconds (JVM running for 2.362)
Spring @Autowired
注释仅适用于 Spring.
创建的对象
它不会初始化您使用new
关键字创建的属性。
相反,如 所述,您必须将代码移动到另一个 class。
@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
@Autowired private DirezioneRegionaleService service;
@Override public void run(String...args) throws Exception {
List<DirezioneRegionale> dirRegs = service.getAllDirRegs();
System.out.println(dirRegs);
}
}
此 class 将由 Spring 自动创建,因此会知道已初始化的服务。
run
方法也会在Spring初始化一切后自动执行
我使用 MyBatis 创建了映射器(Java 接口 + XML):
JAVA 界面:
@Mapper
@Repository
@Primary
public interface DirezioneRegionaleMapper {
List<DirezioneRegionale> getAllDirRegs();
}
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="com.listgroup.piu.dao.DirezioneRegionaleMapper">
<resultMap id="DirezioneRegionaleList" type="com.listgroup.piu.implementation.DirezioneRegionale">
<id column="ORG_LIV4_UO" jdbcType="NUMERIC" property="ID" />
<result column="ORG_LIV4_DES" jdbcType="VARCHAR" property="description" />
</resultMap>
<select id="getAllDirRegs" resultMap="DirezioneRegionaleList">
SELECT DISTINCT t.ORG_LIV4_UO, t.ORG_LIV4_DES
FROM DMM_DIM_STRUTTURA_ORG_HIS_GEN t
</select>
</mapper>
服务CLASS:
@Service
@Transactional
public class DirezioneRegionaleService {
@Autowired
private DirezioneRegionaleMapper direzioneRegionaleMapper;
@Transactional(readOnly = true)
public List<DirezioneRegionale> getAllDirRegs() {
return this.direzioneRegionaleMapper.getAllDirRegs();
}
}
和 DirezioneRegionale class:
public class DirezioneRegionale {
private final int ID;
private final String description;
private List<DirezioneCommerciale> direzioniCommerciali;
public DirezioneRegionale(int ID, String description) {
this.ID = ID;
this.description = description;
this.direzioniCommerciali = null;
}
public void setDirezioniCommerciali(List<DirezioneCommerciale> direzioniCommerciali) {
this.direzioniCommerciali = direzioniCommerciali;
}
public int getID() {
return this.ID;
}
public String getDescription() {
return this.description;
}
public List<DirezioneCommerciale> getDirezioniCommerciali() {
return this.direzioniCommerciali;
}
}
我想做这样的事情:在我的 main 中,我想创建一个服务实例 class 然后调用方法 "getAllDirRegs()",我想获得一个列表,为此,我已经在 XML 文件中创建了 resultMap,但是有些东西不起作用,因为当 运行 程序时,我有一个 NullPointerException。
这是我的主要内容:
public static void main(String[] args) {
SpringApplication.run(PiuApplication.class, args);
DirezioneRegionaleService service = new DirezioneRegionaleService();
List<DirezioneRegionale> dirRegs = service.getAllDirRegs();
System.out.println(dirRegs);
}
编辑:
这是我的堆栈跟踪:
java.lang.NullPointerException
at com.listgroup.piu.service.DirezioneRegionaleService.getAllDirRegs(DirezioneRegionaleService.java:20)
at com.listgroup.piu.PiuApplication.main(PiuApplication.java:28)
这是我的日志:
2020-02-25 21:08:27,687 INFO default [main] com.listgroup.piu.PiuApplication logStarting [StartupInfoLogger.java:48] Starting PiuApplication on IT586ADIMAGGI1L with PID 15572 (C:\Users\Antonino\Documents\isp.piuv2\target\classes started by Antonino in C:\Users\Antonino\Documents\isp.piuv2)
2020-02-25 21:08:27,689 DEBUG default [main] com.listgroup.piu.PiuApplication logStarting [StartupInfoLogger.java:51] Running with Spring Boot v1.5.19.RELEASE, Spring v4.3.22.RELEASE
2020-02-25 21:08:27,689 INFO default [main] com.listgroup.piu.PiuApplication logStartupProfileInfo [SpringApplication.java:597] The following profiles are active: dblistto
2020-02-25 21:08:27,717 INFO default [main] org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext prepareRefresh [AbstractApplicationContext.java:583] Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5c33f1a9: startup date [Tue Feb 25 21:08:27 CET 2020]; root of context hierarchy
2020-02-25 21:08:27,776 INFO default [background-preinit] org.hibernate.validator.internal.util.Version <clinit> [Version.java:30] HV000001: Hibernate Validator 5.3.6.Final
2020-02-25 21:08:28,421 INFO default [main] org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer initialize [TomcatEmbeddedServletContainer.java:92] Tomcat initialized with port(s): 8081 (http)
2020-02-25 21:08:28,428 INFO default [main] org.apache.coyote.http11.Http11NioProtocol log [DirectJDKLog.java:183] Initializing ProtocolHandler ["http-nio-8081"]
2020-02-25 21:08:28,431 INFO default [main] org.apache.catalina.core.StandardService log [DirectJDKLog.java:183] Starting service Tomcat
2020-02-25 21:08:28,431 INFO default [main] org.apache.catalina.core.StandardEngine log [DirectJDKLog.java:183] Starting Servlet Engine: Apache Tomcat/7.0.59
2020-02-25 21:08:28,519 INFO default [localhost-startStop-1] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] log [DirectJDKLog.java:183] Initializing Spring embedded WebApplicationContext
2020-02-25 21:08:28,519 INFO default [localhost-startStop-1] org.springframework.web.context.ContextLoader prepareEmbeddedWebApplicationContext [EmbeddedWebApplicationContext.java:287] Root WebApplicationContext: initialization completed in 802 ms
2020-02-25 21:08:28,625 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.ServletRegistrationBean onStartup [ServletRegistrationBean.java:191] Mapping servlet: 'dispatcherServlet' to [/]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'metricsFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'characterEncodingFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'requestContextFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'webRequestLoggingFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'requestLoggingFilter' to: [/*]
2020-02-25 21:08:28,626 INFO default [localhost-startStop-1] org.springframework.boot.web.servlet.FilterRegistrationBean configure [AbstractFilterRegistrationBean.java:259] Mapping filter: 'applicationContextIdFilter' to: [/*]
2020-02-25 21:08:28,634 DEBUG default [localhost-startStop-1] org.springframework.web.filter.CommonsRequestLoggingFilter init [GenericFilterBean.java:208] Initializing filter 'requestLoggingFilter'
2020-02-25 21:08:28,634 DEBUG default [localhost-startStop-1] org.springframework.web.filter.CommonsRequestLoggingFilter init [GenericFilterBean.java:239] Filter 'requestLoggingFilter' configured successfully
2020-02-25 21:08:28,997 INFO default [main] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache [RequestMappingHandlerAdapter.java:556] Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5c33f1a9: startup date [Tue Feb 25 21:08:27 CET 2020]; root of context hierarchy
2020-02-25 21:08:29,027 INFO default [main] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-02-25 21:08:29,028 INFO default [main] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-02-25 21:08:29,044 INFO default [main] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler [AbstractUrlHandlerMapping.java:362] Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-25 21:08:29,044 INFO default [main] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler [AbstractUrlHandlerMapping.java:362] Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-25 21:08:29,062 INFO default [main] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler [AbstractUrlHandlerMapping.java:362] Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-25 21:08:29,206 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,207 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2020-02-25 21:08:29,207 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,208 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
2020-02-25 21:08:29,208 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
2020-02-25 21:08:29,208 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,209 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
2020-02-25 21:08:29,209 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,210 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,210 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,211 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,211 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2020-02-25 21:08:29,211 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2020-02-25 21:08:29,212 INFO default [main] org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping register [AbstractHandlerMethodMapping.java:544] Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2020-02-25 21:08:29,306 INFO default [main] org.springframework.jmx.export.annotation.AnnotationMBeanExporter afterSingletonsInstantiated [MBeanExporter.java:431] Registering beans for JMX exposure on startup
2020-02-25 21:08:29,309 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter afterSingletonsInstantiated [MBeanExporter.java:431] Registering beans for JMX exposure on startup
2020-02-25 21:08:29,311 INFO default [main] org.springframework.context.support.DefaultLifecycleProcessor start [DefaultLifecycleProcessor.java:345] Starting beans in phase 0
2020-02-25 21:08:29,312 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'auditEventsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=auditEventsEndpoint]
2020-02-25 21:08:29,319 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'requestMappingEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=requestMappingEndpoint]
2020-02-25 21:08:29,324 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'environmentEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=environmentEndpoint]
2020-02-25 21:08:29,326 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'healthEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=healthEndpoint]
2020-02-25 21:08:29,327 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'beansEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=beansEndpoint]
2020-02-25 21:08:29,329 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'infoEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=infoEndpoint]
2020-02-25 21:08:29,330 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'loggersEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=loggersEndpoint]
2020-02-25 21:08:29,333 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'metricsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=metricsEndpoint]
2020-02-25 21:08:29,334 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'traceEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=traceEndpoint]
2020-02-25 21:08:29,336 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'dumpEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=dumpEndpoint]
2020-02-25 21:08:29,337 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'autoConfigurationReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=autoConfigurationReportEndpoint]
2020-02-25 21:08:29,338 INFO default [main] org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter registerBeanInstance [MBeanExporter.java:679] Located managed bean 'configurationPropertiesReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint]
2020-02-25 21:08:29,346 INFO default [main] org.apache.coyote.http11.Http11NioProtocol log [DirectJDKLog.java:183] Starting ProtocolHandler ["http-nio-8081"]
2020-02-25 21:08:29,352 INFO default [main] org.apache.tomcat.util.net.NioSelectorPool log [DirectJDKLog.java:183] Using a shared selector for servlet write/read
2020-02-25 21:08:29,359 INFO default [main] org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer start [TomcatEmbeddedServletContainer.java:216] Tomcat started on port(s): 8081 (http)
2020-02-25 21:08:29,362 INFO default [main] com.listgroup.piu.PiuApplication logStarted [StartupInfoLogger.java:57] Started PiuApplication in 1.836 seconds (JVM running for 2.362)
Spring @Autowired
注释仅适用于 Spring.
它不会初始化您使用new
关键字创建的属性。
相反,如
@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
@Autowired private DirezioneRegionaleService service;
@Override public void run(String...args) throws Exception {
List<DirezioneRegionale> dirRegs = service.getAllDirRegs();
System.out.println(dirRegs);
}
}
此 class 将由 Spring 自动创建,因此会知道已初始化的服务。
run
方法也会在Spring初始化一切后自动执行