HIBERNATE5 Environment Construction

Source: Internet
Author: User

1. Guide Package
    • Hibernate Development Package
    • the driver package for the database
 2. Core configuration Files Core configuration file (assigned to SRC)1. Core Configuration Files for Hibernate :
    1. Hibernate.cfg.xml
    2. hibernate.properties
We use more in the development of Hibernate.cfg.xml This way, the reason it is more capable of configuration, easy to modify
Our main explanation is the hibernate.cfg.xml configuration  1. Can load database related information<!--Configure four items about database connections Driverclass URL username password<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.driver</propert Y> <property name= "Hibernate.connection.url" >jdbc:mysql:///hibernateTest</property><property name= "Hibernate.connection.username" >root</property><property name= "Hibernate.connection.password" >123</property>2.hibernate Related Configurations<!--can display the SQL sent to the database--<property name= "Hibernate.show_sql" >true</property> <!--formatted SQL --<property name= "Hibernate.format_sql" >true</property>      <!--hibernate dialect (tell the linked database type MySQL for example) --<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property> 3. Load the mapping profile          <!--Configure Hibernate mapping File location--<mapping resource= "Cn/itheima/domain/customer.hbm.xml"/> for the contents of the Hibernate.cfg.xml configuration file, refer to the Hibernate/project/etc/hibernate.properties configuration
 After configuring these properties, we can create the table automatically#hibernate. Hbm2ddl.auto Create-drop#hibernate. Hbm2ddl.auto Create#hibernate. Hbm2ddl.auto Update#hibernate. Hbm2ddl.auto Validate Create-drop each time a new table is created and deleted after execution is complete. Typically used in tests
Create a new table each time, typically in a test
Update If the database has tables, is not created, no table is created, and if the mappings do not match, the table structure is automatically updated (can only be added)validate only uses the existing table, and the mapping relationship is verified.  3. Mapping configuration FilesNaming conventions:class name. Hbm.xmlIt is generally placed under the package where the entity class resides. The main function of this configuration file is to establish a mapping relationship between the table and the class.1. Declare the package name uniformly so that you do not need to write the full name of the class in <class>.

2. About <class> Label Configuration<class name= "Cn.itcast.domain.Customer" table= "T_coustomer " catalog= "Hibernatetest" ><id></id>
</class>Name property: Names of the full Name table table of the class can be omitted, and the name of the table is consistent with the class name. Catalog Property: Database name can be omitted. If omitted, reference the library name in the URL path in the core configuration file3. About <id> Tags<!--ID is used to describe the primary key --<id name= "id" column= "id" >       <!--primary key generation strategy --<generator class= "native" ></generator></id>First it must exist. <id> is used to establish the properties in the class with the primary key mappings in the table. The name of the attribute in the names class in the column table is the primary key name in the column, and it can also be omitted, when the name of the columns is the same as the property names in the class. Length of the Type property specifies the type <generator> it primarily describes the primary key generation strategy.4. About <property> Tags<!--Use property to describe the correspondence between attributes and fields--<property name= "name" column= "name" length= "></property> "<property name= "Address" column= "Address" length= "></property> "About the type problem in Hibernate's mapping file There are three types of values for the Type property: Data type 2.hibernate in 1.java Data type of 3.SQLdefault isHibernatedata type in
4. Get the tool class for session authoring (optional)
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * HibernateUtils工具类 *  * @author TGV5 * */public class HibernateUtils {private static Configuration config;private static SessionFactory sessionFactory;static {config = new Configuration().configure();sessionFactory = config.buildSessionFactory();}/** * 获取SessionFactory *  * @return */public static Session openSession() {return sessionFactory.openSession();}public static Session getCurrentSession() {return sessionFactory.getCurrentSession();}}
5.C3P0 configuration of the connection pool (although Hibernate has a default connection pool but performance is not as good as C3P0 connection pool) 1. Import the 3 c3p0 jar packages provided by Hibernate 2. Add a provider configuration in the core configuration file Hibernate.cfg.xml C3P0 configuration can not be configured, there will be a default configuration
<!--  设置连接提供者 --><property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><!-- c3p0连接池的配置 --><property name="hibernate.c3p0.max_size">20</property><!--  最大连接池 --><property name="hibernate.c3p0.min_size">5</property><!--  最小连接数 --><property name="hibernate.c3p0.timeout">120</property> <!-- 超时 --><property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 空闲连接  -->
List of attachments

HIBERNATE5 Environment Construction

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.