我一直在做一个webapp(一个数据库的CRUD接口),我使用jpetstore作为指导,除了:
- 我也在使用 mybatis-generator 的代码。
- 服务。我不想将服务作为单独的部分。我想直接在 Action bean 上使用 Mappers
- 我在一个文件夹 (org.lmb97.data) 中拥有所有 mappers.xml、mappers.java、example.java 和 entity.java(所有生成器的代码)
当我尝试使用映射器时,我遇到了 NullPointerException。我检查了好几次,但仍然没有找到解决方案。我不知道是哪个问题。我已经尽力了……但不知道。我认为这与配置有关。我检查了错误跟踪器中似乎相关但没有相关的各种错误。
我将在这里粘贴一些我认为有趣的部分:
这是 applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- transaction manager, use JtaTransactionManager for global tx -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.56.2/txomon_lmb97"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
<!-- In theory, this has to be for making a transaction manager (don't know what it is for) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="org.lmb97.data" />
</bean>
<!-- enable component scanning (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="org.lmb97" />
<!-- enable autowire -->
<context:annotation-config />
<!-- enable transaction demarcation with annotations -->
<tx:annotation-driven />
<!-- scan for mappers and let them be autowired -->
<bean id="mybatisMapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.lmb97.data.*Mapper" />
</bean>
</beans>
这是 web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>StripesResources</param-value>
</context-param>
<!-- Aqui empiezo a definir Stripes -->
<filter>
<display-name>Stripes Filter</display-name>
<filter-name>StripesFilter</filter-name>
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
<init-param>
<param-name>ActionResolver.Packages</param-name>
<param-value>org.lmb97.web.action</param-value>
</init-param>
<init-param>
<param-name>Interceptor.Classes</param-name>
<param-value>net.sourceforge.stripes.integration.spring.SpringInterceptor</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>StripesDispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>StripesDispatcher</servlet-name>
<servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>StripesDispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--Aqui empiezo a definir Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
正如我所说,我不想有一个服务层,我直接在 ActionBean 上做这一切,在 EventsActionBean 的第 76 行抛出异常https://github.com/txomon/Universidad/blob/eb7753e32a2eccbf4e5e81e815d68b929bcb2e61/3/LabTD/practica2/src/java/org/lmb97/web/action/EventsActionBean.java#L74
。我第一次调用映射器。
我有 apache tomcat 的日志和普通日志,在这里配置了 ALL 中的 log4j,它位于 .7z 中,因为它们是很棒的日志。并且 repo 在这里,我正在放置确切的提交,这样如果我使用 repo,您可以看到实际状态。
netbeans 项目已配置为,如果您克隆 repo,您将拥有包含在其中的所有库,而没有外部依赖项。
欢迎任何帮助/想法,您可以要求我测试,
干杯,并提前感谢您!