java - How do I integrate my Spring project with Hibernate -



java - How do I integrate my Spring project with Hibernate -

i have done little employee project using springs. project helps add,edit,delete employee in database. had used normal jdbc database connection. want integrate hibernate project. please help me. m learning hibernate , trying out of it.

this employee controller.

bundle com.employee.controller; import com.employee.dao.employeedao; import com.employee.model.employee; @controller public class employeecontroller { @autowired employeedao employeedao; @requestmapping(value = "/employee", method = requestmethod.get) public modelandview employee() { homecoming new modelandview("registration", "command", new employee()); } @requestmapping(value = "/addemployee", method = requestmethod.post) public string addemployee(@modelattribute employee employee/*, modelmap model*/) { seek { employeedao.create(employee.getfirstname(), employee.getemailid(), employee.getage()); homecoming "1"; }catch(exception e) { homecoming "2"; } } @requestmapping("/viewemployeedetails") public string fetchemployeedetails(@modelattribute employee employee,modelmap map,httpsession session) throws sqlexception { system.out.println("inside fetchemployye details"); employee e=new employee(); arraylist<employee> xx=new arraylist<employee>(); xx=employeedao.listemployee(); system.out.println("prinitgn list size..!!! "+xx.size()); map.addattribute("empdetails", xx); session.setattribute("empdetails", xx); homecoming "viewnew"; } @requestmapping("/edituserdetails") public string edituserdetails(@modelattribute/*("usereditdetails")*/ employee employee,modelmap map,httpsession session,httpservletrequest req) { system.out.println("inside edituserdetails"); string id=req.getparameter("empid"); int val=integer.parseint(id); system.out.println("employee id is..!! "+id); arraylist<employee> xx=new arraylist<employee>(); xx=(arraylist<employee>) session.getattribute("empdetails"); arraylist<employee> newxx=new arraylist<employee>(); employee e1=new employee(); (int = 0; < xx.size(); i++) { if(val==xx.get(i).getempid()) { e1.setage(xx.get(i).getage()); e1.setemailid(xx.get(i).getemailid()); e1.setempid(xx.get(i).getempid()); e1.setfirstname(xx.get(i).getfirstname()); newxx.add(e1); } } system.out.println("hhhh:"+e1.getfirstname()); map.put("usereditdetails", e1); // map.put("yyy",newxx); homecoming "edit"; } @requestmapping(value = "/view", method = requestmethod.get) public modelandview view() { homecoming new modelandview("view", "command", new employee()); } @requestmapping(value="/viewemployee") public string viewemployee() { homecoming "viewnew"; } @requestmapping(value = "/edit", method = requestmethod.get) public modelandview edit() { homecoming new modelandview("edit", "command", new employee()); } @requestmapping(value="/editemployee" ,method=requestmethod.post) public string editemployee(@modelattribute employee employee/*@requestparam int id, @requestparam string firstname, @requestparam string emailid, @requestparam int age*/){ seek { int id = employee.getempid(); string firstname = employee.getfirstname(); string emailid = employee.getemailid(); int age = employee.getage(); /*int id=1; string firstname="nitesh"; string emailid="nitesh"; int age=25;*/ string check= employeedao.update(id, firstname, emailid, age); if (check=="1") { homecoming "1"; }else { homecoming "2"; } }catch(exception e) { homecoming "2"; } } @requestmapping(value = "/delete", method = requestmethod.get) public modelandview delete() { homecoming new modelandview("delete", "command", new employee()); } @requestmapping(value="/deleteemployee") public string deleteemployee(@modelattribute employee employee) { seek { int id = employee.getempid(); string check =employeedao.delete(id); if (check=="1") { homecoming "1"; } }catch(exception e) { homecoming "2"; } homecoming "2"; } }

this dao impl

package com.employee.dao.impl; import com.employee.dao.employeedao; import com.employee.mapping.employeemapper; import com.employee.model.employee; import com.sun.org.apache.bcel.internal.generic.select; public class employeedaoimpl implements employeedao{ private datasource datesource; private jdbctemplate jdbctemplateobject; @override public void setdatasource(datasource datasource) { this.datesource = datasource; this.jdbctemplateobject = new jdbctemplate(datasource); } public void create(string firstname, string emailid, integer age) { string sql = "insert employee (firstname, emailid, age) values (?, ?, ?)"; jdbctemplateobject.update(sql,firstname,emailid,age); system.out.println("created record name = " + firstname + " age = " + age); return; } @override public employee getemployee(integer id) throws sqlexception { system.out.println("creating statement..."); employee emp = new employee(); connection con = datesource.getconnection(); system.out.println("connection done"); string sql = "select * employee empid= "+id; statement stmt = con.createstatement(); resultset rs = stmt.executequery(sql); while(rs.next()){ //retrieve column name emp.setempid(rs.getint("empid")); emp.setfirstname(rs.getstring("firstname")); emp.setemailid(rs.getstring("emailid")); emp.setage(rs.getint("age")); homecoming emp; } rs.close(); con.close(); homecoming null; } @override public arraylist<employee> listemployee() throws sqlexception { system.out.println("creating statement..."); list<employee> emp = new arraylist<employee>(); employee emp1 =null; connection con = datesource.getconnection(); system.out.println("connection done"); string sql = "select * employee"; statement stmt = con.createstatement(); //resultset rs = stmt.execute(sql); resultset rs = stmt.executequery(sql); // resultset rs=stmt.getresultset(); system.out.println("xxxxxxxxx :"+rs.tostring()); while(rs.next()){ //retrieve column name emp1=new employee(); system.out.println("getting values in result set..!!!"); emp1.setempid(rs.getint("empid")); emp1.setfirstname(rs.getstring("firstname")); emp1.setemailid(rs.getstring("emailid")); emp1.setage(rs.getint("age")); emp.add(emp1); } rs.close(); con.close(); homecoming (arraylist<employee>) emp; } @override public string delete(integer id) throws sqlexception { /*string sql = "delete employee id = ?"; jdbctemplateobject.update(sql, id); system.out.println("deleted record id = " + id ); return;*/ employee emp = new employee(); system.out.println("deleting connetion "); connection con = datesource.getconnection(); system.out.println("connection establised"); statement stmt = con.createstatement(); string sql1 = "select * employee empid="+id; resultset rs = stmt.executequery(sql1); while(rs.next()) { emp.setempid(rs.getint("empid")); } if(emp.getempid() == 0) { homecoming "2"; }else { string sql = "delete employee empid="+id; stmt.executeupdate(sql); system.out.println("deleted"); homecoming "1"; } } @override public string update(integer id, string firstname, string emailid, integer age) throws sqlexception { /* string sql = "update employee set age = ? set firstname= ? setemailid=? id = ?"; jdbctemplateobject.update(sql, firstname,emailid,age, id); system.out.println("updated record id = " + id ); return;*/ employee emp = new employee(); system.out.println("editing connection "); connection con = datesource.getconnection(); system.out.println("connection establised"); statement stmt = con.createstatement(); string sql1 = "select * employee empid="+id; resultset rs = stmt.executequery(sql1); while(rs.next()) { emp.setempid(rs.getint("empid")); } if(emp.getempid()==0) { homecoming "2"; }else { string sql = "update employee set firstname='"+firstname+"', emailid = '"+emailid +"', age ='"+age+"' empid="+id; stmt.executeupdate(sql); system.out.println("updated"); homecoming "1"; } } /*public employee check(integer id) throws sqlexception { system.out.println("creating statement..."); employee emp = new employee(); connection con = datesource.getconnection(); system.out.println("connection done"); string sql = "select empid employee empid= "+id; statement stmt = con.createstatement(); resultset rs = stmt.executequery(sql); while(rs.next()){ //retrieve column name emp.setempid(rs.getint("empid")); homecoming emp; } homecoming emp; } */ @override public employee check(integer id) throws sqlexception { // todo auto-generated method stub homecoming null; } @override public employee fetchemployeedetails() { // todo auto-generated method stub homecoming null; } }

this employee mapper

package com.employee.mapping; import com.employee.model.employee; public class employeemapper implements rowmapper<employee> { public employee maprow(resultset rs, int rownum) throws sqlexception { employee emp = new employee(); emp.setempid( rs.getint("id")); emp.setfirstname(rs.getstring("name")); emp.setage(rs.getint("age")); /* emp.setemailid(rs.getstring("emailid"));*/ homecoming emp; } } **this spring-servlet.xml** <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:c="http://www.springframework.org/schema/c" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemalocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="com.employee.controller" /> <context:annotation-config/> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:annotation-driven></mvc:annotation-driven> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.microsoft.sqlserver.jdbc.sqlserverdriver"/> <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databasename=employee"/> <property name="username" value="sa"/> <property name="password" value="r@1234"/> </bean> <bean id="employeedaoimpl" class="com.employee.dao.impl.employeedaoimpl"> <property name="datasource" ref="datasource" /> </bean> <!-- <jee:jndi-lookup jndi-name="java:/employee" id="employee"></jee:jndi-lookup> --> </beans>

this hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- database connection settings --> <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.sqlserverdriver</property> <property name="connection.url">jdbc:sqlserver://localhost:1433;databasename=employee</property> <property name="connection.username">sa</property> <property name="connection.password">r@1234</property> <!-- jdbc connection pool (use built-in) --> <property name="connection.pool_size">1</property> <!-- sql dialect --> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <!-- disable second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.nocacheprovider</property> <!-- echo executed sql stdout --> <property name="show_sql">true</property> <!-- drop , re-create database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- names annotated entity class --> <mapping class="com.employee.model.employee"/> </session-factory> </hibernate-configuration>

please help me integrate project hibernate

define datasource & hibernate sessionfactory in spring application context file. to integrate hibernate application dao class - can either utilize 'hibernatetemplate' or straight inject hibernate sesisonfactory in dao class.

refer - http://www.javabench.in/2012/04/spring-hibernate-integration.html

java spring hibernate spring-mvc

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -