Spring Notes ④--spring integrated Hibernate link Database

Source: Internet
Author: User

Integrate hibernate

Integrate what?

    1. An IOC container to manage Hibernate's sessionfactory
    2. Let hibernate use a declarative transaction on spring

?

Add Hibernate driver Package first

New Hibernate.cfg.xml

Configure basic properties for Hibernate

    1. The data source needs to be configured in the IOC container, so you no longer need to configure the data source here
    2. The associated. Hbm.xml is also configured in the IOC container when the Sessionfactory instance is configured.
    3. Configure Hibernate's basic properties: dialect, SQL display and formatting, strategies for generating data tables, and level two caching.

<Propertyname="hibernate.dialect"> Org.hibernate.dialect.MySQL5Dialect</Property>

?

<Propertyname=' hibernate.show_sql '>true</ Property >

<Propertyname="hibernate.format_sql">true</ Property>

?

<Propertyname="Hibernate.hbm2ddl.auto">update</propert

?

?

Ctrl+shift+t Open source file

?

When you join spring

?

Db.properties

jdbc.user=Root

jdbc.password=Root

jdbc.driverclass=Com.mysql.jdbc.Driver

jdbc.jdbcurl=jdbc:mysql://localhost: 3306/spring

?

Jdbc.initpoolsize=5

Jdbc.maxpoolsize=Ten

?

?

Configure the data source

Need to import

???? xmlns:context= "Http://www.springframework.org/schema/context"

<!-- Configure the data source -

???? <context:property-placeholderlocation="Classpath:db.properties" />

???? <bean id= " DataSource " class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >

???????? <property name= "user" value= "${jdbc.user}" ></< Span style= "COLOR: #3f7f7f" >property>

???????? <property name= "password" value< Span style= "Color:black" >= "${jdbc.password}" > </property>

???????? <property name= "Driverclass" Value= "${jdbc.driverclass}" ></property>

???????? <property name= "Jdbcurl" value< Span style= "Color:black" >= "${jdbc.jdbcurl}" > </property>

???????? <property name= "Initialpoolsize" Value= "${jdbc.initpoolsize}" ></property>

???????? <propertyname="Maxpoolsize"value="${jdbc.maxpoolsize}" ></Property>

???? </Bean>

?

Test class test can get DataSource

?

Public class Go {

???? privatestatic applicationcontext ctx=new Classpathxmlapplicationcontext ("Applicationcontext.xml");

?

???? Public staticvoid main (string[] args) throws SQLException {

???????? DataSource D= ctx. Getbean (DataSource. class );

???????? System. out. println (D. getconnection ());

????}

}

?

Using spring to manipulate hibernate and use spring's transactions

?

Spring Notes ④--spring integrated Hibernate link Database

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.