SPRING4/SPRING-MVC/HIBERNATE4 Integration III based on Eclipse MAVEN: the application of hibernate4 generic DAO __hibernate

Source: Internet
Author: User
Tags aop

First, the AOP proxy is set to Cglib dynamic Proxy, and in Spring-hibernate.xml, add:

<aop:aspectj-autoproxy  proxy-target-class= "true"/>

It is generally desirable to define DAO base classes as generics, with generic interfaces, to use interface references in Controller/service, and to configure the truly implemented generic DAO classes in XML.

1. Define the DAO implementation class and its interfaces that support generics:

public class Hibernateentitydao<t> extends Hibernategenericdao implements
Ihibernateentitydao<t> ...

2, for example, now to do entity for Tauser DAO implementation class, very simple: to specify the type of entity can be:

public class Hibernateuserdao extends hibernateentitydao<tauser> {	
}

3, the individual tends to DAO in the spring XML configuration without @repository annotations, because later to change the logic of what, directly in the XML will implement the class point to the new class. This is configured inside the Spring-dao.xml:

<bean id= "Hibernateuserdao" class= "Com.freestyle.test.hibernate.dao.HibernateUserDao"/>


4, in the controller inside the test, the following code, the @resource way to inject XML inside the configured Hibernateuserdao to interface Mvdao:

Package Com.freestyle.test.hibernate.controller;
Import java.sql.SQLException;
Import Java.util.HashMap;

Import Java.util.Map;
Import Javax.annotation.Resource;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.context.annotation.DependsOn;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;

Import Org.springframework.web.servlet.ModelAndView;
Import Com.freestyle.common.hibernate.dao.interfaces.IHibernateEntityDao;


Import Com.freestyle.test.hibernate.entities.TaUser; @Controller @RequestMapping ("/hibernate") @DependsOn ({"Hibernateuserdao"}) public class Hibernatecontroller Implements org.springframework.web.servlet.mvc.controller{//@Resource (name= "Hibernateuserdao")//private
	Hibernateuserdao Lvdao; @Resource (name= "Hibernateuserdao") @Resource (name= "Hibernateuserdao") protected IhibErnateentitydao<tauser> Mvdao; 
			@RequestMapping ("/test") public Modelandview test (@RequestParam (value = "Name", DefaultValue = "World") String name)
		Throws SQLException {System.out.println ("Hello" + name);
		Map<string, object> lvmap= new hashmap<string, object> ();
		/*tauser lvuser= lvdao.getuser ("DGMISLRH");
		if (lvuser!=null) {System.out.println (Lvuser.getfaname ());
		}*/tauser Lvuser=mvdao.get (name);
			if (lvuser!=null) {System.out.println (Lvuser.getfaname ());
		Lvmap.put ("UserName", Lvuser.getfaname ());
		
	Return to New Modelandview ("/hello", Lvmap); Public Modelandview HandleRequest (httpservletrequest request, httpservletresponse response) throws Exception {Sy
		Stem.out.println (Request.getparametermap (). toString ());
	return null; }

}

Start Tomcat debugging, enter HTTP://LOCALHOST/GPCHAT/HIBERNATE/TEST?NAME=DGMISLRH on the browser, and display the log on the console:

Hibernate:select Tauser0_.fa_login as fa_login1_0_0_, tauser0_.fa_email as fa_email2_0_0_, t Auser0_.fa_line as fa_line3_0_0_, tauser0_.fa_manage_by as fa_manag4_0_0_, tauser0_.fa_name as fa_name5_0_ 0_, tauser0_.fa_passwd as fa_passw6_0_0_, tauser0_.fa_plant as fa_plant7_0_0_, Tauser0_.fa_remark As fa_remar8_0_0_, tauser0_.fa_role as fa_role9_0_0_, tauser0_.fa_status as fa_stat10_0_0_, Tauser
0_.fa_type as fa_type11_0_0_ from Ta_user tauser0_ where tauser0_.fa_login=?
17:04:29,606 TRACE basicbinder:81-binding parameter [1] as [VARCHAR]-[DGMISLRH] tauser entity. 17:04:29,611 TRACE basicextractor:78-extracted value ([fa_email2_0_0_]: [VARCHAR])-[runhui.li@alco.com.hk] 17:04:29, 611 Trace basicextractor:78-extracted value ([fa_line3_0_0_]: [VARCHAR])-[*] 17:04:29,611 trace basicextractor:68-e xtracted value ([fa_manag4_0_0_]: [VARCHAR])-[NULL] 17:04:29,611 Trace basicextractor:78-extracted value ([fa_name5_0_0_]: [VARCHAR])-[Chen Daven] 17:04:29,612 trace Basicextractor : 78-extracted value ([fa_passw6_0_0_]: [VARCHAR])-[389468f83cb96feafa750a8e5bae5f731a59f031] 17:04:29,612 TRACE Basi cextractor:78-extracted value ([fa_plant7_0_0_]: [VARCHAR])-[DG] 17:04:29,612 TRACE basicextractor:78-extracted val UE ([fa_remar8_0_0_]: [VARCHAR])-[] 17:04:29,612 TRACE basicextractor:78-extracted value ([fa_role9_0_0_]: [VARCHAR] )-[NA] 17:04:29,612 trace basicextractor:78-extracted value ([fa_stat10_0_0_]: [VARCHAR])-[A] 17:04:29,612 trace Ba sicextractor:78-extracted value ([fa_type11_0_0_]: [VARCHAR])-[A] Chen Daven


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.