Home Database Mysql Tutorial SSH2 DAO注入sessionfactory的方式汇总

SSH2 DAO注入sessionfactory的方式汇总

Jun 07, 2016 pm 03:00 PM
dao Way injection

方法一:在spring配置文件中,为继承HibernateDaoSupport的DAO注入sessionFactory Spring为Hibernate的DAO提供工具类:HibernateDaoSupport。该类主要提供如下两个方法,方便DAO的实现: public final HibernateTemplate getHibernateTemplate() public fina

方法一:在spring配置文件中,为继承HibernateDaoSupport的DAO注入sessionFactory

Spring为Hibernate的DAO提供工具类:HibernateDaoSupport。该类主要提供如下两个方法,方便DAO的实现:    public final HibernateTemplate getHibernateTemplate()

 public final void setSessionFactory(SessionFactory sessionFactory)

其中,setSessionFactory方法用来接收Spring的ApplicationContext的依赖注入,可接收配置在Spring的SessionFactory实例,getHibernateTemplate方法则用来根据刚才的SessionFactory产生Session,最后生成HibernateTemplate来完成数据库访问。

典型的继承HibernateDaoSupport的DAO实现的代码如下:

public class PersonDAOHibernate extends HibernateDaoSupport implements PersonDAO

{

    //采用log4j来完成调试时的日志功能

private static Log log = LogFactory.getLog(NewsDAOHibernate.class);

         //返回全部的人的实例

    public List getPersons()

         {       

                   //通过HibernateTemplate的find方法返回Person的全部实例

           return getHibernateTemplate().find("from Person");

    }

         /**

          * 根据主键返回特定实例

          * @ return 特定主键对应的Person实例

          * @ param 主键值

    public News getPerson(int personid)

         {

           return (Person)getHibernateTemplate().get(Person.class, new Integer(personid));

    }

         /**

          * @ person 需要保存的Person实例

          */

    public void savePerson(Person person)

         {                

                   getHibernateTemplate().saveOrUpdate(person);

    }

