在本节中,我们将创建一个search field web应用程序。该应用程序包括具有搜索字段的表格形式的数据。在这种集成中,我们使用spring处理后端部分,使用angular处理前端部分。
一旦我们在服务器上部署了应用程序,就会生成一个表单,其中包含表格形式的数据以及一些搜索字段。 现在,我们可以从这些字段中搜索表中存在的数据。在这里,我们使用两个搜索字段-名称和电子邮件id。 要搜索数据,需要在任何搜索字段中提供完整的关键字。
使用任何ide来开发spring和hibernate项目。可能是myeclipse/eclipse/netbeans。在这里,我们正在使用eclipse。 用于数据库的mysql。 使用任何ide来开发angular项目。它可能是visual studio代码/sublime。在这里,我们正在使用visual studio code。 服务器: apache tomcat/jboss/glassfish/weblogic/websphere。
在这里,我们正在使用以下技术:
spring5 休眠5 角度6 mysql
让我们创建数据库 searchfieldexample 。由于hibernate自动创建了表,因此无需创建表。在这里,我们需要在表中显式提供数据,以便它们可以出现在屏幕上以执行搜索操作。但是,我们也可以从下载链接中存在的文件中导入数据。
让我们看看我们需要遵循的spring目录结构:
要开发搜索字段应用程序,请按照请按以下步骤操作: -
将依赖项添加到pom.xml文件。
pom.xml
4.0.0 com.nhooo searchfieldexample war 0.0.1-snapshot searchfieldexample maven webapp http://maven.apache.org 5.0.6.release 5.2.16.final 5.1.45 0.9.5.2 1.8 1.8 org.springframework spring-webmvc ${springframework.version} org.springframework spring-tx ${springframework.version} org.springframework spring-orm ${springframework.version} com.fasterxml.jackson.core jackson-databind 2.9.5 org.hibernate hibernate-core ${hibernate.version} mysql mysql-connector-java ${mysql.connector.version} com.mchange c3p0 ${c3po.version} javax.servlet javax.servlet-api 3.1.0 javax.servlet.jsp javax.servlet.jsp-api 2.3.1 javax.servlet jstl 1.2 javax.xml.bind jaxb-api 2.3.0 junit junit 3.8.1 test searchfieldexample
创建配置类
我们执行基于注释的配置,而不是xml。因此,我们创建两个类并在其中指定所需的配置。
demoappconfig.java
package com.nhooo.searchfieldexample.config; import java.beans.propertyvetoexception; import java.util.properties; import javax.sql.datasource; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.propertysource; import org.springframework.core.env.environment; import org.springframework.orm.hibernate5.hibernatetransactionmanager; import org.springframework.orm.hibernate5.localsessionfactorybean; import org.springframework.transaction.annotation.enabletransactionmanagement; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; import com.mchange.v2.c3p0.combopooleddatasource; @configuration @enablewebmvc @enabletransactionmanagement @componentscan("com.nhooo.searchfieldexample") @propertysource(value = { "classpath:persistence-mysql.properties" }) @propertysource(value = { "classpath:persistence-mysql.properties" }) @propertysource(value = { "classpath:application.properties" }) public class demoappconfig implements webmvcconfigurer { @autowired private environment env; @bean public datasource mydatasource() { // create connection pool combopooleddatasource mydatasource = new combopooleddatasource(); // set the jdbc driver try { mydatasource.setdriverclass("com.mysql.jdbc.driver"); } catch (propertyvetoexception exc) { throw new runtimeexception(exc); } // set database connection props mydatasource.setjdbc); mydatasource.setuser(env.getproperty("jdbc.user")); mydatasource.setpassword(env.getproperty("jdbc.password")); // set connection pool props mydatasource.setinitialpoolsize(getintproperty("connection.pool.initialpoolsize")); mydatasource.setminpoolsize(getintproperty("connection.pool.minpoolsize")); mydatasource.setmaxpoolsize(getintproperty("connection.pool.maxpoolsize")); mydatasource.setmaxidletime(getintproperty("connection.pool.maxidletime")); return mydatasource; } private properties gethibernateproperties() { // set hibernate properties properties props = new properties(); props.setproperty("hibernate.dialect", env.getproperty("hibernate.dialect")); props.setproperty("hibernate.show_sql", env.getproperty("hibernate.show_sql")); props.setproperty("hibernate.format_sql", env.getproperty("hibernate.format_sql")); props.setproperty("hibernate.hbm2ddl.auto", env.getproperty("hibernate.hbm2ddl")); return props; } // need a helper method // read environment property and convert to int private int getintproperty(string propname) { string propval = env.getproperty(propname); // now convert to int int intpropval = integer.parseint(propval); return intpropval; } @bean public localsessionfactorybean sessionfactory(){ // create session factorys localsessionfactorybean sessionfactory = new localsessionfactorybean(); // set the properties sessionfactory.setdatasource(mydatasource()); sessionfactory.setpackagestoscan(env.getproperty("hibernate.packagestoscan")); sessionfactory.sethibernateproperties(gethibernateproperties()); return sessionfactory; } @bean @autowired public hibernatetransactionmanager transactionmanager(sessionfactory sessionfactory) { // setup transaction manager based on session factory hibernatetransactionmanager txmanager = new hibernatetransactionmanager(); txmanager.setsessionfactory(sessionfactory); return txmanager; } }
myspringmvcdispatcherservletinitializer.java
package com.nhooo.searchfieldexample.config; import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer; public class myspringmvcdispatcherservletinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class[] getrootconfigclasses() { // todo auto-generated method stub return null; } @override protected class[] getservletconfigclasses() { return new class[] { demoappconfig.class }; } @override protected string[] getservletmappings() { return new string[] { "/" }; } }
创建实体类
在这里,我们将创建一个entity/pojo(普通的旧java对象)类。
user.java
package com.nhooo.searchfieldexample.entity; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.table; @entity @table(name="user") public class user { @id @generatedvalue(strategy=generationtype.auto) @column(name="userid") private int userid; @column(name="name") private string name; @column(name="email_id" ) public string emailid; @column(name="qualification") public string qualification; public user() {} public user(int userid, string name, string emailid, string qualification) { super(); this.userid = userid; this.name = name; this.emailid = emailid; this.qualification = qualification; } public int getuserid() { return userid; } public void setuserid(int userid) { this.userid = userid; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getemailid() { return emailid; } public void setemailid(string emailid) { this.emailid = emailid; } public string getqualification() { return qualification; } public void setqualification(string qualification) { this.qualification = qualification; } @override public string tostring() { return "user [userid=" userid ", name=" name ", emailid=" emailid ", qualification=" qualification "]"; } }
创建dao界面
在这里,我们正在创建dao界面以执行与数据库相关的操作。
userdao.java
package com.nhooo.searchfieldexample.dao.interfaces; import java.util.list; import com.nhooo.searchfieldexample.entity.user; public interface userdao { public int saveuser(user user); public listgetfiltereddata(user user); }
创建dao接口实现类
userdaoimpl.java
package com.nhooo.searchfieldexample.dao.implementation; import java.util.arraylist; import java.util.list; 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 com.nhooo.searchfieldexample.dao.interfaces.userdao; import com.nhooo.searchfieldexample.entity.user; @repository("userdao") public class userdaoimpl implements userdao { @autowired sessionfactory sessionfactory; public int saveuser(user user) { session session = null; try { session = sessionfactory.getcurrentsession(); int userid = (integer) session.save(user); return userid; } catch(exception exception) { system.out.println("excption while saving data into db " exception); return 0; } finally { session.flush(); } } public listgetfiltereddata(user user) { session session = null; try { session = sessionfactory.getcurrentsession(); arraylist