我想通過老師教程寫個用戶注冊登錄的模塊 但是遇到谷歌都解決不掉的問題
報錯信息:
### 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; } }
求科普啊,實例化也不行
2017-06-19
實體類要滿足javabean的設計原則
2017-08-03
老師的教程不是這樣教的
2017-05-30
問題解決了?
問題出在實體類身上
User.java
我按照老師的教程打的 從數據庫復制的大寫字段,為啥我調用就出錯? ?老師能給我解答一下嗎 ?這讓我很是無奈,計算機的邏輯太操蛋了
2017-05-30
https://github.com/mybatis/mybatis-3/issues/329
不知道是不是這個問題,誒,
再貼上servlet;
LoginServlet.java;