【问题标题】:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'user0_.userId' in 'field list'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的未知列“user0_.userId”
【发布时间】:2018-05-12 08:09:08
【问题描述】:

我收到错误:

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

请检查代码并回复我。

第一步:创建数据库表

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

示例:

email = mahesh@gmail.com
pwd = 1234

第二步:实体类

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

第四步:控制器类

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

第六步道类

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 步:应用程序上下文文件

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

【问题讨论】:

  • 您没有在表中创建userId 字段。
  • 错误信息告诉你问题出在哪里。 “未知列 ... userId”。
  • 错误 [io.undertow.request](默认任务 10) UT005023:对 /agile/ 的异常处理请求:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

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


【解决方案1】:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

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

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 2017-06-27
    • 2016-10-07
    • 2014-07-19
    • 2018-05-15
    • 2012-10-13
    • 1970-01-01
    • 2015-03-04
    • 2015-05-28
    相关资源
    最近更新 更多