• hibernate之使用Annotation注解搭建项目


    之前开发都是使用xml配置来开发项目,开发起来特别繁琐

    大家会发现通过注解大大简化了我们开发流程,使我们从繁琐的XML配置中解放出来。

    第一步:新建一个javaweb项目。并将hibernate需要的相关jar包导入。

     

    第二步: 使用annotation需要额外增加3个jar包:

    ◦ hibernate-annotations.jar

    ◦ ejb3-persistence.jar

    ◦ hibernate-commons-annotations.jar

     

    第三步:新建一个pojo并增加注解:

     1 package com.qcf.pox;
     2 
     3 import java.util.Date;
     4 
     5 import javax.persistence.Entity;
     6 import javax.persistence.GeneratedValue;
     7 import javax.persistence.GenerationType;
     8 import javax.persistence.Id;
     9 
    10 @Entity 
    11 public class Student {
    12     
    13     @Id
    14     @GeneratedValue(strategy=GenerationType.AUTO)//代表主键的生成策略
    15     private int stuno;
    16     private String stuname;
    17     private Date birthday;
    18     public int getStuno() {
    19         return stuno;
    20     }
    21     public void setStuno(int stuno) {
    22         this.stuno = stuno;
    23     }
    24     public String getStuname() {
    25         return stuname;
    26     }
    27     public void setStuname(String stuname) {
    28         this.stuname = stuname;
    29     }
    30     public Date getBirthday() {
    31         return birthday;
    32     }
    33     public void setBirthday(Date birthday) {
    34         this.birthday = birthday;
    35     }
    36     public Student() {
    37         super();
    38     }
    39     public Student(int stuno, String stuname, Date birthday) {
    40         super();
    41         this.stuno = stuno;
    42         this.stuname = stuname;
    43         this.birthday = birthday;
    44     }
    45     
    46 }
    View Code

     

    第四步:在hibernate.cfg.xml中增加:

    这里我们需要注意的是 使用注解的时候是class   使用xml配置的时候使用的resource

    1         <!-- 引入映射文件 -->
    2         <mapping class="com.qcf.pox.Student"/>
    View Code

    第五步:写测试代码:

     1 package com.qcf.test;
     2 
     3 import java.util.Date;
     4 
     5 import org.hibernate.Session;
     6 import org.hibernate.SessionFactory;
     7 import org.hibernate.Transaction;
     8 import org.hibernate.cfg.AnnotationConfiguration;
     9 import org.hibernate.cfg.Configuration;
    10 
    11 import com.qcf.pox.Student;
    12 
    13 public class TestAnnotation {
    14     public static void main(String[] args) {
    15         //获取configuration对象
    16         Configuration configuration=new AnnotationConfiguration().configure();
    17         SessionFactory factory=configuration.buildSessionFactory();
    18         Session session=factory.openSession();
    19         //创建student对象
    20         Student student=new Student();
    21         student.setStuname("fawfeaw");
    22         student.setBirthday(new Date());
    23         //获取事务
    24         Transaction transaction= session.beginTransaction();
    25         //将student变为持久态
    26         session.save(student);
    27         //提交事务
    28         transaction.commit();
    29         session.close();
    30     }
    31     
    32 }
    View Code

    科普知识:

    常见的注解及其典型用法

    注解名称

    作用

    典型代码/其他

    @Entity

    将一个类声明为一个实体bean(即一个持久化POJO)  

    @Entity

    public class User {

    //…类体省略

    }

    @Table

    注解声明了该实体bean映射指定的表(table,目录(catalog)和schema的名字

    @Entity

    @Table(name="_user")

    public class User {

       //…省略类体

    }

    @Id

    注解声明了该实体bean的标识属性(对应表中的主键)

    @Entity

    public class User {

    @Id

    @GeneratedValue(strategy=GenerationType.AUTO)

    private int id;

    private String name;

    private Date birthday;

       //省略了getset方法

    }

    @GeneratedValue

    声明了主键的生成策略。该注解有如下属性

    @Column

    声明了属性到列的映射

    @Column(name="_uname")

    private String name;

    @Transient

    声明的属性不会跟数据库关联

    @Transient

    private Date birthday;

     

    则数据库中不会有birthday

    @Formula

    用一个查询语句动态的生成一个类的属性. 表示这个属性是一个虚拟的列,表中并没有这个列。需要通过查询语句计算出来。

    @Formula("(select count(*) from _user u where u.id>id)")

    private int countUser;

    要点:

    1. Sql语句必须位于括号中
    2. 这里最好写完全的sql语句。表名最好使用别名
    3. 如果要引用当前对象的属性值,可以直接使用属性,如:

    u.id>id

    第二个id就是对象的值!

     

     

  • 相关阅读:
    使用npm安装一些包失败了的看过来(npm国内镜像介绍)(解决生成空的abp模板项目一直卡在还原cpm包中)
    .NET CORE 发布到IIS问题 HTTP ERROR 500.30
    .NET Core默认不支持GB2312,使用Encoding.GetEncoding(“GB2312”)的时候会抛出异常。
    .net c# 文件分片/断点续传之下载--客户端
    aspnetcore 实现断点续传
    C# 反射获取属性值、名称、类型以及集合的属性值、类型名称
    C# 3Des两种加密方式 (对应java中的desede/CBC/PKCS5Padding加密)
    Asp.NetCore3.1中多次读取Request.Body
    ASP.NET Core 2.0系列学习笔记-DI依赖注入
    C# Newtonsoft.Json JObject合并对象整理
  • 原文地址:https://www.cnblogs.com/quchengfeng/p/4110698.html
Copyright © 2020-2023  润新知