如何使用电子邮件地址从数据库获取单个记录

时间:2019-04-10 04:18:58

标签: spring hibernate

我们在数据库中有一个注册表单数据,现在我必须获得与单记录相关的电子邮件,该电子邮件已在另一个登录jsp页面POJO类中输入。

我开发了用于注册表格的应用程序,并将他的数据存储到数据库中。所以我正在创建波纹管类。

此处介绍如何在Dao中实现代码开发getDataByUsingEmail方法来从数据库中获取单个记录。

这里还有另一个pojo课...所以我要从那里读取电子邮件地址。

控制器类:

 package com.spring.form;

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;

    import com.spring.model.FormData;
    import com.spring.model.LoginData;
    import com.spring.services.FormDataService;

    import javax.servlet.http.HttpServletResponse;


    @Controller
    public class FrontController
    {
        @Autowired
        private FormDataService formDataService;


        HashMap<String, String> map=new HashMap<String, String>();


        @RequestMapping(value="/loginAndGetData",method=RequestMethod.POST)
        public String getFormDataByEmail(Model model,LoginData loginData) 
        {
            String emailId=loginData.getLoginEmail();

            System.out.println("Email entered is : "+loginData.getLoginEmail());

            model.addAttribute("formData", new FormData());
            model.addAttribute("listFormData", formDataService.getFormDataByEmail(emailId));

            return "result";

        }
}

模型类:

package com.spring.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="mdm_registration_form", schema="MDMS")
public class FormData 
{
    @Id
    @Column(name="email")
    private String email;

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

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

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

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

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

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

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

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

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

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

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

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

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



    public FormData(){

    }

    public String getFname() {
        return Fname;
    }
    public void setFname(String fname) {
        Fname = fname;
    }

    public String getLname() {
        return Lname;
    }

    public void setLname(String lname) {
        Lname = lname;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getGend() {
        return gend;
    }

    public void setGend(String gend) {
        this.gend = gend;
    }

    public String getMarry() {
        return marry;
    }

    public void setMarry(String marry) {
        this.marry = marry;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return email;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    public String getAddid() {
        return addid;
    }

    public void setAddid(String addid) {
        this.addid = addid;
    }

    public String getIdupload() {
        return idupload;
    }

    public void setIdupload(String idupload) {
        this.idupload = idupload;
    }

    public String getAdupload() {
        return adupload;
    }

    public void setAdupload(String adupload) {
        this.adupload = adupload;
    }

    public String getPassword() {
        return password;
    }

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


}

Dao实施:

package com.spring.dao;

import java.util.List;

import javax.persistence.Query;

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

import com.spring.model.FormData;
import com.spring.model.LoginData;


@Repository

public class FormDaoImpl implements FormDao
{

    @Autowired
    private SessionFactory sessionFactory;



    @Override
    public FormData getFormDataByEmail(String emailId) 
    {
        /*
        FormData formData= null;
        Session session = this.sessionFactory.getCurrentSession();
        Query query= null;
        String passedquery="select * from MDMS.mdm_registration_form where email='"+emailId+"'";
        formData= session.get(FormData.class, emailId);
        return formData; */

        Session session = this.sessionFactory.getCurrentSession();

        String hql = "FROM FormData formData WHERE formData.email ="+emailId;

        Query query = session.createQuery(hql);

        //List results = query;

        return formData;
    }

服务实施

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.dao.FormDao;
import com.spring.model.FormData;


@Service
public class FormDataServiceImpl implements FormDataService
{
    @Autowired
    private FormDao formDao;


    @Override
    @Transactional
    public FormData getFormDataByEmail(String emailId)
    {

        return formDao.getFormDataByEmail(emailId);
    }
}

LoginData Pojo类:

package com.spring.model;

public class LoginData {

    private String loginEmail;
    private String loginPassword;

    public LoginData()
    {

    }

    public String getLoginEmail() {
        return loginEmail;
    }

    public void setLoginEmail(String loginEmail) {
        this.loginEmail = loginEmail;
    }

    public String getLoginPassword() {
        return loginPassword;
    }

    public void setLoginPassword(String loginPassword) {
        this.loginPassword = loginPassword;
    }


}

请提出建议, 预先感谢

0 个答案:

没有答案