com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'user0_.userId'

时间:2017-11-28 18:48:52

标签: mysql spring-mvc jboss hibernate-mapping spring-jdbc

我收到错误:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'user0_.userId'

请检查代码并回复我。

第1步:创建数据库表

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

示例:

email = mahesh@gmail.com
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 class

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文件

<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>

第9步:项目名称 - 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 个答案:

答案 0 :(得分:0)

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

发生异常是因为您的查询条件list()返回空用户列表,并且您尝试访问0索引。