我想通过老师教程写个用户注册登录的模块 但是遇到谷歌都解决不掉的问题
报错信息:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'username' of 'class com.wfion.entity.User' with value 'demo' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'username' in 'class com.wfion.entity.User'
### The error may exist in com/wfion/config/sqlxml/User.xml
### The error may involve User.loginquery
### The error occurred while handling results
### SQL: select ID,USERNAME,PASSWORD from USER where USERNAME=?
config配置文件;
<?xml version="1.0" encoding="UTF-8" ?> <!-- Copyright 2009-2016 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- <settings> <setting name="useGeneratedKeys" value="false"/> <setting name="useColumnLabel" value="true"/> </settings> <typeAliases> <typeAlias alias="UserAlias" type="com.wfion.entity.User"/> </typeAliases> --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/autodistribute"/> <property name="username" value="demo"/> <property name="password" value="demo"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/wfion/config/sqlxml/User.xml"/> </mappers> </configuration>
User.xml;
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright 2009-2016 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="User"> <!-- type是配置的类,id是自定义的resultMap名称 --> <resultMap type="com.wfion.entity.User" id="UserResult"> <id column="ID" jdbcType="INTEGER" property="id"/> <result column="USERNAME" jdbcType="VARCHAR" property="username"/> <result column="PASSWORD" jdbcType="VARCHAR" property="password"/> </resultMap> <!-- resultMap相同才会匹配到然后执行SQL语句 parameterType为注入对象类型 --> <select id="loginquery" parameterType="com.wfion.entity.User" resultMap="UserResult"> select ID,USERNAME,PASSWORD from USER where USERNAME=#{username} </select> </mapper>
User.java
package com.wfion.entity; public class User { private int ID; private String USERNAME; private String PASSWORD; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getUSERNAME() { return USERNAME; } public void setUSERNAME(String uSERNAME) { USERNAME = uSERNAME; } public String getPASSWORD() { return PASSWORD; } public void setPASSWORD(String pASSWORD) { PASSWORD = pASSWORD; } }
UserDao.java
package com.wfion.dao; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.ibatis.session.SqlSession; import com.wfion.db.DbAccess; import com.wfion.entity.User; /** * 和User相关的数据库操作 * */ public class UserDao { /** * 处理用户登录判断 */ public User loginQuery(String username){ DbAccess dbAccess=new DbAccess(); SqlSession sqlSession=null; User user=new User(); user.setUSERNAME(username); try { sqlSession=dbAccess.getSqlSession(); user=sqlSession.selectOne("User.loginquery",username); //通过sqlSession执行sql语句 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(sqlSession!=null){ sqlSession.close(); } } return user; } /** * 处理用户登录判断 */ // public User loginQuery(String username){ // User user=null; // try { // Class.forName("com.mysql.jdbc.Driver"); // Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/autodistribute","demo","demo"); // String sql="select ID,USERNAME,PASSWORD from USER where USERNAME=?"; // PreparedStatement statement=conn.prepareStatement(sql); // statement.setString(1, username); // ResultSet rs=statement.executeQuery(); // while(rs.next()){ // user=new User(); // user.setID(rs.getInt("ID")); // user.setUSERNAME(rs.getString("USERNAME")); // user.setPASSWORD(rs.getString("PASSWORD")); // } // } catch (ClassNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // return user; // // } }
DbAccess.java
package com.wfion.db; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class DbAccess { public SqlSession getSqlSession() throws IOException{ //通过配置文件获取数据库信息 Reader reader=Resources.getResourceAsReader("com/wfion/config/Configuration.xml"); //通过配置文件构建一个SqlSessionFactory SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); //通过SqlSessionFactory打开一个数据库会话 SqlSession sqlSession=sqlSessionFactory.openSession(); return sqlSession; } }
求科普啊,实例化也不行