1.先来看一下项目的目录结构
2.在整合之前我们要在pom.xml中添加上依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.28</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
3.加上了必要的依赖后我们就要在application.properties进行丁配置
#配置数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=tiger
spring.datasource.url=jdbc:mysql://localhost:3306/jpa
#相当于数据库方言
spring.jpa.database=mysql
#是否显示sql语句
spring.jpa.show-sql=true
#格式化sql语句
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
4.实体类代码如下
package com.it.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private int userId;
@Column(name = "user_name",length = 20,unique = true,nullable = false)
private String userName;
@Column(name = "password",length = 20,nullable = false)
private String password;
private String addr;
}
5.下面是dao层代码
package com.it.dao;
import com.it.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.io.Serializable;
import java.util.List;
public interface IUserDao extends JpaRepository<User, Serializable> {
List<User> findByPassword(String password);
}
6.service层代码
package com.it.service;
import com.it.entity.User;
import java.util.List;
public interface IUserService {
List<User> getAllUsers();
void saveUsers(User user);
List<User> findByPassword(String password);
}
package com.it.service.impl;
import com.it.dao.IUserDao;
import com.it.entity.User;
import com.it.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;
@Override
public List<User> getAllUsers() {
return userDao.findAll();
}
@Override
public void saveUsers(User user) {
userDao.saveAndFlush(user);
}
@Override
public List<User> findByPassword(String password) {
return userDao.findByPassword(password);
}
}
7.测式代码如下
package com.it.service;
import com.it.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
public class TestUserService {
@Resource
private IUserService userService;
@Test
public void testSaveUser(){
User user = new User(0,"wen","tiger","安徽安庆");
userService.saveUsers(user);
}
@Test
public void findUserByPassword(){
List<User> tiger = userService.findByPassword("tiger");
System.out.println(tiger);
}
}
我这里给出了findUserByPassword()测试方法的在控制台打印出来的sql,如果你不在application.properties配置文件中加入spring.jpa.properties.hibernate.format_sql=true就不会像下面一样格式.会显示在一行,这样不方便查看.
Hibernate:
select
user0_.user_id as user_id1_0_,
user0_.addr as addr2_0_,
user0_.password as password3_0_,
user0_.user_name as user_nam4_0_
from
user user0_
where
user0_.password=?
[User(userId=2, userName=wen, password=tiger, addr=安徽安庆)]