• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

hibernate无法写入数据

hibernate-release-5.2.9.Final

junit4.9
mysql使用5.1.29


这是cfg.xml文件



   
    root
    123456
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/webases
   org.hibernate.dialect.MySQL5InnoDBDialect  
     
     true
     true
     create
   
   

这是实体类
package hibernate;


import java.util.Date;


public class Student {
      private int sid;
      private String sname;
      private String gender;
      private Date birthday;
      private String address;
      
     
      public Student(int sid,String sname,String gender,Date birthday,String address){
     this.sid=sid;
     this.sname=sname;
     this.gender=gender;
     this.birthday=birthday;
     this.address=address;
     
      }
public Student(){
     
     
      }
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
      
}
这是hbm.xml




   
        
            
            
        
        
            
        
        
            
        
        
            
        
        
            
        
   

这是测试类
package hibernate;


import java.util.Date;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class StudentsTest {


  private SessionFactory sessionFactory;
  private Session session;
  private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config=new Configuration().configure();
config.addClass(Student.class);
//创建服务注册对象
ServiceRegistry serviceRegistry =new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
//创建会话工厂对象
sessionFactory=config.buildSessionFactory(serviceRegistry);
//会话对象
session=sessionFactory.openSession();
//开启事物
transaction=session.beginTransaction();
}
@After
public void destroy(){
transaction.commit();//提交事物
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂


}


@Test
public void testSaveStudent(){
Student s=new Student(1,"张三","男",new Date(),"丽水");
session.save(s);


}


}
214003t3xsdx3m3wmorjqx.jpg

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031168位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!