idea链接数据库自动生成实体类

Intellij IDEA 如何通过数据库表生成带注解的实体类图文详细教程

Intellij IDEA 如何通过数据库表生成带注解的实体类

Contents

  1. 第一步:新建一个Maven项目。项目的名称为JpaDemo。
  2. 第二步:配置数据库连接。
  3. 第三步:配置hibernate文件。
  4. 第四步:调出idea实体类生成工具。
  5. 第五步:选中需要执行的数据库表。
  6. 第六步:查看导出的效果。
  7. 第七步:修正。

第一步:新建一个Maven项目。项目的名称为JpaDemo。

我这里是通过idea插件对应的spring项目生成器https://start.spring.io,直接生成项目。如图:

下一步,修改成对应项目的基本信息。如图:

选择相应的依赖jar包。

选择项目的位置

完成创建

温馨提示,之前需要安装好maven。

第二步:配置数据库连接。

选择Mysql。

配置数据库基本信息

其实配置了这个数据库连接之后,是可以直接通过脚本进行导出数据库实体类了,但是这个导出的实体类比较简陋,需要进行修改比较多,或是需要自己进行修改生成脚本语句。如:

通过generate POJOs.clj即可导出实体类。

需要选一下实体类放置的地方。

效果如下:

但是以上的实体类没有带注解。那么我们通过项目中用到hibernate,或是jpa需要加注解怎么办,总不能一个个注解加上去吧。idea当然不会这么干啦。

使用IntelliJ IDEA快编码速度:我们程序员的工作不是写程序,而是写程序解决问题。那我们删了之前生成的实体类。我们重新生成一份带注解的实体类。

第三步:配置hibernate文件。

如果没有配置该配置文件,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>

如图:

第四步:调出idea实体类生成工具。

调出生成实体类的配置工具

保存后。在主面板左侧有persistence,在hibernate图标上点击右键-Generate Persistence Mapping-By Database Scheme。

一开始是没有选中数据源的。

配置选项

(1)数据源选择

(2)生成实体类的位置

(3)实体类的前缀和后缀

(4)可以全选表,或是全不选表

(5)可以生成hibernate的实体类对应的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>

猜你喜欢

转载自blog.csdn.net/aa953629866/article/details/80749033
今日推荐