2017-11-28 12 views
0

私はエラーを取得しています:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:不明な列「user0_.userId」「フィールドリスト」で

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'user0_.userId' in 'field list'

コードをチェックして、私に返信してください。

ステップ1:作成したデータベーステーブル

CREATE TABLE `userlogin` (
    `email` varchar(20) DEFAULT NULL, 
    `password` varchar(20) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

例:

email = [email protected] 
pwd = 1234 

ステップ2:エンティティ・クラス

package com.ims.entity; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 

@Entity 
@Table(name="userlogin") 
public class User { 

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long userId; 

    @Column(name="email") 
    private String email; 

    @Column(name="password") 
    private String password; 

//setter and getter methods 

ステップ3:Beanクラス

package com.ims.bean; 

import org.hibernate.validator.constraints.NotEmpty; 

public class LoginBean { 

    @NotEmpty 
    private String emailid; 
    @NotEmpty 
    private String password; 

    public String getEmail() { 
     return emailid; 
    } 

    public void setEmail(String email) { 
     this.emailid = email; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

ステップ4:コントローラクラス

package com.ims.controller; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.ims.bean.LoginBean; 
import com.ims.entity.User; 
import com.ims.service.UserService; 

@Controller 
public class LoginController extends BaseController{ 

    @Autowired 
    private UserService userService; 

    @RequestMapping({"/","/home"}) 
    public String viewHomePage(Model model){ 
     model.addAttribute(new LoginBean()); 
     return "home"; 
    } 

ステップ5:サービスクラス

package com.ims.service; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Propagation; 
import org.springframework.transaction.annotation.Transactional; 

import com.ims.bean.LoginBean; 
import com.ims.dao.UserDao; 
import com.ims.entity.User; 

@Service 
@Transactional(propagation=Propagation.REQUIRED) 
public class UserService { 

    @Autowired 
    private UserDao userDao; 

    public boolean isUserExists(LoginBean loginBean){ 
     User user = userDao.findUser(loginBean); 
     return user != null ? true: false; 
    } 

    public User findUser(LoginBean loginBean){ 
     User user = userDao.findUser(loginBean); 
     return user; 
    } 

ステップ6 DAOクラス

package com.ims.dao; 
import java.util.List; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

import com.ims.bean.LoginBean; 
import com.ims.entity.User; 

@Repository 
public class UserDaoImpl implements UserDao { 

    @Autowired 
    private SessionFactory sessionFactory; 

    @SuppressWarnings("unchecked") 
    public User findUser(LoginBean loginBean){ 
     User user = null; 
     Session session = sessionFactory.openSession(); 
     List<User> users = session.createQuery("from User where email = '"+loginBean.getEmail()+"' and password='"+loginBean.getPassword()+"'").list(); 
     if(users != null){ 
      user = users.get(0); 
     } 
     session.close(); 
     return user; 
    } 

ステップ7 hibermate構成ファイル

<hibernate-configuration> 
    <session-factory> 
    <!-- We're using MySQL database so the dialect needs to MySQL as well--> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

    </session-factory> 
</hibernate-configuration> 

ステップ8:ApplicationContextのファイルステップ9

<tx:annotation-driven /> 

    <bean id="messageSource" 
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:messages" /> 
     <property name="defaultEncoding" value="UTF-8" /> 
    </bean> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
     p:driverClassName="com.mysql.jdbc.Driver" 
     p:url="jdbc:mysql://localhost:3306/agiledb" p:username="root" 
     p:password="admin" 
     p:initialSize="5" 
     p:maxActive="10" 
     p:maxWait="-1" 
     p:testOnBorrow="true" 
     p:validationQuery="select 1 as dbcp_connection_test"   
     p:minIdle="1" /> 

     <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
     p:dataSource-ref="dataSource" p:configLocation="/WEB-INF/hibernate.cfg.xml" 
     p:packagesToScan="com.ims.entity" /> 

     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
      <property name="sessionFactory" ref="sessionFactory" /> 
     </bean> 

</beans> 

:プロジェクト名 - あなたの検索条件リストは、()の復帰であるため、servlet.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' --> 
     <property name="prefix" value="/WEB-INF/view/"/> 
     <property name="suffix" value=".jsp"/> 
</bean> 
+1

テーブルに 'userId'フィールドを作成していません。 – anthony

+0

エラーメッセージは、問題の内容を示します。 "不明な列...ユーザーID"。 – rghome

+0

ERROR [io.undertow.request](デフォルトタスク-10)UT005023:/ agile /への例外処理要求:org.springframework.web.util.NestedServletException:リクエスト処理に失敗しました。ネストされた例外はjava.lang.IndexOutOfBoundsExceptionです。インデックス:0、サイズ:0 – Mahesh

答えて

0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 

例外が発生しています空のユーザーの一覧を表示し、0のインデックスにアクセスしようとしています。

関連する問題