《mysql使用hibernate入门案例》
2013-02-25 17:09
781 查看
//User.java package com.fenghuo.domain; public class User { private Integer id; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<!-- User.hbm.xml --> <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 该文件用于配置,domain跟表的映射关系 --> <hibernate-mapping package="com.fenghuo.domain"> <class name="User" table="user"> <!-- id元素用于指定主键属性 --> <id name="id" column="id" type="java.lang.Integer"> <!-- 用于指定主键生成策略 --> <generator class="native"></generator> </id> <!-- 对其他属性进行配置 --> <property name="username" type="java.lang.String"> <column name="username" not-null="false"></column> </property> <property name="password" type="java.lang.String"> <column name="password" not-null="false"></column> </property> </class> </hibernate-mapping>
<!-- hibernate.cfg.xml --> <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置使用的driver --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.url">jdbc:mysql:///test</property> <!-- 告诉hibernate用的那种数据库 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 显示出对应的sql语句 --> <property name="show_sql">true</property> <!-- 指定对象管理映射文件 --> <mapping resource="com/fenghuo/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
//TestMain.java package com.fenghuo.view; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.fenghuo.domain.User; public class TestMain { public static void main(String[] args) { //1.创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化 Configuration configuration = new Configuration().configure(); //2.创建SessionFactory[是一个会话工厂,是一个重量级的类] SessionFactory sessionFactory = configuration.buildSessionFactory(); //3.创建Session Session session = sessionFactory.openSession(); //4.使用事务提交 Transaction transaction = session.beginTransaction(); //添加一个用户 User user = new User(); user.setUsername("fenghuo"); user.setPassword("123"); session.save(user); transaction.commit(); session.close(); } }
文件结构:
相关文章推荐
- 【自】nodeJs使用express框架进行快速建站 连接mysql进行增删改查的实际入门案例
- MySQL入门——约束简介、外键约束说明、外键约束主表与子表的创建与使用约束案例
- 怎样在hibernate的HQL语句中使用mysql 的自定义函数?
- MySQL数据库之使用与开发(一)------mysql入门
- java使用hibernate访问mysql 如何配置hibernate.cfg.xml
- 使用hibernate和mysql生成数据表时,类型为string的属性对应的数据列不能插入汉字
- hibernate中获取使用mysql当前插入一条记录的id值
- MySQL入门教程3 —— 创建、选择并使用数据库
- MySQL的id关联和索引使用的实际优化案例
- Hibernate 入门小案例
- mysql中的游标使用案例
- PHP---MySQL快速入门使用
- CXF使用教程(一)——简介以及入门案例
- Hibernate使用快速入门介绍(2)
- Hibernate学习笔记 -- day02 hibernateAPI详解以及入门案例演示
- Hibernate入门案例及增删改查
- 【转】mysqlbinlog 的使用案例
- Hibernate入门案例
- mysql的入门基本使用(适合新手学习)
- 使用Hibernate时不能在mysql字段中用desc