hibernate之利用配置文件和annotation往数据库里插入数据
来源:互联网 发布:2017淘宝双11销售冠军 编辑:程序博客网 时间:2024/06/06 09:13
大三学了javaEE三大框架,但课上只着重讲了struts,hibernate讲了一点,已经忘记的差不多了。马上就要找工作了,重新捧起hibernate自学,今天是第一天,希望能有个好开始。
惯例是先介绍开发环境是:hibernate-annotations-3.4.0.GA,hibernate-distribution-3.3.2.GA,slf4j-1.5.8,JDK1.6,MySQL5.0,myeclipse10.0。
先介绍使用配置文件来往数据库里插入数据:
利用MySQL创建_Student表:
create table _Student(_id int primary key,_name varchar(20),_age int);进入myeclipse先建个java project命名为:Test_001,然后导包:以前导包都是通过Bulid Path->add External jars,现在介绍个使用user library添加jar的新方法:
1.点击Myeclipse的window菜单,选择“Preference”
2.在preferences窗口中选择java->User Libraries,然后点击窗口右边的New...按钮,在弹出的子窗口中输入user library的名称,此时在user libraries窗口中会出现新加的
library名称。
3.向该user library中添加jar包。选中hibernate,然后点击Add JARS...按钮,选择你要添加的jar后,点击“打开”按钮,则hibernate库中就会出现你刚添加的jar文件信息。
4.最后点击窗口下的“OK”按钮,完成user library的添加和其jar的添加。
将user lirbrary添加到工程里面:
1.鼠标右键单击要添加user library库的工程名称,然后点击Build Path->Add Libraries....菜单,
2.在弹出的子窗口中选中User Library,单击Next按钮,在新窗口中选中你要加入的library名称后点击finish按钮即可,这样就在该工程下 会出现你刚加入的library名称。
接着介绍一下我的user library库中所包含的包:slf4j-1.5.8包里的slf4j-nop-1.5.8.jar,hibernate-distribution-3.3.2.GA包根目录里的hibernate3.jar,lib里的required包里的antlr-2.7.6.jar,commons-collections-3.1.jar,dom4j-1.6.1.jar,javassist-3.9.0.GA.jar,jta-1.1.jar,slf4j-api-1.5.8.jar。要连接MySQL数据库当然少不了数据库连接驱动:mysql-connector-java-3.1.13-bin.jar,这个就不需要放到user library库里了,直接通过add external jars方法导入即可。
开发环境介绍完了,果断进入正题:
建包:com.hibernate.model,然后创建个Student类:
package com.hibernate.model;public class Student {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}然后创建配置文件,从路径hibernate-distribution-3.3.2.GA/documentation/manual/zh-CN/html/index.html里找到hibernate官方提供的配置文件格式创建配置文件: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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://192.168.199.230/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">1</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property>--> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property>--> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/hibernate/model/Student.hbm.xml"/> <mapping class="com.hibernate.model.Teacher"></mapping> </session-factory></hibernate-configuration>
创建映射文件Student.hbm.xml,将数据库里的_Student表和Student类映射起来(同理从官方文档里找参考配置文件):
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.hibernate.model"><class name="Student" table="_Student"><id name="id" column="_id"></id><property name="name" column="_name"></property><property name="age" column="_age"></property></class></hibernate-mapping>最后就是创建StudentTest来测试连接了,代码如下:
package com.hibernate.huanglei;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.hibernate.model.Student;public class StudentTest {public static void main(String[] args) {Student s=new Student();s.setId(1120010109);s.setName("huanglei");s.setAge(23);Configuration cfg = new Configuration();SessionFactory sf = cfg.configure().buildSessionFactory();Session session=sf.openSession();session.beginTransaction();session.save(s);session.getTransaction().commit();session.close();sf.close();}}最后亮成果:
接着介绍annotation添加数据:
惯例:先配置开发环境,往user library里再添加几个annotation必须的包:dom4j.jar,ejb3-persistence.jar,hibernate-commons-annotations.jar,hibernate-core.jar,slf4j-api.jar,hibernate-annotations.jar。
创建_Teacher表:
create table _Teacher(_id int primary key,_name varchar(20),_title varchar(20));创建Teacher类:
package com.hibernate.model;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="_teacher")public class Teacher {private int id;private String name;private String title;@Id@Column(name="_id")public int getId() {return id;}public void setId(int id) {this.id = id;}@Column(name = "_name")public String getName() {return name;}public void setName(String name) {this.name = name;}@Column(name = "_title")public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}}创建配置文件:
<?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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://192.168.199.230/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">1</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property>--> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property>--> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/hibernate/model/Student.hbm.xml"/> <mapping class="com.hibernate.model.Teacher"></mapping> </session-factory></hibernate-configuration>创建TeacherTest类:
package com.hibernate.huanglei;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;import com.hibernate.model.Teacher;public class TeacherTest {public static void main(String[] args) {Teacher t = new Teacher();t.setId(4);t.setName("yy");t.setTitle("高级");//此处与使用xml配置文件方式不同Configuration cfg = new AnnotationConfiguration();SessionFactory sf = cfg.configure().buildSessionFactory();Session session = sf.openSession();session.beginTransaction();session.save(t);session.getTransaction().commit();session.close();sf.close();}}亮成果:
欢迎关注行者摩罗微信公众号(xingzhemoluo),共同交流编程经验,扫描下方二维码即可;
- hibernate之利用配置文件和annotation往数据库里插入数据
- hibernate使用配置文件创建联合主键往MySQL数据库插入数据
- Hibernate:利用配置文件生成数据库
- 利用Hibernate配置文件生成数据库
- 利用Hibernate配置文件生成数据库
- 利用Hibernate配置文件生成数据库
- Hibernate采用*.hbm.xml方式和注释(annotation)插入数据示例
- Hibernate采用*.hbm.xml方式和注释(annotation)插入数据示例
- 对hibernate的学习之用配置文件和annotation配置的区别
- 利用Hibernate往SqlServer里存入图片或二进制数据
- 利用Hibernate往SqlServer里存入图片或二进制数据
- 如何利用Hibernate配置文件生成数据库
- 利用hibernate配置文件逆向映射数据库表
- 利用Hibernate配置文件反向生成数据库
- 利用存储过程读指定文件夹里所有txt文件,将数据插入数据库中
- Hibernate中的Annotation和更新数据库
- 怎么利用Spring利用注解读取配置文件里的数据
- hibernate 支持配置文件和annotation两种方式
- .htaccess教程:简介、访问控制、验证、目录浏览控
- Path Sum II
- 25个让Java程序员更高效的Eclipse插件
- MFC开启与关闭外部进程
- dp,px,pt,sp 的区别 以及dp 和 px 互转
- hibernate之利用配置文件和annotation往数据库里插入数据
- JS事件:target与currentTarget区别
- HDU 3652 B-number
- table 列排序&行变色&按上下键光标在行上移动&上下移动行并保存DB
- Android源码学习之八—系统启动过程
- 数据结构(第1章: 概述)
- 最临近、双线性、三次卷积插值(图像放缩)
- 操作系统笔试题汇总
- nyoj 946 商业规划 STL(set)