Getting started with hibernate

1. Entity class

package domain;
public class user {
private int id;
private String username;
private  String password;
@Override
public String toString() {
return "user [id=" + id + ", username=" + username + ", password=" + password + "]";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}

2.User.hbm.xml

Role: configure mapping, generate primary key

<!-- Mapping of configuration class and table structure-->
<class name="domain.user" table="user">
<!-- The configuration id 
sees the name attribute, and the JavaBean attribute
sees the column attribute, which is a table Fields of the structure
-->
<id name="id" column="id">
<!-- Primary key generation strategy-->
<generator class="native"/>
</id> <!-- Configure other Properties --> <property name="username" column="username" type="string"></property> <property name="password" column="password" type="string"></property>




</class>

3.hibernate.cfg.xml

Role: establish a connection with the database

<!-- Remember: first configure the SessionFactory tag, a database corresponds to a SessionFactory tag-->
<session-factory> <!-- There are 5 parameters that must be configured, 4 parameters, the dialect of the database --> < property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property> <property name= "hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <!-- database dialect--> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property> <!-- Optional configuration --> <!-- Display SQL statement, display in console --> <property name="hibernate.show_sql">true</property> <!-- Format SQL statement--> <property name="hibernate.format_sql">true</property> <!-- Generate database table structure 

















update: If there is no table structure, create a table structure. If it exists, it will not be created, add data
-->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- mapping configuration file, you need to import the mapping configuration file--> <mapping resource= "domain/User.hbm.xml"/>



</session-factory>

4.demo1 test class, hibernate entry to store data

package dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import domain.user;

public class demo1 {
public static void main(String[] args) {
//Read configuration file
Configuration conf= new Configuration().configure();
//Create sessionFactory factory
SessionFactory sf=conf.buildSessionFactory();
//Open session
Session session=sf.openSession();
//Create transaction
Transaction tx=null; tx=session.beginTransaction();//Start //Persistence   try{ user u=new user(); u.setUsername("xu "); u.setPassword("123"); session.save(u); tx.commit();   }catch(Exception e){   if(tx!=null){   tx.rollback();}   e.printStackTrace ();   }finally{   session.close();















  }
  }

}

5. Test results


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774137&siteId=291194637