Database operations using JdbcTemplate and Hibernatetemplate in spring

Source: Internet
Author: User

Currently I am exposed to the spring database operations mainly include two kinds, mainly use JdbcTemplate and hibernatetemplate. The latter is used in conjunction with hibernate operations.

Both provide persistent layer access templating, and only need to obtain a sessionfactory to perform persistence operations. Can complete the increase and deletion check operation.

There are two ways to get JdbcTemplate and hibernatetemplate

1. The traditional way

2. Obtained using Jdbcdaosupport and Hibernatedaosupport, which contains two methods.

(1) gethibernatetemplate/getjdbctemplate (2) setsessionfactory

Also need to inject sesssionfactory

The second way is generally used

Building a DAO Layer query

Basedao

1  Package Com.jason.centralbank.dao; 2 3 Import java.util.List; 4 5  Public Interface Basedao<t> {67     list<t> findAll (class<t> entityclazz) ; 8 }

Basedaohibernate

1  PackageCom.jason.centralbank.dao;2 3 Importorg.hibernate.SessionFactory;4 Importorg.springframework.beans.factory.annotation.Autowired;5 ImportOrg.springframework.orm.hibernate4.support.HibernateDaoSupport;6 7 Importjava.util.List;8 9  Public classBasedaohibernate<t>extendsHibernatedaosupportImplementsBasedao<t> {Ten  One @Autowired A      Public voidsetsessionfactoryoverride (sessionfactory sessionfactory) -     { -         Super. Setsessionfactory (sessionfactory); the     } -  -@SuppressWarnings ("Redundantcast") - @Override +      PublicList<t> FindAll (class<t>Entityclazz) { -         return(list<t>) Gethibernatetemplate (). Loadall (Entityclazz); +     } A}

Depositinforbasedao

 Package Com.jason.centralbank.dao; Import com.jason.centralbank.entities.DepositInfor;  Public Interface extends Basedao<depositinfor>{}

Depositinfordao

1  PackageCom.jason.centralbank.dao;2 3 Importcom.jason.centralbank.entities.DepositInfor;4 Importorg.springframework.stereotype.Repository;5 6 @Repository7  Public classDepositinfordaoextendsBasedaohibernate<depositinfor>Implementsdepositinforbasedao{8}

Depositinfor

 Packagecom.jason.centralbank.entities;ImportJavax.persistence.*; @Entity @table (name= "2011 Deposit amount in Baotou area") Public classdepositinfor {@Id @Column (name= "unit")    PrivateString institution; @Column (Name= "January")    PrivateString Jan; @Column (Name= "February")    PrivateString Feb; @Column (Name= "March")    PrivateString Mar; @Column (Name= "April")    PrivateString APR; @Column (Name= "May")    PrivateString May; @Column (Name= "June")    PrivateString June; @Column (Name= "First half")    PrivateString firsthalfyear;  PublicString getinstitution () {returninstitution; }     Public voidsetinstitution (String institution) {Institution=institution; }     PublicString Getjan () {returnJan; }     Public voidSetjan (String Jan) {Jan=Jan; }     PublicString Getfeb () {returnFeb; }     Public voidSetfeb (String Feb) {Feb=Feb; }     PublicString Getmar () {returnMar; }     Public voidSetmar (String mar) {Mar=Mar; }     PublicString getapr () {returnAPR; }     Public voidsetapr (String Apr) {Apr=Apr; }     PublicString Getmay () {returnMay ; }     Public voidSetmay (String May) { may=May ; }     PublicString Getjun () {returnJune; }     Public voidSetjun (String June) {June=June; }     PublicString getfirsthalfyear () {returnfirsthalfyear; }     Public voidsetfirsthalfyear (String firsthalfyear) {firsthalfyear=firsthalfyear; } @Override PublicString toString () {return"depositinfor{" + "institution=" + institution + "\" + ", jan= '" + Jan + "\" +                ", feb= '" + Feb + "\" + ", mar= ' + Mar + ' \ ' +", apr= ' "+ Apr +" \ "+ ", may= '" + May + "\" + ", jun= ' + June + ' \ ' + ', firsthalfyear= ' + firsthalfyear + ' \ ' + '} '; }}

Database operations using JdbcTemplate and Hibernatetemplate in spring

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.