{"id":1781,"date":"2023-03-26T22:33:08","date_gmt":"2023-03-26T14:33:08","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1781"},"modified":"2023-04-23T21:17:05","modified_gmt":"2023-04-23T13:17:05","slug":"use-of-jpa-in-spring-boot","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/use-of-jpa-in-spring-boot\/","title":{"rendered":"Spring Boot \u4e2d JPA \u7684\u4f7f\u7528"},"content":{"rendered":"<h2>JPA\u7b80\u4ecb<\/h2>\n<p><code>JPA<\/code>\u5168\u79f0<code>Java Persistence API<\/code>\uff0cJPA\u901a\u8fc7JDK 5.0\u6ce8\u89e3\u6216XML\u63cf\u8ff0\u5bf9\u8c61\uff0d\u5173\u7cfb\u8868\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5e76\u5c06\u8fd0\u884c\u671f\u7684\u5b9e\u4f53\u5bf9\u8c61\u6301\u4e45\u5316\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<h2>\u9879\u76ee\u4f9d\u8d56<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;mysql&lt;\/groupId&gt;\n    &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\n    &lt;version&gt;6.0.6&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>\u9879\u76ee\u914d\u7f6e<\/h2>\n<pre><code>#\u901a\u7528\u6570\u636e\u6e90\u914d\u7f6e\nspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver\nspring.datasource.url=jdbc:mysql:\/\/127.0.0.1:3306\/test?charset=utf8mb4&amp;useSSL=false\nspring.datasource.username=springboot\nspring.datasource.password=springboot\n# Hikari \u6570\u636e\u6e90\u4e13\u7528\u914d\u7f6e\nspring.datasource.hikari.maximum-pool-size=20\nspring.datasource.hikari.minimum-idle=5\n# JPA \u76f8\u5173\u914d\u7f6e\nspring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect\nspring.jpa.show-sql=true\nspring.jpa.hibernate.ddl-auto=create<\/code><\/pre>\n<ul>\n<li><code>spring.jpa.show-sql=true<\/code> \u914d\u7f6e\u5728\u65e5\u5fd7\u4e2d\u6253\u5370\u51fa\u6267\u884c\u7684 SQL \u8bed\u53e5\u4fe1\u606f<\/li>\n<li><code>spring.jpa.hibernate.ddl-auto=create<\/code> \u914d\u7f6e\u6307\u660e\u5728\u7a0b\u5e8f\u542f\u52a8\u7684\u65f6\u5019\u8981\u5220\u9664\u5e76\u4e14\u521b\u5efa\u5b9e\u4f53\u7c7b\u5bf9\u5e94\u7684\u8868\u3002\u8fd9\u4e2a\u53c2\u6570\u5f88\u5371\u9669\uff0c\u56e0\u4e3a\u4f1a\u628a\u5bf9\u5e94\u7684\u8868\u5220\u9664\u6389\u7136\u540e\u91cd\u5efa\u3002\u6240\u4ee5\u5343\u4e07\u4e0d\u8981\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528\u3002\u53ea\u6709\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\uff0c\u4e00\u5f00\u59cb\u521d\u59cb\u5316\u6570\u636e\u5e93\u7ed3\u6784\u7684\u65f6\u5019\u624d\u80fd\u4f7f\u7528\u4e00\u6b21\u3002<\/li>\n<li><code>spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect<\/code> \u5728 Srping Boot 2.0 \u7248\u672c\u4e2d\uff0c<code>Hibernate<\/code>\u521b\u5efa\u6570\u636e\u8868\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\u5b58\u50a8\u5f15\u64ce\u9009\u62e9\u7684\u662f<code>MyISAM<\/code>\uff08\u4e4b\u524d\u597d\u50cf\u662f<code>InnoDB<\/code>\uff0c\u8fd9\u70b9\u6bd4\u8f83\u8be1\u5f02\uff09\u3002\u8fd9\u4e2a\u53c2\u6570\u662f\u5728\u5efa\u8868\u7684\u65f6\u5019\uff0c\u5c06\u9ed8\u8ba4\u7684\u5b58\u50a8\u5f15\u64ce\u5207\u6362\u4e3a<code>InnoDB<\/code>\u7528\u7684\u3002<\/li>\n<\/ul>\n<h2>\u6570\u636e\u5b9e\u4f53\u7c7b<\/h2>\n<p>\u6570\u636e\u5e93\u5b9e\u4f53\u7c7b\u662f\u4e00\u4e2a<code>POJO Bean<\/code>\u5bf9\u8c61\u3002\u8fd9\u91cc\u6211\u4eec\u5148\u5efa\u7acb\u4e00\u4e2a<code>UserDO<\/code>\u7684\u6570\u636e\u5e93\u5b9e\u4f53\u3002\u6570\u636e\u5e93\u5b9e\u4f53\u7684\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">import javax.persistence.*;\n\n@Data\n@Entity\n@Table(name = &quot;AUTH_USER&quot;)\npublic class UserDO {\n    @Id\n    private Long id;\n    @Column(length = 32)\n    private String name;\n    @Column(length = 32)\n    private String account;\n    @Column(length = 64)\n    private String pwd;\n}<\/code><\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>@Entity<\/code> \u662f\u4e00\u4e2a\u5fc5\u9009\u7684\u6ce8\u89e3\uff0c\u58f0\u660e\u8fd9\u4e2a\u7c7b\u5bf9\u5e94\u4e86\u4e00\u4e2a\u6570\u636e\u5e93\u8868<\/li>\n<li><code>@Table(name = &quot;AUTH_USER&quot;)<\/code> \u662f\u4e00\u4e2a\u53ef\u9009\u7684\u6ce8\u89e3\u3002\u58f0\u660e\u4e86\u6570\u636e\u5e93\u5b9e\u4f53\u5bf9\u5e94\u7684\u8868\u4fe1\u606f\uff0c\u5305\u62ec\u8868\u540d\u79f0\u3001\u7d22\u5f15\u4fe1\u606f\u7b49\u3002\u8fd9\u91cc\u58f0\u660e\u8fd9\u4e2a\u5b9e\u4f53\u7c7b\u5bf9\u5e94\u7684\u8868\u540d\u662f<code>AUTH_USER<\/code>\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\uff0c\u5219\u8868\u540d\u548c\u5b9e\u4f53\u7684\u540d\u79f0\u4fdd\u6301\u4e00\u81f4<\/li>\n<li><code>@Id<\/code> \u6ce8\u89e3\u58f0\u660e\u4e86\u5b9e\u4f53\u552f\u4e00\u6807\u8bc6\u5bf9\u5e94\u7684\u5c5e\u6027<\/li>\n<li><code>@Column(length = 32)<\/code> \u7528\u6765\u58f0\u660e\u5b9e\u4f53\u5c5e\u6027\u7684\u8868\u5b57\u6bb5\u7684\u5b9a\u4e49\u3002\u9ed8\u8ba4\u7684\u5b9e\u4f53\u6bcf\u4e2a\u5c5e\u6027\u90fd\u5bf9\u5e94\u4e86\u8868\u7684\u4e00\u4e2a\u5b57\u6bb5\u3002\u5b57\u6bb5\u7684\u540d\u79f0\u9ed8\u8ba4\u548c\u5c5e\u6027\u540d\u79f0\u4fdd\u6301\u4e00\u81f4\uff08\u5e76\u4e0d\u4e00\u5b9a\u76f8\u7b49\uff09\u3002\u5b57\u6bb5\u7684\u7c7b\u578b\u6839\u636e\u5b9e\u4f53\u5c5e\u6027\u7c7b\u578b\u81ea\u52a8\u63a8\u65ad\u3002\u8fd9\u91cc\u4e3b\u8981\u662f\u58f0\u660e\u4e86\u5b57\u7b26\u5b57\u6bb5\u7684\u957f\u5ea6\u3002\u5982\u679c\u4e0d\u8fd9\u4e48\u58f0\u660e\uff0c\u5219\u7cfb\u7edf\u4f1a\u91c7\u7528<code>255<\/code>\u4f5c\u4e3a\u8be5\u5b57\u6bb5\u7684\u957f\u5ea6<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u914d\u7f6e\u5168\u90e8\u6b63\u786e\uff0c\u5219\u6b64\u65f6\u8fd0\u884c\u8fd9\u4e2a\u9879\u76ee\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u770b\u5230\u65e5\u5fd7\u4e2d\u5982\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code>Hibernate: drop table if exists auth_user\nHibernate: create table auth_user (id bigint not null, account varchar(32), name varchar(32), pwd varchar(64), primary key (id)) engine=InnoDB<\/code><\/pre>\n<p>\u7cfb\u7edf\u5c06\u81ea\u52a8\u521b\u5efa\u8868\u53ca\u8868\u7ed3\u6784<\/p>\n<h2>\u5b9e\u73b0\u6301\u4e45\u5c42\u670d\u52a1<\/h2>\n<p>\u5728<code>Spring Data JPA<\/code>\u7684\u4e16\u754c\u91cc\uff0c\u5b9e\u73b0\u4e00\u4e2a\u6301\u4e45\u5c42\u7684\u670d\u52a1\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u4e8b\u60c5\u3002\u4ee5\u4e0a\u9762\u7684<code>UserDO<\/code>\u5b9e\u4f53\u5bf9\u8c61\u4e3a\u4f8b\uff0c\u8981\u5b9e\u73b0\u4e00\u4e2a\u589e\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\u3001\u67e5\u8be2\u529f\u80fd\u7684\u6301\u4e45\u5c42\u670d\u52a1\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u58f0\u660e\u4e00\u4e2a\u63a5\u53e3\uff0c\u8fd9\u4e2a\u63a5\u53e3\u7ee7\u627f<code>org.springframework.data.repository.Repository&lt;T, ID&gt;<\/code>\u63a5\u53e3\u6216\u8005\u5176\u5b50\u63a5\u53e3\u5c31\u884c\u3002\u8fd9\u91cc\u4e3a\u4e86\u529f\u80fd\u7684\u5b8c\u5907\uff0c\u6211\u4eec\u7ee7\u627f<code>org.springframework.data.jpa.repository.JpaRepository&lt;T, ID&gt;<\/code>\u63a5\u53e3\u3002\u5176\u4e2d<code>T<\/code>\u6570\u636e\u5e93\u5b9e\u4f53\u7c7b\uff0c<code>ID<\/code>\u662f\u6570\u636e\u5e93\u5b9e\u4f53\u7c7b\u7684\u4e3b\u952e\u3002<\/p>\n<p>\u7136\u540e\u518d\u7b80\u5355\u7684\u5728\u8fd9\u4e2a\u63a5\u53e3\u4e0a\u589e\u52a0\u4e00\u4e2a<code>@Repository<\/code>\u6ce8\u89e3\u5373\u53ef<\/p>\n<pre><code class=\"language-java\">import org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.stereotype.Repository;\n\n\/**\n * \u7528\u6237\u670d\u52a1\u6570\u636e\u63a5\u53e3\u7c7b\n *\/\n@Repository\npublic interface UserDao extends JpaRepository&lt;UserDO, Long&gt; {\n}<\/code><\/pre>\n<p>\u4e00\u884c\u4ee3\u7801\u4e5f\u4e0d\u7528\u5199\u3002\u9488\u5bf9<code>UserDO<\/code>\u8fd9\u4e2a\u5b9e\u4f53\u7c7b\uff0c\u6211\u4eec\u5df2\u7ecf\u62e5\u6709<code>JpaRepository<\/code>\u63a5\u53e3\u7684\u6240\u6709\u529f\u80fd<\/p>\n<h2>\u6d4b\u8bd5\u6570\u636e<\/h2>\n<pre><code class=\"language-java\">import org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.context.junit4.SpringRunner;\n\nimport java.util.Optional;\n\n@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class UserDOTest {\n\n    @Autowired\n    private UserDao userDao;\n\n    @Before\n    public void before() {\n        UserDO userDO = new UserDO();\n        userDO.setId(1L);\n        userDO.setName(&quot;\u98ce\u6e05\u626c&quot;);\n        userDO.setAccount(&quot;fengqy&quot;);\n        userDO.setPwd(&quot;123456&quot;);\n        userDao.save(userDO);\n        userDO = new UserDO();\n        userDO.setId(3L);\n        userDO.setName(&quot;\u4e1c\u65b9\u4e0d\u8d25&quot;);\n        userDO.setAccount(&quot;bubai&quot;);\n        userDO.setPwd(&quot;123456&quot;);\n        userDao.save(userDO);\n        userDO.setId(5L);\n        userDO.setName(&quot;\u5411\u95ee\u5929&quot;);\n        userDO.setAccount(&quot;wentian&quot;);\n        userDO.setPwd(&quot;123456&quot;);\n        userDao.save(userDO);\n    }\n\n    @Test\n    public void testAdd() {\n        UserDO userDO = new UserDO();\n        userDO.setId(2L);\n        userDO.setName(&quot;\u4efb\u6211\u884c&quot;);\n        userDO.setAccount(&quot;renwox&quot;);\n        userDO.setPwd(&quot;123456&quot;);\n        userDao.save(userDO);\n        userDO = new UserDO();\n        userDO.setId(4L);\n        userDO.setName(&quot;\u4ee4\u72d0\u51b2&quot;);\n        userDO.setAccount(&quot;linghuc&quot;);\n        userDO.setPwd(&quot;123456&quot;);\n        userDao.save(userDO);\n    }\n\n    @After\n    public void after() {\n        userDao.deleteById(1L);\n        userDao.deleteById(3L);\n        userDao.deleteById(5L);\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u91c7\u7528<code>Junit<\/code>\u6765\u6267\u884c\u6d4b\u8bd5\u7528\u4f8b\u3002<code>@Before<\/code>\u6ce8\u89e3\u5728\u6d4b\u8bd5\u7528\u4f8b\u4e4b\u524d\u6267\u884c\u51c6\u5907\u7684\u4ee3\u7801\u3002\u8fd9\u91cc\u5148\u63d2\u5165\u4e09\u4e2a\u7528\u6237\u4fe1\u606f\u3002\u6267\u884c\u8be5\u6d4b\u8bd5\uff0c\u67e5\u770b\u6570\u636e\u5e93\u5373\u53ef\u770b\u5230\u6570\u636e\u5e93\u4e2d\u6709\u4e86<code>5<\/code>\u6761\u8bb0\u5f55<\/p>\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6d4b\u8bd5\u7528\u4f8b\u9a8c\u8bc1\u901a\u8fc7\u6807\u8bc6\u67e5\u627e\u5bf9\u8c61\u529f\u80fd\uff0c\u9a8c\u8bc1\u6240\u6709\u6570\u636e\u67e5\u8be2\u529f\u80fd\u7684\u6b63\u786e\u6027\uff0c\u67e5\u8be2\u529f\u80fd\u751a\u81f3\u53ef\u4ee5\u8fdb\u884c\u6392\u5e8f\u548c\u5206\u9875<\/p>\n<pre><code class=\"language-java\">@Test\npublic void testLocate() {\n    Optional&lt;UserDO&gt; userDOOptiona = userDao.findById(1L);\n    if (userDOOptiona.isPresent()) {\n        UserDO userDO = userDOOptiona.get();\n        System.out.println(&quot;name = &quot; + userDO.getName());\n        System.out.println(&quot;account = &quot; + userDO.getAccount());\n    }\n}\n\n@Test\npublic void testFindAll() {\n    List&lt;UserDO&gt; userDOList = userDao.findAll(new Sort(Sort.Direction.DESC,&quot;account&quot;));\n    for (UserDO userDO : userDOList) {\n        System.out.println(&quot;name = &quot; + userDO.getName());\n        System.out.println(&quot;account = &quot; + userDO.getAccount());\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u6240\u505a\u7684\u5168\u90e8\u4e8b\u60c5\u4ec5\u4ec5\u662f\u5728<code>Sping Boot<\/code>\u5de5\u7a0b\u91cc\u9762\u589e\u52a0\u6570\u636e\u5e93\u914d\u7f6e\u4fe1\u606f\uff0c\u58f0\u660e\u4e00\u4e2a<code>UserDO<\/code>\u7684\u6570\u636e\u5e93\u5b9e\u4f53\u5bf9\u8c61\uff0c\u7136\u540e\u58f0\u660e\u4e00\u4e2a\u6301\u4e45\u5c42\u7684\u63a5\u53e3\uff0c\u6539\u63a5\u53e3\u7ee7\u627f\u81ea<code>org.springframework.data.jpa.repository.JpaRepository&lt;T, ID&gt;<\/code>\u63a5\u53e3\u3002\u7136\u540e\uff0c\u7cfb\u7edf\u5c31\u81ea\u52a8\u62e5\u6709\u4e86\u4e30\u5bcc\u7684\u589e\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\u3001\u67e5\u8be2\u529f\u80fd\u3002\u67e5\u8be2\u529f\u80fd\u751a\u81f3\u8fd8\u62e5\u6709\u4e86\u6392\u5e8f\u548c\u5206\u9875\u7684\u529f\u80fd\u3002<\/p>\n<p>\u8fd9\u5c31\u662f<code>JPA<\/code>\u7684\u5f3a\u5927\u4e4b\u5904\u3002\u9664\u4e86\u8fd9\u4e9b\u63a5\u53e3\u5916\uff0c\u7528\u6237\u8fd8\u4f1a\u6709\u5176\u4ed6\u7684\u4e00\u4e9b\u9700\u6c42\uff0c<code>JPA<\/code>\u4e5f\u4e00\u6837\u53ef\u4ee5\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<h2>\u6269\u5c55\u67e5\u8be2<\/h2>\n<h3>\u6839\u636e\u5c5e\u6027\u6765\u67e5\u8be2<\/h3>\n<p>\u5982\u679c\u60f3\u8981\u6839\u636e\u5b9e\u4f53\u7684\u67d0\u4e2a\u5c5e\u6027\u6765\u8fdb\u884c\u67e5\u8be2\u6211\u4eec\u53ef\u4ee5\u5728<code>UserDao<\/code>\u63a5\u53e3\u4e2d\u8fdb\u884c\u58f0\u660e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u6839\u636e\u5b9e\u4f53\u7684<code>account<\/code>\u8fd9\u4e2a\u5c5e\u6027\u6765\u8fdb\u884c\u67e5\u8be2\uff08\u5728\u767b\u5f55\u529f\u80fd\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u7528\u5230\uff09\u3002\u6211\u4eec\u5728<code>UserDao<\/code>\u4e2d\u589e\u52a0\u4e00\u4e2a\u63a5\u53e3\u58f0\u660e\u5373\u53ef<\/p>\n<pre><code class=\"language-java\">UserDO findByAccount(String account);<\/code><\/pre>\n<p>\u7136\u540e\u589e\u52a0\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b<\/p>\n<pre><code class=\"language-java\">@Test\npublic void testFindByAccount() {\n    UserDO userDO = userDao.findByAccount(&quot;wentian&quot;);\n    if (userDO != null) {\n        System.out.println(&quot;name = &quot; + userDO.getName());\n        System.out.println(&quot;account = &quot; + userDO.getAccount());\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u4e4b\u540e\uff0c\u4f1a\u5728\u65e5\u5fd7\u4e2d\u6253\u5370\u51fa<\/p>\n<pre><code>name = \u5411\u95ee\u5929\naccount = wentian<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u5f3a\u5927\uff0c\u4e0d\u4ec5\u80fd\u591f\u652f\u6301\u5355\u4e2a\u5c5e\u6027\uff0c\u8fd8\u80fd\u652f\u6301\u591a\u4e2a\u5c5e\u6027\u7ec4\u5408\u3002\u4f8b\u5982\u5982\u679c\u6211\u4eec\u60f3\u67e5\u627e\u8d26\u53f7\u548c\u5bc6\u7801\u540c\u65f6\u6ee1\u8db3\u67e5\u8be2\u6761\u4ef6\u7684\u63a5\u53e3\u3002\u90a3\u4e48\u6211\u4eec\u5728<code>UserDao<\/code>\u63a5\u53e3\u4e2d\u58f0\u660e<\/p>\n<pre><code class=\"language-java\">UserDO findByAccountAndPwd(String account, String pwd);<\/code><\/pre>\n<p>\u518d\u4f8b\u5982\uff0c\u6211\u4eec\u8981\u67e5\u8be2<code>id<\/code>\u5927\u4e8e\u67d0\u4e2a\u6761\u4ef6\u7684\u7528\u6237\u5217\u8868\uff0c\u5219\u53ef\u4ee5\u58f0\u660e\u5982\u4e0b\u7684\u63a5\u53e3<\/p>\n<pre><code class=\"language-java\">List&lt;UserDO&gt; findAllByIdGreaterThan(Long id);<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8bed\u53e5\u7ed3\u6784\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u8868\uff08JPA\u5173\u952e\u5b57\uff09\u8bf4\u660e<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5173\u952e\u5b57<\/th>\n<th style=\"text-align: left;\">\u65b9\u6cd5\u547d\u540d<\/th>\n<th style=\"text-align: left;\">sql where\u5b57\u53e5<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>And<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameAndPwd<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name= ? and pwd =?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>Or<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameOrSex<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name= ? or sex=?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>Is,Equals<\/code><\/td>\n<td style=\"text-align: left;\"><code>findById,findByIdEquals<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id= ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>Between<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdBetween<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id between ? and ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>LessThan<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdLessThan<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id &lt; ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>LessThanEquals<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdLessThanEquals<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id &lt;= ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>GreaterThan<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdGreaterThan<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id &gt; ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>GreaterThanEquals<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdGreaterThanEquals<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id &gt; = ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>After<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdAfter<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id &gt; ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>Before<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdBefore<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id &lt; ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>IsNull<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameIsNull<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name is null<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>isNotNull,NotNull<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameNotNull<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name is not null<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>Like<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameLike<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name like ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>NotLike<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameNotLike<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name not like ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>StartingWith<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameStartingWith<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name like &#039;?%&#039;<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>EndingWith<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameEndingWith<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name like &#039;%?&#039;<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>Containing<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameContaining<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name like &#039;%?%&#039;<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>OrderBy<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdOrderByXDesc<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id=? order by x desc<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>Not<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameNot<\/code><\/td>\n<td style=\"text-align: left;\"><code>where name &lt;&gt; ?<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>In<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdIn(Collection&lt;?&gt; c)<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id in (?)<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>NotIn<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByIdNotIn(Collection&lt;?&gt; c)<\/code><\/td>\n<td style=\"text-align: left;\"><code>where id not in (?)<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>True<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByAaaTue<\/code><\/td>\n<td style=\"text-align: left;\"><code>where aaa = true<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>False<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByAaaFalse<\/code><\/td>\n<td style=\"text-align: left;\"><code>where aaa = false<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>IgnoreCase<\/code><\/td>\n<td style=\"text-align: left;\"><code>findByNameIgnoreCase<\/code><\/td>\n<td style=\"text-align: left;\"><code>where UPPER(name)=UPPER(?)<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u81ea\u5b9a\u4e49\u67e5\u8be2<\/h3>\n<p>\u5982\u679c\u4e0a\u8ff0\u7684\u60c5\u51b5\u8fd8\u65e0\u6cd5\u6ee1\u8db3\u9700\u8981\u3002\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u901a\u8fc7<code>import org.springframework.data.jpa.repository.Query<\/code>\u6ce8\u89e3\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f8b\u5982\u6211\u4eec\u60f3\u67e5\u8be2\u540d\u79f0\u7b49\u4e8e\u67d0\u4e24\u4e2a\u540d\u5b57\u7684\u6240\u6709\u7528\u6237\u5217\u8868\uff0c\u5219\u58f0\u660e\u5982\u4e0b\u7684\u63a5\u53e3\u5373\u53ef<\/p>\n<pre><code class=\"language-java\">@Query(&quot;SELECT O FROM UserDO O WHERE O.name = :name1 OR O.name = :name2&quot;)\nList&lt;UserDO&gt; findTwoName(@Param(&quot;name1&quot;) String name1, @Param(&quot;name2&quot;) String name2);<\/code><\/pre>\n<p>\u8fd9\u91cc\u662f\u7528<code>PQL<\/code>\u7684\u8bed\u6cd5\u6765\u5b9a\u4e49\u4e00\u4e2a\u67e5\u8be2\u3002\u5176\u4e2d\u4e24\u4e2a\u53c2\u6570\u540d\u5b57\u7531\u8bed\u53e5\u4e2d\u7684<code>:<\/code>\u540e\u9762\u7684\u5b57\u7b26\u6765\u51b3\u5b9a<\/p>\n<p>\u5982\u679c\u4f60\u4e60\u60ef\u7f16\u5199<code>SQL<\/code>\u8bed\u53e5\u6765\u5b8c\u6210\u67e5\u8be2\uff0c\u8fd8\u53ef\u4ee5\u518d\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\u5b9e\u73b0<\/p>\n<pre><code class=\"language-java\">@Query(nativeQuery = true, value = &quot;SELECT * FROM AUTH_USER WHERE name = :name1 OR name = :name2&quot;)\nList&lt;UserDO&gt; findSQL(@Param(&quot;name1&quot;) String name1, @Param(&quot;name2&quot;) String name2);<\/code><\/pre>\n<p>\u8fd9\u91cc\u5728<code>@Query<\/code>\u6ce8\u89e3\u4e2d\u589e\u52a0\u4e00\u4e2a<code>nativeQuery = true<\/code>\u7684\u5c5e\u6027\uff0c\u5c31\u53ef\u4ee5\u91c7\u7528\u539f\u751f<code>SQL<\/code>\u8bed\u53e5\u7684\u65b9\u5f0f\u6765\u7f16\u5199\u67e5\u8be2\u3002<\/p>\n<h2>\u8054\u5408\u4e3b\u952e<\/h2>\n<p>\u4ece<code>org.springframework.data.jpa.repository.JpaRepository&lt;T, ID&gt;<\/code>\u63a5\u53e3\u5b9a\u4e49\u6765\u770b\uff0c\u6570\u636e\u5b9e\u4f53\u7684\u4e3b\u952e\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u5982\u679c\u4e00\u4e2a\u6570\u636e\u5e93\u7684\u8868\u7684\u4e3b\u952e\u662f\u4e24\u4e2a\u6216\u8005\u4e24\u4e2a\u4ee5\u4e0a\u5b57\u6bb5\u8054\u5408\u7ec4\u6210\u7684\u600e\u4e48\u89e3\u51b3\u5462\u3002<\/p>\n<p>\u6211\u4eec\u6269\u5145\u4e00\u4e0b\u524d\u9762\u7684\u573a\u666f\u3002\u5047\u5982\u6211\u4eec\u6709\u4e00\u4e2a\u89d2\u8272<code>Role<\/code>\u5bf9\u8c61\uff0c\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a\u4e00\u4e2a<code>id<\/code>\uff0c\u4e00\u4e2a<code>name<\/code>\uff0c\u5bf9\u5e94<code>auth_role<\/code>\u6570\u636e\u8868\uff0c\u540c\u65f6\u6709\u4e00\u4e2a\u89d2\u8272\u7528\u6237\u5173\u7cfb\u5bf9\u8c61<code>RoleUser<\/code>\uff0c\u8bf4\u660e\u89d2\u8272\u548c\u7528\u6237\u5bf9\u5e94\u5173\u7cfb\uff0c\u6709\u4e24\u4e2a\u5c5e\u6027<code>roleId<\/code>\u548c<code>userId<\/code>\uff0c\u5bf9\u5e94<code>auth_role_user<\/code>\u8868\u3002\u90a3\u4e48\u6211\u4eec\u9700\u8981\u58f0\u660e\u4e00\u4e2a<code>RoleDO<\/code>\u5bf9\u8c61\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">import javax.persistence.*;\n\n\/**\n * \u89d2\u8272\u5b9e\u4f53\u7c7b\n *\/\n @Data\n@Entity\n@Table(name = &quot;AUTH_ROLE&quot;)\npublic class RoleDO {\n    @Id\n    private Long id;\n    @Column(length = 32)\n    private String name;\n    @Column(length = 64)\n    private String note;\n}<\/code><\/pre>\n<p>\u5bf9\u4e8e\u6709\u591a\u4e2a\u5c5e\u6027\u4f5c\u4e3a\u8054\u5408\u4e3b\u952e\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u4e00\u822c\u8981\u65b0\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u4e3b\u952e\u7c7b\uff0c\u5b83\u7684\u5c5e\u6027\u548c\u6570\u636e\u5e93\u5b9e\u4f53\u4e3b\u952e\u7684\u5b57\u6bb5\u4e00\u6837\uff0c\u8981\u5b9e\u73b0<code>java.io.Serializable<\/code>\u63a5\u53e3\uff0c\u7c7b\u58f0\u660e\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">import java.io.Serializable;\n\n\/**\n * \u8054\u5408\u4e3b\u952e\u5bf9\u8c61\n *\/\npublic class RoleUserId implements Serializable {\n    private Long roleId;\n    private Long userId;\n}<\/code><\/pre>\n<p>\u540c\u6837\u7684\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a RoleUserDO \u5bf9\u8c61\u5982\u4e0b<\/p>\n<pre><code class=\"language-java\">import javax.persistence.*;\nimport java.io.Serializable;\n\n\/**\n * \u89d2\u8272\u7528\u6237\u5173\u7cfb\u5b9e\u4f53\u7c7b\n *\/\n@Entity\n@IdClass(RoleUserId.class)\n@Table(name = &quot;AUTH_ROLE_USER&quot;)\npublic class RoleUserDO  {\n    @Id\n    private Long roleId;\n    @Id\n    private Long userId;\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u56e0\u4e3a\u6570\u636e\u5b9e\u4f53\u7c7b\u548c\u6570\u636e\u5b9e\u4f53\u4e3b\u952e\u7c7b\u7684\u5c5e\u6027\u4e00\u6837\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5220\u9664\u6389\u8fd9\u4e2a\u6570\u636e\u5b9e\u4f53\u4e3b\u952e\u7c7b\uff0c\u7136\u540e\u5c06\u6570\u636e\u5b9e\u4f53\u7c7b\u7684\u4e3b\u952e\u7c7b\u58f0\u660e\u4e3a\u81ea\u5df1\u5373\u53ef\u3002\u5f53\u7136\uff0c\u81ea\u5df1\u4e5f\u8981\u5b9e\u73b0<code>java.io.Serializable<\/code>\u63a5\u53e3\u3002<\/p>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u5982\u679c\u8981\u67e5\u8be2\u67d0\u4e2a\u89d2\u8272\u4e0b\u7684\u6240\u6709\u7528\u6237\u5217\u8868\uff0c\u5c31\u53ef\u4ee5\u58f0\u660e\u5982\u4e0b\u7684\u63a5\u53e3<\/p>\n<pre><code class=\"language-java\">@Query(&quot;SELECT U FROM UserDO U, RoleUserDO RU WHERE U.id = RU.userId AND RU.roleId = :roleId&quot;)\nList&lt;UserDO&gt; findUsersByRole(@Param(&quot;roleId&quot;) Long roleId);<\/code><\/pre>\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u540c\u6837\u4f1a\u770b\u5230\u7cfb\u7edf\u81ea\u52a8\u5efa\u7acb<code>AUTH_ROLE<\/code>\u548c<code>AUTH_ROLE_USER<\/code>\u8868<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc<code>auth_role_user<\/code>\u8868\u4e2d\uff0c\u5c5e\u6027\u540d<code>userId<\/code>\u8f6c\u6362\u4e3a\u4e86<code>user_id<\/code>\uff0c<code>roleId<\/code>\u8f6c\u6362\u4e3a\u4e86<code>role_id<\/code><\/p>\n<p>\u5982\u679c\u6211\u4eec\u8981\u7528<code>SQL<\/code>\u8bed\u53e5\u7684\u65b9\u5f0f\u5b9e\u73b0\u4e0a\u9762\u7684\u529f\u80fd\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u628a\u8fd9\u4e2a\u63a5\u53e3\u58f0\u660e\u4fee\u6539\u4e3a\u4e0b\u9762\u7684\u5f62\u5f0f\uff1a<\/p>\n<pre><code class=\"language-java\">@Query(nativeQuery = true, &quot;SELECT U.* FROM AUTH_USER U, AUTH_ROLE_USER RU WHERE U.id = RU.user_id AND RU.role_id = :roleId&quot;)\nList&lt;UserDO&gt; findUsersByRole(@Param(&quot;roleId&quot;) Long roleId);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JPA\u7b80\u4ecb JPA\u5168\u79f0Java Persistence API\uff0cJPA\u901a\u8fc7JDK 5.0\u6ce8\u89e3\u6216XML\u63cf\u8ff0\u5bf9\u8c61\uff0d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[451],"class_list":["post-1781","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-jpa"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1781"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1781\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}