利用idea开发工具实现ssh(spring+struts+hibernate)加vue.js前后台对数据库的查询
项目目录:
entity: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页面:
package com.nf.dao; import com.nf.entity.User; import java.util.List; public interface UserDao { public List<User> getAllUsers() ; }
UserDaoImpl页面:
package com.nf.dao; import com.nf.entity.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; import javax.persistence.Query; import javax.transaction.Transactional; import java.util.List; @Repository @Scope("prototype") @Transactional// 在Spring中@Transactional提供一种控制事务管理的快捷手段 public class UserDaoImpl implements UserDao { @Autowired private SessionFactory sessionFactory ; public List<User> getAllUsers() { Session session = sessionFactory.getCurrentSession() ; Query q = session.createQuery("from User",User.class) ; List<User> list = q.getResultList() ; return list; } }
UserServiceDao页面:
package com.nf.service; import com.nf.entity.User; import java.util.List; public interface UserServiceDao { public List<User> getAllUsers() ; }
UserServiceImpl页面:
package com.nf.service; import com.nf.dao.UserDao; import com.nf.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import java.util.List; @Service @Scope("prototype") public class UserServiceImpl implements UserServiceDao { @Autowired private UserDao userDao ; public List<User> getAllUsers() { return userDao.getAllUsers(); } }
UserAction页面:
package com.nf.controller; import com.nf.entity.User; import com.nf.service.UserServiceDao; import com.opensymphony.xwork2.ActionSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import java.util.HashMap; import java.util.List; @Controller @Scope("prototype") public class UserAction extends ActionSupport{ private HashMap<String,Object> myJson = new HashMap<String, Object>() ; public HashMap<String, Object> getMyJson() { return myJson; } public void setMyJson(HashMap<String, Object> myJson) { this.myJson = myJson; } @Autowired private UserServiceDao userServiceDao ; public String getAllUsers_json(){ List<User> list = userServiceDao.getAllUsers() ; myJson.put("list",list) ; return this.SUCCESS ; } }
applicationContext.xml页面:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <context:component-scan base-package="com.nf"></context:component-scan> <!--加载JDBC的配置文件--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!--几个个性化的信息--> <!--每300秒检查所有连接池中空闲的连接--> <property name="idleConnectionTestPeriod" value="300"></property> <!--最大的空闲时间--> <property name="maxIdleTime" value="2000"></property> <!--最大连接数--> <property name="maxPoolSize" value="5"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!--1.数据库连接池--> <property name="dataSource" ref="myDataSource"></property> <!--2.相关hibernate的配置信息--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL57InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.connection.autocommit">false</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop>--> </props> </property> <!--3.实体类映射关系--> <property name="packagesToScan" value="com.nf"></property> </bean> <!--事务管理器配置,Hibernate单数据源事务--> <bean id="defaultTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--使用注解annotation定义事务--> <tx:annotation-driven transaction-manager="defaultTransactionManager" ></tx:annotation-driven> </beans>
jdbc.properties连接数据库文件:
driverClass=com.mysql.cj.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/lib?serverTimezone=UTC user=root password=
struts.xml代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.objectFactory" value="spring"></constant> <package name="mypackage" extends="json-default"> <!-- <action name="shownews" class="artAction" method="getAllNews"> <result name="success">ok.jsp</result> </action>--> <action name="shownews_json" class="com.nf.controller.UserAction" method="getAllUsers_json"> <result type="json"> <param name="root">myJson</param> </result> </action> </package> </struts>
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Archetype Created Web Application</display-name> <!--2个struts的过滤器--> <filter> <filter-name>struts-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class> </filter> <filter> <filter-name>struts-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--1个spring的监听器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
index.jsp默认页面:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/1/9 Time: 15:23 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> hello mvc </body> </html>
qianduan.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hardDream</title> <body> <div id="app"> <div v-for="user in list"> <span>{{user.id}}</span> <span>{{user.name}}</span> <span>{{user.address}}</span> <span>{{user.phone}}</span> <br><br><br><br> </div> </div> <script src="js/vue.js"></script>//导入vue.js文件 <script src="js/jquery-3.2.1.min.js"></script>//导入jQuery文件 <script language="JavaScript"> var model = {"list":[]} ; var vm = new Vue({ el:'#app', data:model }) ; $.ajax({ url:'shownews_json', type:'GET', //data:clientInput, dataType:'json', timeout:3000, success:function(result){ model.list = result.list; }, error:function(XMLHttpRequest, textStatus, errorThrown){ alert('服务器忙,请不要说脏话,理论上大家都是文明人'); alert(textStatus+XMLHttpRequest.status); } }) ; </script> </body> </html>
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nf</groupId> <artifactId>ancheng</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ancheng Maven Web</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-json-plugin --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.5.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.5.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.12.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl --> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <webResources> <resource> <directory>Web</directory> </resource> </webResources> </configuration> </plugin> </plugins> <finalName>ancheng</finalName> </build> </project>
hardDream!!