您好,登錄后才能下訂單哦!
本篇文章為大家展示了SpringBoot如何連接并操作Mysql 數據庫,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
本文默認你的開發環境.數據庫已經安裝好
想用使用數據庫.我們需要現在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 "沒找到符合要求的用戶"; } } }
如此我們就完成了對數據庫的操作:
上述內容就是SpringBoot如何連接并操作Mysql 數據庫,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。