我正在尝试习惯Spring项目中的Mybatis。
我将所有xml文件设置为创建datasource
,sqlsession
,sqlsessiontemplate
的bean,并且它永远不会生成错误。
所以这次,我尝试将一些基本信息插入到只有id和密码列的用户表中。
所以这是我下面的代码。
具有Sqlsessiontemplate对象和一些相关方法的AbstactDao.class。
public class AbstractDAO {
protected Log log = LogFactory.getLog(AbstractDAO.class);
@Autowired
private SqlSessionTemplate sqlSession;
protected void printQueryId(String queryId) {
if(log.isDebugEnabled()){
log.debug("\t QueryId \t: " + queryId);
}
}
public Object insert(String queryId, Object params){
printQueryId(queryId);
return sqlSession.insert(queryId, params);
}
public Object update(String queryId, Object params){
printQueryId(queryId);
return sqlSession.update(queryId, params);
}
public Object delete(String queryId, Object params){
printQueryId(queryId);
return sqlSession.delete(queryId, params);
}
public Object selectOne(String queryId){
printQueryId(queryId);
return sqlSession.selectOne(queryId);
}
public Object selectOne(String queryId, Object params){
printQueryId(queryId);
return sqlSession.selectOne(queryId, params);
}
@SuppressWarnings("rawtypes")
public List selectList(String queryId){
printQueryId(queryId);
return sqlSession.selectList(queryId);
}
@SuppressWarnings("rawtypes")
public List selectList(String queryId, Object params){
printQueryId(queryId);
return sqlSession.selectList(queryId,params);
}
}
用户类。此类中的信息将插入数据库
public class User {
private String id;
private String pw;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
}
mapper.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='bbs'>
<insert id="insertuser" parameterType="com.min.test.User">
insert into user (id,pw) values(#{id}, #{pw})
</insert>
</mapper>
此代码用于在main方法中执行insert命令。
User user = new User();
user.setId("zz");
user.setPw("dkdkdk");
AbstractDAO dao = new AbstractDAO();
dao.insert("bbs.insertuser", user);
错误讯息。
Servlet.service() for servlet [appServlet] in context with path [/practice] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.min.test.AbstractDAO.insert(AbstractDAO.java:31)
at com.min.test.HomeController.home(HomeController.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
您实例化AbstractDAO dao = new AbstractDAO();
,因此永远不会分配SqlSessionTemplate sqlSession
。
相反,它应标记为@Repository
,您应该通过@Autowired获取它,而不是手动创建实例。
BTW:抽象类不被实例化。而是定义通用T类型并为每个实体创建不同的dao实例,例如UserDao扩展了AbstractDao并使用UserDao与User。
一起工作