Spring Boot Mysql 数据库操作示例
作者:空心菜的爱 时间:2024-01-19 10:22:28
本文默认你的开发环境.数据库已经安装好
想用使用数据库.我们需要现在pom文件中添加相应的依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imgod</groupId>
<artifactId>testjpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>TestJpa</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 添加Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们的配置文件进行数据库的配置
application.properties:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=imgod1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.jpa.properties.hibernate.hbm2ddl.auto=update
前面配置的是数据库信息,最后一行配置的是.如果我们定义的实体在数据库中没有对应的表的话.会帮我们自动创建
我们的实体类型
User.Java
package com.imgod.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotNull
private String email;
@NotNull
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
对应数据库中的user表,没有的话会自动创建
想对数据库进行操作,我们需要创建一个接口继承CrudRepository
UserDao.java
package com.imgod.dao;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.imgod.bean.User;
import java.lang.String;
import java.util.List;
@Transactional
@Repository
public interface UserDao extends CrudRepository<User, Integer> {
//jpa 方法名就是查询语句,只要规法写方法名一切就都可以完成(当然.有时候会造成方法名又臭又长)
User findByEmail(String email);//根据邮箱查询
List<User> findByName(String name);//根据用户名查询
//select * from test.users where email='imgod@qq.com' and name='imgod';
List<User> findByNameAndEmail(String name,String email);//根据用户名和邮箱进行查询
//select * from test.users where email='imgod@qq.com' and name='imgod4444' order by id desc;
List<User> findByNameAndEmailOrderByIdDesc(String name,String email);//根据用户名和邮箱进行查询,排序
//select * from test.users where email='imgod@qq.com' and name='imgod4444' order by id desc limit 2;
List<User> findTop2ByNameAndEmailOrderByIdDesc(String name,String email);//根据用户名和邮箱进行查询,排序,前两个
//根据邮箱进行分页查询
List<User> findByEmail(String email,Pageable pageable);//根据用户名和邮箱进行查询
}
实现CrudRepository我们就可以对数据库进行一些基础操作了
如果我们想要实现更多的操作.需要自己按照严格的命名规则为方法定义方法名
下面就是我们重要的控制器的实现了:
package com.imgod.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.imgod.bean.User;
import com.imgod.dao.UserDao;
@RestController
public class UserController {
@Autowired
private UserDao userDao;
/**
* 根据邮件去查找
*
* @param email
* @return
*/
@RequestMapping(value = "/findUserByEmail")
public Object getUserByEmail(String email) {
System.out.println("email:" + email);
User user = userDao.findByEmail(email);
if (null == user) {
return "暂无数据";
} else {
return user;
}
}
/**
* 获取所有的用户信息
*
* @return
*/
@RequestMapping(value = "/getall")
public Object getAllUser() {
List<User> list = (List<User>) userDao.findAll();
if (null == list || list.size() == 0) {
return "暂无数据";
} else {
return list;
}
}
/**
* 删除指定id用户
*
* @param id
* @return
*/
@RequestMapping(value = "/deleteUser")
public Object deleteuUser(int id) {
User user = userDao.findOne(id);
if (null == user) {
return "删除用户失败:" + id + "没找到该用户";
} else {
userDao.delete(id);
return "删除用户成功:" + id;
}
}
/**
* 添加用户
*
* @param id
* @param email
* @param name
* @return
*/
@RequestMapping(value = "/adduser")
public Object addUser(String id, String email, String name) {
System.out.println("email:" + email);
int tempId = Integer.parseInt(id);
System.out.println("tempId:" + tempId + "email:" + email + "name:" + name);
User tempUser = userDao.findOne(tempId);
if (null == tempUser) {
tempUser = new User();
tempUser.setId(tempId);
}
tempUser.setEmail(email);
tempUser.setName(name);
User resultUser = userDao.save(tempUser);
if (null == resultUser) {
return "新增用户失败";
} else {
return "新增用户:" + resultUser.getName();
}
}
// 条件查询
/**
* 获取姓名和邮箱是指定内容的用户
*
* @return
*/
@RequestMapping(value = "/getUser1")
public Object getUser(String email, String name) {
List<User> userList = userDao.findByNameAndEmail(name, email);
if (null != userList && userList.size() != 0) {
return userList;
} else {
return "没找到符合要求的用户";
}
}
/**
* 获取姓名和邮箱是指定内容的用户并排序
*
* @return
*/
@RequestMapping(value = "/getUser2")
public Object getUser2(String email, String name) {
List<User> userList = userDao.findByNameAndEmailOrderByIdDesc(name, email);
if (null != userList && userList.size() != 0) {
return userList;
} else {
return "没找到符合要求的用户";
}
}
/**
* 获取姓名和邮箱是指定内容的用户并排序,前两个
*
* @return
*/
@RequestMapping(value = "/getUser3")
public Object getUser3(String email, String name) {
List<User> userList = userDao.findTop2ByNameAndEmailOrderByIdDesc(name, email);
if (null != userList && userList.size() != 0) {
return userList;
} else {
return "没找到符合要求的用户";
}
}
/**
* 分页获取邮箱为指定内容的数据
*
* @return
*/
@RequestMapping(value = "/getUser4")
public Object getUser4(String email, int page) {
// page 属于下标 从0开始 0代表是第一页
List<User> userList = userDao.findByEmail(email, new PageRequest(page, 2));
if (null != userList && userList.size() != 0) {
return userList;
} else {
return "没找到符合要求的用户";
}
}
}
如此我们就完成了对数据库的操作:
来源:http://blog.csdn.net/u010399316/article/details/53420532
标签:spring,boot,数据库
0
投稿
猜你喜欢
python数组过滤实现方法
2021-09-11 20:28:44
sql时间格式化输出、Convert函数应用示例
2024-01-22 01:23:53
Python库 Bokeh 数据可视化实用指南
2022-09-28 14:54:51
Unicode和Python的中文处理
2022-08-17 21:05:27
vue中的provide/inject的学习使用
2024-04-27 16:08:55
python实现简单井字棋游戏
2023-08-08 21:38:01
python中使用PIL制作并验证图片验证码
2023-06-05 11:28:33
Python 错误和异常代码详解
2022-02-12 15:14:08
Python读取一个目录下所有目录和文件的方法
2023-05-30 23:04:21
压缩包密码破解示例分享(类似典破解)
2022-10-17 17:03:37
Python映射类型之dict详解
2023-08-03 13:19:19
Python中如何引入第三方模块
2023-08-27 08:34:48
Python实现图像的二进制与base64互转
2021-03-18 17:57:55
Python对接六大主流数据库(只需三步)
2022-04-25 21:51:55
ActionScript3.0是革命性的
2008-05-01 12:36:00
Python 抓取数据存储到Redis中的操作
2022-11-28 03:13:12
golang实现各种情况的get请求操作
2024-04-25 15:30:15
连接mysql的常用工具分享
2024-01-21 20:01:18
Pygame游戏开发之太空射击实战子弹与碰撞处理篇
2022-05-24 03:59:19
Golang中生成随机字符串并复制到粘贴板的方法
2024-02-16 08:32:10