Intellij IDEA如何通过数据库表生成带注解的实体类
第一步:新建一个Maven的项目项目的名称为JpaDemo。
我这里是通过思想插件对应的春天项目生成器https://start.spring.io,直接生成项目如图:
下一步,修改成对应项目的基本信息如图:
选择相应的依赖的jar包。
选择项目的位置
完成创建
温馨提示,之前需要安装好行家。
第二步:配置数据库连接。
选择MySQL的。
配置数据库基本信息
其实配置了这个数据库连接之后,是可以直接通过脚本进行导出数据库实体类了,但是这个导出的实体类比较简陋,需要进行修改比较多,或是需要自己进行修改生成脚本语句如:
通过生成POJOs.clj即可导出实体类。
需要选一下实体类放置的地方。
效果如下:
但是以上的实体类没有带注解。那么我们通过项目中用到休眠,或是JPA需要加注解怎么办,总不能一个个注解加上去吧.idea当然不会这么干啦。
使用IntelliJ IDEA快编码速度:我们程序员的工作不是写程序,而是写程序解决问题。那我们删了之前生成的实体类。我们重新生成一份带注解的实体类。
第三步:配置休眠文件。
如果没有配置该配置文件,想法则没有显示出生成实体类的工具选项。
配置一下Hibernate的配置文件。
在资源文件下新建一个hibernate.cfg.xml的配置文件。并输入以下内容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?
xml
version
=
'1.0'
encoding
=
'utf-8'
?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name
=
"connection.driver_class"
>
com.mysql.jdbc.Driver
</property>
<property
name
=
"connection.url"
>
jdbc:mysql://localhost/test
</property>
<property
name
=
"connection.username"
>
root
</property>
<property
name
=
"connection.password"
>
123456
</property>
<!-- JDBC connection pool (use the built-in) -->
<!--
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property
name
=
"dialect"
>
org.hibernate.dialect.MySQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property
name
=
"current_session_context_class"
>
thread
</property>
<!-- Echo all executed SQL to stdout -->
<property
name
=
"show_sql"
>
true
</property>
<!-- Drop and re-create the database schema on startup -->
<!--
<property name="hbm2ddl.auto">update</property>
-->
</session-factory>
</hibernate-configuration>
|
如图:
第四步:调出想法实体类生成工具。
调出生成实体类的配置工具
保存后。在主面板左侧有持久性,在hibernate图标上点击右键 - 生成持久性映射 - 按数据库方案。
一开始是没有选中数据源的。
配置选项
(1)数据源选择
(2)生成实体类的位置
(3)实体类的前缀和后缀
(4)可以全选表,或是全不选表
(5)可以生成休眠的实体类对应的XML文件
(6)展开表之后可以修改对应之间的类型。
第五步:选中需要执行的数据库表。
第六步:查看导出的效果。
生成过程
导出的结果
可以查看其中的一个实体类,看看效果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package
com
.
souvc
.
entity
;
import
javax
.
persistence
.
Basic
;
import
javax
.
persistence
.
Column
;
import
javax
.
persistence
.
Entity
;
import
javax
.
persistence
.
Table
;
/**
* Created by Administrator on 2017/3/22.
*/
@Entity
@Table
(
name
=
"authorities"
,
schema
=
"test"
,
catalog
=
""
)
public
class
SouvcAuthoritiesEntity
{
private
String
username
;
private
String
authority
;
@Basic
@Column
(
name
=
"username"
,
nullable
=
false
,
length
=
50
)
public
String
getUsername
(
)
{
return
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
@Basic
@Column
(
name
=
"authority"
,
nullable
=
false
,
length
=
50
)
public
String
getAuthority
(
)
{
return
authority
;
}
public
void
setAuthority
(
String
authority
)
{
this
.
authority
=
authority
;
}
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
(
)
!=
o
.
getClass
(
)
)
return
false
;
SouvcAuthoritiesEntity
that
=
(
SouvcAuthoritiesEntity
)
o
;
if
(
username
!=
null
?
!
username
.
equals
(
that
.
username
)
:
that
.
username
!=
null
)
return
false
;
if
(
authority
!=
null
?
!
authority
.
equals
(
that
.
authority
)
:
that
.
authority
!=
null
)
return
false
;
return
true
;
}
@Override
public
int
hashCode
(
)
{
int
result
=
username
!=
null
?
username
.
hashCode
(
)
:
0
;
result
=
31
*
result
+
(
authority
!=
null
?
authority
.
hashCode
(
)
:
0
)
;
return
result
;
}
}
|
Hibernate的主配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<?
xml
version
=
'1.0'
encoding
=
'utf-8'
?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name
=
"connection.driver_class"
>
com.mysql.jdbc.Driver
</property>
<property
name
=
"connection.url"
>
jdbc:mysql://localhost:3306/test
</property>
<!-- JDBC connection pool (use the built-in) -->
<!--
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property
name
=
"dialect"
>
org.hibernate.dialect.MySQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property
name
=
"current_session_context_class"
>
thread
</property>
<!-- Echo all executed SQL to stdout -->
<property
name
=
"show_sql"
>
true
</property>
<mapping
class
=
"com.souvc.entity.SouvcAuthoritiesEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcAuthoritiesEntity.hbm.xml"
/>
<mapping
resource
=
"com/souvc/entity/SouvcCustomEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcCustomEntity"
/>
<mapping
class
=
"java.lang.String"
/>
<mapping
resource
=
"java/lang/java.lang.String.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcDataDictionaryEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcDataDictionaryEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcDataDictionaryListEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcDataDictionaryListEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcEmailAccountInfoEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcEmailAccountInfoEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcEmailInfoEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcEmailInfoEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcPermissionEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcPermissionEntity.hbm.xml"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcRoleEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcRoleEntity"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcRolePermissionsEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcRolePermissionsEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcUserEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcUserEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcUserLoginLogsEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcUserLoginLogsEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRcUserRoleEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRcUserRoleEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcRoleEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcRoleEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcUserEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcUserEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcUserRoleEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcUserRoleEntity.hbm.xml"
/>
<mapping
class
=
"com.souvc.entity.SouvcUsersEntity"
/>
<mapping
resource
=
"com/souvc/entity/SouvcUsersEntity.hbm.xml"
/>
<!-- Drop and re-create the database schema on startup -->
<!--
<property name="hbm2ddl.auto">update</property>
-->
</session-factory>
</hibernate-configuration>
|
其他配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?
xml
version
=
'1.0'
encoding
=
'utf-8'
?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name
=
"com.souvc.entity.SouvcAuthoritiesEntity"
table
=
"authorities"
schema
=
"test"
>
<property
name
=
"username"
>
<column
name
=
"username"
sql-type
=
"varchar(50)"
length
=
"50"
/>
</property>
<property
name
=
"authority"
>
<column
name
=
"authority"
sql-type
=
"varchar(50)"
length
=
"50"
/>
</property>
</class>
</hibernate-mapping>
|
第七步:修正。
如果还没有符合项目的要求,那么我们可以自己进行修改一下。