springboot2.x整合JPA

项目结构

springboot2.x整合JPA_第1张图片

pom


	4.0.0
	com.pwl.springboot-jpa
	springboot-jpa
	war
	0.0.1-SNAPSHOT
	Mybatis Maven Webapp
	http://maven.apache.org
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
	
	
		
			junit
			junit
			test
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
			mysql
			mysql-connector-java
		
		
			org.springframework.boot
			spring-boot-devtools
			true
		
	
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
				
			
		
	


然后配置数据库连接

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 {
	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);
	}

}

 

测试结果

springboot2.x整合JPA_第2张图片

你可能感兴趣的:(SpringBoot)