Integrate hibernate
Integrate what?
- An IOC container to manage Hibernate's sessionfactory
- Let hibernate use a declarative transaction on spring
?
Add Hibernate driver Package first
New Hibernate.cfg.xml
Configure basic properties for Hibernate
- The data source needs to be configured in the IOC container, so you no longer need to configure the data source here
- The associated. Hbm.xml is also configured in the IOC container when the Sessionfactory instance is configured.
- 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