您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

hibernate如何连接mysql数据库?

2024/3/14 21:37:57发布21次查看
教大家如何把hibernate连接mysql数据库,只要大家跟着我下面的步骤走,就绝对没有问题的。代码都是可以直接用到或者复制的。
1.首先,我们把hibernate最基本的数据库连接,使用mysql。 见一个java工程,见一个包名为book, 在book的包下加一个java类book.java,其代码如下:
package book; public class book { private integer id; private string name; private string writer; public integer get hibernate
最基本的数据库连接,使用mysql。 见一个java工程,见一个包名为“book”
2.然后在在book的包下加一个java类book.java,其代码如下:
package book; public class book { private integer id; private string name; private string writer; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getwriter() { return writer; } public void setwriter(string writer) { this.writer = writer; } }
3.然后在book包下建一个book.hbm.xml,其代码如下:
<?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="book" default-lazy="false"> <class name="book"> <id name="id"> <generator class="increment"/> </id> <property name="name" ></property> <property name="writer" ></property> </class> </hibernate-mapping>
4.这个事与数据库里面的字段名形成映射关系,自己在mysql建立book表时与之对应,id是自增长的, 然后在工程的根目录下建一个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> <property name="connection.driver_class">com.mysql.jdbc.driver</property> <property name="connection.url">jdbc:mysql://localhost/mydb</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <property name="show_sql">true</property> <!-- <property name="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</property>--> <!-- <property name="current_session_context_class">thread</property>--> <mapping resource="book/book.hbm.xml" /> </session-factory> </hibernate-configuration>
5.这是连接mysql数据库的,用户名和密码改为你mysql数据库的
<property name="show_sql">true</property>这是在后台打印sql语句 <mapping resource="book/book.hbm.xml" />这是找到映射文件。
6.然后些个测试类:代码如下:
package test; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.hibernate.cfg.configuration; import book.book; public class maintest { /** * @param args */ public static void main(string[] args) { try { configuration cfg=new configuration()。configure(); sessionfactory sf=cfg.buildsessionfactory(); session session = sf.opensession(); transaction ts=session.begintransaction(); book b=new book(); b.setname("hibernate"); b.setwriter("div"); session.save(b); // book b=(book) session.get(book.class,1); // if(b!=null){ // b.setname("xujun"); // system.out.println("书名为:"+b.getname()); // system.out.println("作者为:"+b.getwriter()); // session.delete(b); // } ts.commit(); session.close(); sf.close(); } catch (exception e) { e.printstacktrace(); } } }
7.mysql表的字段如下:
8.把数据库建好后就可以测试。对了,关键的还没有说,还得把antlr.jar,cglib.jar,asm.jar,asm-attrs.jar,commons-colletions.jar,commons-logging.jar,ehcache.jar,    jta.jar,dom4.jar,log4.jar,hibernate3.jar引入到lib目录下
以上就是hibernate如何连接mysql数据库?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product