这个类名字那,我随便起的。别介意哈,User.java和User.hbm.xml好像名字得一样,反正一样就对了,里面有映射关系,好像是自己写的。
public class User{
private String id;
private String name;
private String address;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
}
上面是一个User基础类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User">
<id name="id" column="id"><!-- name:pojo类中属性名字 column:数据库中字段名 -->
<generator class="native"></generator><!-- 主键的生成策略,这个是数据库中决定主键 -->
</id>
<property name="name" column="name"></property>
<property name="address" column="address"></property>
</class>
</hibernate-mapping>
这个是User.hbm.xml文件
记得建一个文件log4j.properties这个东西 不然报错下面是代码
log4j.rootCategory=debug,stdout,A1
#log4j.rootCategory=INFO,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p %c - %m%n
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=${webApp.root}/WEB-INF/mylog.log
log4j.appender.A1.Encoding=UTF-8
log4j.appender.A1.MaxFileSize=4096KB
log4j.appender.A1.Append=true
log4j.appender.A1.MaxBackupIndex=10
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %5p - %c -%-4r [%t] - %m%n
log4j.logger.org.mybatis=TRACE
下面是hibernate.cfg.xml 这个是配置数据库的
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myhi</property><!--数据库的地址-->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property><!--数据库的密码-->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.max_size">5</property>
<property name="c3p0.timeout">120</property>
<property name="c3p0.idle_test_period">3000</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.autocommit">true</property>
<mapping resource="User.hbm.xml" />
</session-factory>
</hibernate-configuration>
下面盗图一张下面写一个测试类 名字随意
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
*
*/
/**
* Description:
*
* @author l.liang
* @2018-7-27
*/
/**
* Description:
*
* @author l.liang
* @2018-7-27
*/
public class Users{
private static SessionFactory sessionFactory;
static {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml"); // 读取指定的主配置文件
sessionFactory = configuration.buildSessionFactory(); // 根据配置生成Session工厂
}
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Transaction tx = session.beginTransaction();
@Test
public void updateUserTest() {
User user = new User();
user.setName("王二");
user.setAddress("北京市海淀区宝盛西里");
// 保存
Session session = sessionFactory.openSession(); // 打开一个新的Session
Transaction tx = session.beginTransaction(); // 开启事务
session.save(user);
tx.commit(); // 提交事务
session.close(); // 关闭Session,释放资源(不一定是真正的关闭)
}
}
后面说一下建数据库,我用的是Navicat for Mysql 建一个数据库就可以了。很简单 不过的设置一下主键id 自动增
jar包地址,自取