项目结构
pom
-
<project xmlns="http://maven.apache.org/POM/4.0.0"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-
<modelVersion>4.0.0
</modelVersion>
-
<groupId>com.pwl.springboot-jpa
</groupId>
-
<artifactId>springboot-jpa
</artifactId>
-
<packaging>war
</packaging>
-
<version>0.0.1-SNAPSHOT
</version>
-
<name>Mybatis Maven Webapp
</name>
-
<url>http://maven.apache.org
</url>
-
<parent>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-parent
</artifactId>
-
<version>2.0.1.RELEASE
</version>
-
</parent>
-
<dependencies>
-
<dependency>
-
<groupId>junit
</groupId>
-
<artifactId>junit
</artifactId>
-
<scope>test
</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-data-jpa
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>mysql
</groupId>
-
<artifactId>mysql-connector-java
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-devtools
</artifactId>
-
<optional>true
</optional>
-
</dependency>
-
</dependencies>
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.apache.maven.plugins
</groupId>
-
<artifactId>maven-compiler-plugin
</artifactId>
-
<configuration>
-
<source>1.8
</source>
-
<target>1.8
</target>
-
</configuration>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
然后配置数据库连接
-
spring.datasource.url=jdbc:mysql:
//127.0.0.1:3306/springboot?useSSL=false
-
spring.datasource.username=root
-
spring.datasource.password=root
-
spring.datasource.driver-
class-name=com.mysql.jdbc.Driver
-
-
spring.jpa.properties.hibernate.hbm2ddl.auto=update
-
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
-
spring.jpa.show-sql=
true
实体类
-
package com.pwl.springboot.entity;
-
-
import javax.persistence.Entity;
-
import javax.persistence.Id;
-
import javax.persistence.Table;
-
-
@Entity
-
@Table(name=
"user")
//指定数据库表名
-
public
class User {
-
@Id
-
private String id;
-
private String name;
-
private String age;
-
public String getId() {
-
return id;
-
}
-
public void setId(String id) {
-
this.id = id;
-
}
-
public String getName() {
-
return name;
-
}
-
public void setName(String name) {
-
this.name = name;
-
}
-
public String getAge() {
-
return age;
-
}
-
public void setAge(String age) {
-
this.age = age;
-
}
-
}
service层
UserService接口
-
package com.pwl.springboot.service;
-
-
import com.pwl.springboot.entity.User;
-
-
public
interface UserService {
-
public User getUserByname(String name);
-
}
UserServiceImpl实现类
-
package com.pwl.springboot.service.impl;
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Service;
-
-
import com.pwl.springboot.dao.UserDao;
-
import com.pwl.springboot.entity.User;
-
import com.pwl.springboot.service.UserService;
-
-
@Service
-
public
class UserServiceImpl implements UserService{
-
@Autowired
-
private UserDao userDao;
-
@Override
-
public User getUserByname(String name) {
-
User user = userDao.findByName(name);
-
return user;
-
}
-
-
}
dao
-
package com.pwl.springboot.dao;
-
-
import org.springframework.data.jpa.repository.JpaRepository;
-
-
import com.pwl.springboot.entity.User;
-
-
public
interface UserDao extends JpaRepository<User, String> {
-
public User findByName(String name);
-
}
主要就是继承了JpaRepository
具体的关键字,使用方法和生产成SQL如下表所示
Keyword | Sample | JPQL snippet |
---|---|---|
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age ⇐ ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection age) | … where x.age not in ?1 |
TRUE | findByActiveTrue() | … where x.active = true |
FALSE | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
最后就是controller
-
package
com
.pwl
.springboot
.controller;
-
-
import
org
.springframework
.beans
.factory
.annotation
.Autowired;
-
import
org
.springframework
.stereotype
.Controller;
-
import
org
.springframework
.web
.bind
.annotation
.RequestMapping;
-
import
org
.springframework
.web
.bind
.annotation
.ResponseBody;
-
import
com
.pwl
.springboot
.entity
.User;
-
import
com
.pwl
.springboot
.service
.UserService;
-
-
@
Controller
-
@RequestMapping(
"/")
-
public class HelloController {
-
@
Autowired
-
private UserService userService;
-
-
@
RequestMapping("
hello")
-
@ResponseBody
-
public String hello() {
-
return "
hello_world";
-
}
-
-
@
RequestMapping("
getUser")
-
@ResponseBody
-
public User getUser(String name) {
-
return
userService
.getUserByname(
name);
-
}
-
-
}
测试结果