- 配置文件位置:resources->META-INF->persistence.xml
配置项:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<!--必须-->
<persistence-unit name="Student_details">
<class>com.jpa.lj.entity.Student</class>
<!--必须-->
<!--数据库连接项-->
<properties>
<property name="eclipselink.jdbc.url" value="jdbc:mysql://172.18.37.51:3306/jpatest?useSSL=false"/>
<property name="eclipselink.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="eclipselink.jdbc.user" value="root"/>
<property name="eclipselink.jdbc.password" value="topsec*talent"/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
<!--end-->
</persistence-unit>
</persistence>
- 注意在使用maven构建项目时,需注意mysql版本信息,其中8.0其驱动不再是
com.mysql.jdbc.Driver
,新版本改成了com.mysql.cj.jdbc.Driver
- 注意设置,其数据库连接url中useSSL=false
- 数据库,时间设置,默认数据库时间是
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();
}