学习Hibernate

项目结构

 

News.java和NewsTest.java是另一个小项目,不用理会。

 

1、建立UserName.java类

package com.cfc.hibernate;

public class UserName {

	private int id;
	private String name;
	private String password;

	public int getId() {
		return this.id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return this.password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

2、建立UserName.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="com.cfc.hibernate">
	
	
	<class name="UserName">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name" />
		<property name="password" />
	</class>
	
	
	
</hibernate-mapping>


3、建立:hibernate.cfc.xml

<!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="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>  
            <!-- 数据库名称地址 -->  
            <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=hibernatedemo01</property>  
            <!-- 数据库用户名 -->  
            <property name="hibernate.connection.username">sa</property>  
            <!-- 数据库密码 -->  
            <property name="hibernate.connection.password">1234</property>  
            <!-- 配置数据库适配器.可以直接使用相关数据库的相关特点-->  
            <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>  
            
            <!-- 根据需要自动创建数据表 -->  
            <property name="hibernate.hbm2ddl.auto">update</property>
            
            <!-- 打印sql语句到控制台 --> 
            <property name="hibernate.show_sql">true</property>  
            <!-- 对打印的Sql语句进行格式化 -->  
            <property name="hibernate.format_sql">true</property>  
              
             
            <!-- 对资源进行映射 -->  
            <mapping resource="com/cfc/hibernate/UserName.hbm.xml"/>  
        </session-factory>  
    </hibernate-configuration>  


4、测试类:Test.java

package com.cfc.hibernate.demo;

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

import com.cfc.hibernate.UserName;

public class Test {

	public static void main(String[] args){
		try{
			Configuration conf=new Configuration().configure();
			
			SessionFactory sf=conf.buildSessionFactory();
			
			Session sess=sf.openSession();
			Transaction tx=sess.beginTransaction();
			UserName user=new UserName();
			user.setName("ss");
			user.setPassword("22");
			sess.save(user);
			tx.commit();
			sess.close();
			System.out.print("end");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
}


结果是:

 

总结:

1、之前的UserName.java曾用User.java。因为之前建立过,后又删了。出现了问题(总说:Caused by: java.sql.SQLException: 在关键字 'User' 附近有语法错误,后在网上查到相关资料,换名就行了)。

2、原先使用的是驱动是微软的。但出现了问题,上网找一些资料。说是微软提供的驱动有bug。于是改了用了jtds-1.2.2.jar。问题解决

hibernate相关的包已经上传。不是完整版。http://download.csdn.net/detail/caikule/5112687

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值