update一下经大佬提点后如何解决的
可能因为我曾用粗暴的方法改动过Log.java的类名,导致Log类编译出问题了,编译后类文件夹内根本没有Log.class文件。于是我又粗暴地delete了Log类重写了一次,果然没问题了。
顺便百度了一下如何修改类名
https://zhidao.baidu.com/question/340215163.html
原问题如下
小白入门程序媛,这个是一个mybatis的关于银行转账的小练习
在写log部分之前,我写的account部分测试过是没有问题的,转账的业务也成功了的。现在是想增加一个记录日志的操作,在数据库log表格添加转账记录,并且利用log4j在硬盘生成log file中记录转账信息。
可是按照同样的套路写了了log实体类后再写logmapper.xml,却出现了以下的错误,后来干脆连mybatis中的别名的部分都去掉了,直接在mapper.xml中使用完整的类名,还是说找不到我写的log类。实在搞不懂是namespace出错了还是怎么滴。。。。
错误信息如下:
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version: Apache Tomcat/8.5.42
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built: Jun 4 2019 20:29:04 UTC
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number: 8.5.42.0
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name: Windows 10
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version: 10.0
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture: amd64
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home: C:\Program Files\Java\jdk1.8.0_201\jre
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version: 1.8.0_201-b09
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor: Oracle Corporation
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE: D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME: D:\apache-tomcat-8.5.42
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=D:\apache-tomcat-8.5.42
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=D:\apache-tomcat-8.5.42\endorsed
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=UTF-8
七月 13, 2019 8:47:00 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: Loaded APR based Apache Tomcat Native library [1.2.21] using APR version [1.6.5].
七月 13, 2019 8:47:00 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
七月 13, 2019 8:47:00 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
七月 13, 2019 8:47:00 下午 org.apache.catalina.core.AprLifecycleListener initializeSSL
信息: OpenSSL successfully initialized [OpenSSL 1.1.1a 20 Nov 2018]
七月 13, 2019 8:47:00 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
七月 13, 2019 8:47:00 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
七月 13, 2019 8:47:00 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-nio-8009"]
七月 13, 2019 8:47:00 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
七月 13, 2019 8:47:00 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 828 ms
七月 13, 2019 8:47:00 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Catalina]
七月 13, 2019 8:47:00 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.5.42
七月 13, 2019 8:47:01 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
七月 13, 2019 8:47:02 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
七月 13, 2019 8:47:02 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
七月 13, 2019 8:47:02 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 1377 ms
七月 13, 2019 8:47:32 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [yan.ibbie.servlet.TransferServlet] in context with path [/bank] threw exception
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in yan/ibbie/mapper/logmapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'yan/ibbie/mapper/logmapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'yan.ibbie.pojo.Log'. Cause: java.lang.ClassNotFoundException: Cannot find class: yan.ibbie.pojo.Log
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at yan.ibbie.service.Impl.AccountServiceImpl.transfer(AccountServiceImpl.java:23)
at yan.ibbie.servlet.TransferServlet.service(TransferServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'yan/ibbie/mapper/logmapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'yan.ibbie.pojo.Log'. Cause: java.lang.ClassNotFoundException: Cannot find class: yan.ibbie.pojo.Log
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 26 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'yan/ibbie/mapper/logmapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'yan.ibbie.pojo.Log'. Cause: java.lang.ClassNotFoundException: Cannot find class: yan.ibbie.pojo.Log
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:373)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
... 28 more
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'yan.ibbie.pojo.Log'. Cause: java.lang.ClassNotFoundException: Cannot find class: yan.ibbie.pojo.Log
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:68)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:135)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:128)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:118)
... 31 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'yan.ibbie.pojo.Log'. Cause: java.lang.ClassNotFoundException: Cannot find class: yan.ibbie.pojo.Log
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
... 35 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: yan.ibbie.pojo.Log
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
... 37 more
mybatis.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- default填默认使用的environment的id -->
<environments default="default">
<!-- environment标签声明一个环境 -->
<environment id="default">
<!-- type="JDBC"表示使用原生JDBC事务 -->
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="1233210"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="yan/ibbie/mapper/accountmapper.xml"/>
<mapper resource="yan/ibbie/mapper/logmapper.xml"/>
</mappers>
</configuration>
accountmapper.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="yan.ibbie.mapper.accountmapper">
<!-- 根据账号和密码查询账户信息 -->
<select id="selByAccnoPassword" resultType="yan.ibbie.pojo.Account" parameterType="yan.ibbie.pojo.Account">
select * from account where accno=#{accNo} and password=#{password}
</select>
<!-- 根据账号和姓名查询账户信息 -->
<select id="selByAccnoName" resultType="yan.ibbie.pojo.Account" parameterType="yan.ibbie.pojo.Account">
select * from account where accno=#{accNo} and name=#{name}
</select>
<!-- 根据账号修改账户余额 -->
<update id="updBalanceByAccno" parameterType="yan.ibbie.pojo.Account">
update account set balance=balance+#{balance} where accno=#{accNo}
</update>
</mapper>
logmapper.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="yan.ibbie.mapper.logmapper">
<insert id="insLog" parameterType="yan.ibbie.pojo.Log">
insert into log values(default,#{accOut},#{accIn},#{money})
</insert>
</mapper>
yan.ibbie.pojo.log如下
package yan.ibbie.pojo;
public class Log {
private int id;
private String accOut;
private String accIn;
private double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAccOut() {
return accOut;
}
public void setAccOut(String accOut) {
this.accOut = accOut;
}
public String getAccIn() {
return accIn;
}
public void setAccIn(String accIn) {
this.accIn = accIn;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
AccountServiceImpl如下:
package yan.ibbie.service.Impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.sun.istack.internal.logging.Logger;
import yan.ibbie.pojo.Account;
import yan.ibbie.pojo.Log;
import yan.ibbie.service.AccountService;
public class AccountServiceImpl implements AccountService {
@Override
public int transfer(Account accIn, Account accOut) throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
Account accOutSelect = session.selectOne("yan.ibbie.mapper.accountmapper.selByAccnoPassword",accOut);
if (accOutSelect!=null) {
if(accOutSelect.getBalance()>=accOut.getBalance()) {
Account accInSelect = session.selectOne("yan.ibbie.mapper.accountmapper.selByAccnoName",accIn);
if (accInSelect!=null) {
accIn.setBalance(accOut.getBalance());
accOut.setBalance(-accOut.getBalance());
int index=session.update("yan.ibbie.mapper.accountmapper.updBalanceByAccno", accOut);
index += session.update("yan.ibbie.mapper.accountmapper.updBalanceByAccno",accIn);
if (index==2) {
Log log = new Log();
log.setAccOut(accOut.getAccNo());
log.setAccIn(accIn.getAccNo());
log.setMoney(accIn.getBalance());
session.update("yan.ibbie.mapper.logmapper.insLog", log);
Logger logger = Logger.getLogger(AccountServiceImpl.class);
logger.info(log.getAccOut()+"给"+log.getAccIn()+
"在"+new Date().toLocaleString()+"转了"+log.getMoney());
session.commit();
session.close();
return SUCCESS;
}else {
session.rollback();
session.close();
return ERROR;
}
}else {
return ACCOUNT_NAME_NOT_MATCH;
}
}else {
return ACCOUNT_BALANCE_NOT_ENOUGHT;
}
}else {
return ACCOUNT_PASSWORD_NOT_MATCH;
}
}
}