一、项目说明
项目环境:jdk1.8+tomcat8+idea2018+mysql5.7+navicat
源代码github地址:
实现目标:通过整合jpa,实现数据库的增,删,改,查操作。
二、整合说明
(1)通过idea等方式创建springBoot模板项目
(2)在pom.xml文件中添加相关依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.0.28
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
(3)在 application.properties中添加数据库和jpa配置
#mysql数据库配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/root?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=root #jpa配置 #控制台显示sql spring.jpa.show-sql=true #数据库类型 spring.jpa.database=mysql #项目启动时根据实体类更新数据库中的表 spring.jpa.hibernate.ddl-auto=update #数据库方言 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
(4)在navicat中创建root数据库
注:不用建表,表会通过启动项目时自动创建
(5)在entity中创建User
package com.example.entity;
import javax.persistence.*;
/**
* 用户实体类
*/
//name对应数据库中的表
@Entity(name = "user")
public class User {
@Id //对应数据库id
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键自动生成策略
private Integer id;
@Column(name = "name") //对应数据库普通字段
private String name;
@Column(name = "sex")
private String sex;
@Column(name = "age")
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(Integer id, String name, String sex, Integer age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
(6)在service下创建UserDao
package com.example.service;
import com.example.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
/**
* 用户dao
*/
//JpaRepository中包含了很多常用的方法,如分页,保存,删除等
public interface UserDao extends JpaRepository {
/**
* 根据用户id查询用户
* @param id
* @return
*/
User getUserByIdEquals(Integer id);
//自定义sql
/**
* 根据性别和年龄段查询用户(参数绑定方式一)
* @param age
* @return
*/
@Query("select u from user u where u.sex=:sex and u.age>:age")
List getUserBySexAndAge(@Param("sex") String sex, @Param("age") Integer age);
/**
* 根据姓名和年龄段查询用户(参数绑定方式二)
* @param name
* @param age
* @return
*/
@Query("select u from user u where u.name like %?1% and u.age>?2")
List getUserByNameAndAge(String name, Integer age);
}
(7)在impl下创建UserService
package com.example.service.impl;
import com.example.entity.User;
import com.example.service.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 用户service
*/
@Service
public class UserService {
//注入用户dao
@Autowired
UserDao userDao;
/**
* 新增用户
* @param user
*/
public void addUser(User user){
userDao.save(user);
}
/**
* 修改用户
* @param user
*/
public void updateUser(User user){
userDao.save(user);
}
/**
* 删除用户
* @param id
*/
public void deleteUser(Integer id){
userDao.deleteById(id);
}
/**
* 根据用户id查询用户
* @param id
* @return
*/
public User getUserByIdEquals(Integer id){
return userDao.getUserByIdEquals(id);
}
/**
* 根据性别和年龄段查询用户
* @param sex
* @param age
* @return
*/
public List getUserBySexAndAge(String sex, Integer age){
return userDao.getUserBySexAndAge(sex, age);
}
/**
* 根据姓名和年龄段查询用户
* @param name
* @param age
* @return
*/
public List getUserByNameAndAge(String name, Integer age){
return userDao.getUserByNameAndAge(name, age);
}
/**
* 分页查询所有用户
* @param pageable
* @return
*/
public Page getUserListByPage(Pageable pageable){
return userDao.findAll(pageable);
}
}
(8)在controller下创建UserController
package com.example.controller;
import com.example.entity.User;
import com.example.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 用户controller
*/
@RestController
public class UserController {
@Autowired
UserService userService;
/**
* 新增用户
*/
@GetMapping("/addUser")
public void addUser(){
User user = new User();
user.setName("张三");
user.setSex("男");
user.setAge(18);
userService.addUser(user);
}
/**
* 修改用户
*/
@GetMapping("/updateUser")
public void updateUser(){
User user = new User();
user.setId(1);
user.setName("张三1");
user.setSex("男1");
user.setAge(181);
userService.updateUser(user);
}
/**
* 删除用户
*/
@GetMapping("/deleteUser")
public void deleteUser(){
userService.deleteUser(1);
}
/**
* 根据用户id查询用户
*/
@GetMapping("/getUserByIdEquals")
public void getUserByIdEquals(){
User user = userService.getUserByIdEquals(2);
System.out.println("用户列表==============>" + user);
}
/**
* 根据性别和年龄段查询用户
*/
@GetMapping("/getUserBySexAndAge")
public void getUserBySexAndAge(){
List userList = userService.getUserBySexAndAge("男", 10);
System.out.println("用户列表==============>" + userList);
}
/**
* 根据姓名和年龄段查询用户
*/
@GetMapping("/getUserByNameAndAge")
public void getUserByNameAndAge(){
List userList = userService.getUserByNameAndAge("张三", 10);
System.out.println("用户列表==============>" + userList);
}
/**
* 分页查询所有用户
*/
@GetMapping("/getUserListByPage")
public void getUserListByPage(){
PageRequest pageable = PageRequest.of(0,5);
Page page = userService.getUserListByPage(pageable);
System.out.println("总页数=============>"+page.getTotalPages());
System.out.println("查询结果=============>"+page.getContent());
}
}
(9)测试接口
注:这里通过在浏览器中改变请求接口的地址测试接口,相应的测试结果会在控制台和数据库中展现。