Spring Boot Mysql 数据库操作示例
程序员文章站
2024-03-06 17:23:14
本文默认你的开发环境.数据库已经安装好
想用使用数据库.我们需要现在pom文件中添加相应的依赖
pom.xml
本文默认你的开发环境.数据库已经安装好
想用使用数据库.我们需要现在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 "没找到符合要求的用户"; } } }
如此我们就完成了对数据库的操作:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: C 语言的输入输出流以及数据缓冲
推荐阅读
-
Spring Boot Mysql 数据库操作示例
-
使用Spring Boot+MyBatis框架做查询操作的示例代码
-
PHP+mysql实现从数据库获取下拉树功能示例
-
基于thinkPHP类的插入数据库操作功能示例
-
Spring Boot中使用Spring-data-jpa实现数据库增删查改
-
spring batch 读取多个文件数据导入数据库示例
-
详解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步骤
-
mysql基础--查询数据库操作
-
php mysql操作mysql_connect连接数据库实例详解
-
Spring Data MongoDB 数据库批量操作的方法