中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

SpringBoot如何連接并操作Mysql數(shù)據(jù)庫

本篇文章為大家展示了SpringBoot如何連接并操作MySQL 數(shù)據(jù)庫,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、西平網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、商城網(wǎng)站定制開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為西平等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

本文默認(rèn)你的開發(fā)環(huán)境.數(shù)據(jù)庫已經(jīng)安裝好

想用使用數(shù)據(jù)庫.我們需要現(xiàn)在pom文件中添加相應(yīng)的依賴

pom.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;> 
<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> 

我們的配置文件進(jìn)行數(shù)據(jù)庫的配置

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 

前面配置的是數(shù)據(jù)庫信息,最后一行配置的是.如果我們定義的實(shí)體在數(shù)據(jù)庫中沒有對(duì)應(yīng)的表的話.會(huì)幫我們自動(dòng)創(chuàng)建

我們的實(shí)體類型

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; 
  } 
 
} 

對(duì)應(yīng)數(shù)據(jù)庫中的user表,沒有的話會(huì)自動(dòng)創(chuàng)建

想對(duì)數(shù)據(jù)庫進(jìn)行操作,我們需要?jiǎng)?chuàng)建一個(gè)接口繼承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 方法名就是查詢語句,只要規(guī)法寫方法名一切就都可以完成(當(dāng)然.有時(shí)候會(huì)造成方法名又臭又長) 
  User findByEmail(String email);//根據(jù)郵箱查詢 
  List<User> findByName(String name);//根據(jù)用戶名查詢 
   
  //select * from test.users where email='imgod@qq.com' and name='imgod'; 
  List<User> findByNameAndEmail(String name,String email);//根據(jù)用戶名和郵箱進(jìn)行查詢 
   
  //select * from test.users where email='imgod@qq.com' and name='imgod4444' order by id desc; 
  List<User> findByNameAndEmailOrderByIdDesc(String name,String email);//根據(jù)用戶名和郵箱進(jìn)行查詢,排序 
   
  //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);//根據(jù)用戶名和郵箱進(jìn)行查詢,排序,前兩個(gè) 
 
  //根據(jù)郵箱進(jìn)行分頁查詢 
  List<User> findByEmail(String email,Pageable pageable);//根據(jù)用戶名和郵箱進(jìn)行查詢 
} 

實(shí)現(xiàn)CrudRepository我們就可以對(duì)數(shù)據(jù)庫進(jìn)行一些基礎(chǔ)操作了

如果我們想要實(shí)現(xiàn)更多的操作.需要自己按照嚴(yán)格的命名規(guī)則為方法定義方法名

下面就是我們重要的控制器的實(shí)現(xiàn)了:

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; 
 
  /** 
   * 根據(jù)郵件去查找 
   * 
   * @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 "暫無數(shù)據(jù)"; 
    } else { 
      return user; 
    } 
 
  } 
 
  /** 
   * 獲取所有的用戶信息 
   * 
   * @return 
   */ 
  @RequestMapping(value = "/getall") 
  public Object getAllUser() { 
    List<User> list = (List<User>) userDao.findAll(); 
    if (null == list || list.size() == 0) { 
      return "暫無數(shù)據(jù)"; 
    } 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(); 
    } 
 
  } 
 
  // 條件查詢 
 
  /** 
   * 獲取姓名和郵箱是指定內(nèi)容的用戶 
   * 
   * @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 "沒找到符合要求的用戶"; 
    } 
 
  } 
 
  /** 
   * 獲取姓名和郵箱是指定內(nèi)容的用戶并排序 
   * 
   * @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 "沒找到符合要求的用戶"; 
    } 
 
  } 
 
  /** 
   * 獲取姓名和郵箱是指定內(nèi)容的用戶并排序,前兩個(gè) 
   * 
   * @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 "沒找到符合要求的用戶"; 
    } 
 
  } 
 
  /** 
   * 分頁獲取郵箱為指定內(nèi)容的數(shù)據(jù) 
   * 
   * @return 
   */ 
  @RequestMapping(value = "/getUser4") 
  public Object getUser4(String email, int page) { 
    // page 屬于下標(biāo) 從0開始 0代表是第一頁 
    List<User> userList = userDao.findByEmail(email, new PageRequest(page, 2)); 
    if (null != userList && userList.size() != 0) { 
      return userList; 
    } else { 
      return "沒找到符合要求的用戶"; 
    } 
 
  } 
} 

如此我們就完成了對(duì)數(shù)據(jù)庫的操作:

SpringBoot如何連接并操作Mysql 數(shù)據(jù)庫

SpringBoot如何連接并操作Mysql 數(shù)據(jù)庫

上述內(nèi)容就是SpringBoot如何連接并操作Mysql 數(shù)據(jù)庫,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章題目:SpringBoot如何連接并操作Mysql數(shù)據(jù)庫
URL標(biāo)題:http://www.rwnh.cn/article36/jipcsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司
茂名市| 广昌县| 平度市| 丰城市| 新竹县| 崇义县| 岱山县| 福泉市| 克东县| 交城县| 根河市| 宜兰市| 枞阳县| 永安市| 德格县| 镇坪县| 平和县| 什邡市| 得荣县| 济南市| 延寿县| 沙雅县| 龙泉市| 雅安市| 邢台县| 屏山县| 新建县| 崇阳县| 鹿泉市| 枞阳县| 阳西县| 通许县| 辽宁省| 茌平县| 淳安县| 七台河市| 贡山| 东兴市| 扎鲁特旗| 花莲县| 桑日县|