使用eclipse下的JPA进行实体映射

  1. 配置文件位置:resources->META-INF->persistence.xml
    配置项:



    
        com.jpa.lj.entity.Student


        
            
            
            
            
            
            
        

    

  1. 注意在使用maven构建项目时,需注意mysql版本信息,其中8.0其驱动不再是com.mysql.jdbc.Driver,新版本改成了com.mysql.cj.jdbc.Driver
  2. 注意设置,其数据库连接url中useSSL=false
  3. 数据库,时间设置,默认数据库时间是EDT,需将其改成time_zone,否则进行数据库连接时报错
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | EDT    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.18 sec)
[root@node001 ~]# date
Thu Jun 21 13:54:35 EDT 2018

其具体设置方式为:
在mysql中执行 set time_zone=SYSTEM;
再次在mysql中执行select now();
执行 set global time_zone='+8:00';
执行 flush privileges;
---完成----
样例如下:

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>  set time_zone=SYSTEM;
Query OK, 0 rows affected (0.00 sec)

mysql> select nows();
ERROR 1305 (42000): FUNCTION mysql.nows does not exist
mysql> select nows;
ERROR 1054 (42S22): Unknown column 'nows' in 'field list'
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-06-21 13:56:50 |
+---------------------+
1 row in set (0.03 sec)

mysql> set global time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)
mysql> exit

当然,在完成上面基本的配置项时,还需进行实体Bean进行注解和进行测试

  • 实体Bean
类上标注@Entity
其他项
@Id    //主键
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "payablemoney_gen")   //主键生成策略,和策略名
@Column(name="列名")
  • 测试
    public static void main(String[] args) {
        //实体管理工厂
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("Student_details");
        //实体管理
        EntityManager manager = factory.createEntityManager();
        //打开事物
        manager.getTransaction().begin();
        //调用persist方法
        manager.persist(new Student(1001, "lj", 85));
        manager.persist(new Student(1101, "xw", 75));
        manager.persist(new Student(1201, "ha", 80));
        Student student=new Student();
        student.setFees(100);
        student.setName("张三");

        manager.persist(student);
        //事物提交
        manager.getTransaction().commit();
        //资源关闭
        manager.close();
        factory.close();
    }

你可能感兴趣的:(使用eclipse下的JPA进行实体映射)