         /**

          * @ param personid 需要删除Person实例的主键

          * /

    public void removePerson(int personid)

         {

                   //先加载特定实例

           Object p = getHibernateTemplate().load(Person.class, new Integer(personid));

                   //删除特定实例

           getHibernateTemplate().delete(p);

    }

}

可以与前面的PersonDAOHibernate对比,会发现代码量大大减少。事实上,DAO的实现依然借助于HibernateTemplate的模板访问方式,只是,HibernateDaoSupport将依赖注入SessionFactory的工作已经完成,获取HibernateTemplate的工作也已完成。该DAO的配置必须依赖于SessionFactory,具体的配置如下:

/p>

    "http://www.springframework.org/dtd/spring-beans.dtd">

        

        

                  

           com.mysql.jdbc.Driver

                                   

           jdbc:mysql://wonder:3306/j2ee

                  

           root

                  

           pass

   

        

          

                                     lee/Person.hbm.xml

               

          

          

          

                   

                                    

                                     org.hibernate.dialect.MySQLDialect

                                    

                         update

                   

       

          

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the methods of time processing in Go language? What are the methods of time processing in Go language? Jun 10, 2023 pm 09:42 PM

As a modern programming language, Go language plays an important role in development. The Go language provides some built-in time functions and structures to make time processing more convenient. In this article, we will introduce some commonly used time processing methods in the Go language. time.Now() We can use the time.Now() function to get the current time: now:=time.Now()fmt.Println(now) output: 2019-06-131

How to remove unwanted URLs from Chrome's address bar? How to remove unwanted URLs from Chrome's address bar? Mar 08, 2024 am 09:19 AM

Chrome will automatically record the URLs that have been entered in the address bar, and will automatically "associate query content" in the future. But many times we don't need some URLs, how to delete them? The editor often encounters this problem. Addresses that have been entered before will be blocked in front of commonly used addresses, resulting in the need to select several times to enter the desired website. I have looked for how to delete it at least three times because... I forget it every time. In the address bar shortcuts of Chrome's official help Chrome keyboard shortcuts, the delete shortcut key is clarified: ▍Windows deletes the address bar association content. Press the down arrow key to highlight the corresponding content, and then press the Shift+Delete key ▍macOS deletes the address Bar association content click down

How to delete win10 input method How to delete win10 input method Detailed introduction How to delete win10 input method How to delete win10 input method Detailed introduction Jul 07, 2023 pm 05:33 PM

The win10 input method has made great progress compared with previous system versions, and has many very useful functions. It is also very smooth to use overall and has the function of memory typing. However, some users already have their own input methods that are good to use. Now, if you want to delete the built-in win10 input method, how do you delete the win10 input method? Today I will tell you the details about how to delete the win10 input method. How to delete the input method in win10 1. Press [Win+i] to open the settings interface, and click on the "Time and Language" option. 2. Click "Region and Language" and click the "Options" button under "Chinese" on the right. 3. Click the "Microsoft Pinyin Alphabet" option and the "Delete" button will appear.

How to set restore point in win7 How to set restore point in win7 Jul 20, 2023 am 10:33 AM

The win7 system is Microsoft's traditional operating system and has many practical functions, such as the function of win7 system restore points. By setting a restore point in win7, you can restore the system to the restore point in win7 to achieve the purpose of system repair. How to set restore point in win? The following editor will teach you how to set a restore point in win7. How to set a restore point in win7 1. Find the desktop shortcut, right-click and select Properties. 2. After entering the property settings, select Advanced System Configuration. 3. Select the system maintenance option in the pop-up system properties box. 4. Click to create a restore point now for the controller with system protection turned on. 5. Add a description of the restore point, then click Create, wait patiently for a while, the restore point is successfully established, and then click Close. You can restore the system next time.

How to use shortcuts on iPhone How to use shortcuts on iPhone Nov 29, 2023 am 09:06 AM

These shortcuts can be used for a variety of different things on iPhone> In today's dynamic and fast-moving world, where the pursuit of efficiency and convenience takes center stage, the Shortcuts app on iPhone stands out as a very Powerful tool. It's designed to simplify and speed up daily tasks, significantly increasing productivity. This app is incredibly adaptable, enabling users to automate mundane, repetitive actions that consume precious time. Additionally, it provides tools to tailor personalized commands to individual needs and preferences. What's particularly noteworthy about the Shortcuts app is its integration with Apple's smart assistant, Siri. This feature allows the use of intuitive speech

How to upgrade win7 system from xp system How to upgrade win7 system from xp system Jul 13, 2023 am 08:53 AM

The xp system can be said to be a computer system that Microsoft released a long time ago. Currently, very few people are using it. The key is to use it on old computers. Many netizens want to upgrade the xp system to win7, but they don’t know how to upgrade the xp system to win7. .The actual operation steps will be demonstrated below. 1. Download and install the three-step software for beginners and open it. Select the win7 system and click to reinstall immediately. 2. Wait for the tool to automatically download the win7 system. 3. Reinstall the Windows system online immediately after downloading, please follow the prompts. 4. After the installation is completed, you can choose to restart immediately. 5. Enter the run menu bar and select the second item XiaoBaiPE-MSDNOnlineIns

What are the methods of type conversion in Go language? What are the methods of type conversion in Go language? Jun 11, 2023 am 09:05 AM

As the Go language becomes increasingly popular in the Internet industry, more and more developers are beginning to get involved in this language. In the Go language, type conversion is also one of the common programming operations. This article will introduce some common type conversion methods. Type assertion Type assertion is an operation that converts an interface type to another type. In Go language, type assertion can be made using the following syntax: value,ok:=interface{}.(type) where value represents the converted value, o

What are the signal processing methods in Go language? What are the signal processing methods in Go language? Jun 11, 2023 pm 12:28 PM

What are the signal processing methods in Go language? Go language is an efficient, concise programming language with native concurrency capabilities. It is widely used in network programming, distributed systems, cloud computing and other fields. In Go language, inter-process communication is implemented through pipes and signals. This article will introduce the use of signals and their processing methods in the Go language. Signals Overview A signal is a mechanism used by the operating system to send asynchronous events to processes. In Unix/Linux operating systems, signals are often used to notify processes of certain

See all articles