博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
idea开发工具springmvc加vue.js实现MySQL数据库的查询操作
阅读量:7016 次
发布时间:2019-06-28

本文共 6937 字,大约阅读时间需要 23 分钟。

项目目录:

 

 User.java:

package com.nf.entity;import javax.persistence.*;@Entity@Table(name = "user", schema = "lib", catalog = "")public class User {    private int id;    private String name;    private String address;    private String phone;    public User(){}    public User(int id, String name, String address, String phone) {        this.id = id;        this.name = name;        this.address = address;        this.phone = phone;    }    @Id    @Column(name = "id")    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    @Basic    @Column(name = "name")    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Basic    @Column(name = "address")    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    @Basic    @Column(name = "phone")    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    @Override    public boolean equals(Object o) {        if (this == o) return true;        if (o == null || getClass() != o.getClass()) return false;        User that = (User) o;        if (id != that.id) return false;        if (name != null ? !name.equals(that.name) : that.name != null) return false;        if (address != null ? !address.equals(that.address) : that.address != null) return false;        if (phone != null ? !phone.equals(that.phone) : that.phone != null) return false;        return true;    }    @Override    public int hashCode() {        int result = id;        result = 31 * result + (name != null ? name.hashCode() : 0);        result = 31 * result + (address != null ? address.hashCode() : 0);        result = 31 * result + (phone != null ? phone.hashCode() : 0);        return result;    }}

UserDao.java

package com.nf.dao;import com.nf.entity.User;import java.util.List;public interface UserDao {    public List
getAllUsers() ;}

UserDaoImpl.java

package com.nf.dao;import com.nf.entity.User;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.query.Query;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import java.util.List;@Repositorypublic class UserDaoImpl implements UserDao {    @Autowired    private SessionFactory sessionFactory;    public List
getAllUsers() { Session session = sessionFactory.getCurrentSession(); Query
query = session.createQuery("from User", User.class); return query.getResultList(); }}

UserService.java

package com.nf.service;import com.nf.entity.User;import java.util.List;public interface UserService {    public List
getAllUsers() ;}

UserServiceImpl.java

package com.nf.service;import com.nf.dao.UserDao;import com.nf.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import javax.transaction.Transactional;import java.util.List;@Servicepublic class UserServiceImpl implements UserService {    @Autowired    private UserDao userDao;    @Transactional    public List
getAllUsers() { return userDao.getAllUsers(); }}

UserAction.java

package com.nf.action;import com.nf.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap;import java.util.Map;@Controller@RequestMapping("/user")public class UserAction {    @Autowired    private UserService userService ;    @RequestMapping("/query")    public @ResponseBody    Map
query(){ Map
hashMap = new HashMap
(); hashMap.put("userList",userService.getAllUsers()); return hashMap; }}

applicationContext.xml;

org.hibernate.dialect.MySQL57InnoDBDialect
true
true
false

spring-servlet.xml

jdbc.properties

driverClass=com.mysql.cj.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/lib?serverTimezone=UTCuser=rootpassword=123456

web.xml

OpenSessionInViewFilter
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
flushMode
AUTO
OpenSessionInViewFilter
/*
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-servlet.xml
1
spring
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener

qianduan,.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
显示所有的新闻信息
{
{art.id}}
{
{art.name}}
{
{art.address}}
{
{art.phone}}

结果:

 

转载于:https://www.cnblogs.com/aa1314/p/8276245.html

你可能感兴趣的文章