当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot连接MYSQL数据库并使用JPA进行操作

SpringBoot连接MYSQL数据库并使用JPA进行操作

2019年07月22日  | 移动技术网IT编程  | 我要评论

今天给大家介绍一下如何springboot中连接mysql数据库,并使用jpa进行数据库的相关操作。

步骤一:在pom.xml文件中添加mysql和jpa的相关jar包依赖,具体添加位置在dependencies中,具体添加的内容如下所示。

<!--数据库相关配置--> 
    <dependency> 
      <groupid>org.springframework.boot</groupid> 
      <artifactid>spring-boot-starter-web</artifactid> 
    </dependency> 
    <dependency> 
      <groupid>org.springframework.boot</groupid> 
      <artifactid>spring-boot-starter-data-jpa</artifactid> 
    </dependency> 
    <dependency> 
      <groupid>mysql</groupid> 
      <artifactid>mysql-connector-java</artifactid> 
    </dependency> 
    <dependency> 
      <groupid>org.apache.poi</groupid> 
      <artifactid>poi</artifactid> 
      <version>3.11</version> 
    </dependency> 

步骤二:在application.properties配置文件中加入数据库的相关配置,配置信息如下所示。

spring.datasource.url = jdbc:mysql://localhost:3306/webtest 
spring.datasource.username = root 
spring.datasource.password = 220316 
spring.datasource.driverclassname = com.mysql.jdbc.driver 
# specify the dbms 
spring.jpa.database = mysql 
# show or not log for each sql query 
spring.jpa.show-sql = true 
# hibernate ddl auto (create, create-drop, update) 
spring.jpa.hibernate.ddl-auto = update 
# naming strategy 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.improvednamingstrategy 
# stripped before adding them to the entity manager) 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.mysql5dialect 

这里给大家解释一下:webtest代表数据库名称、root是用户名、220316是密码

步骤三:编写数据库操作的实体类,实体类具体信息如下所示:

package example.entity; 
import javax.persistence.*; 
import javax.validation.constraints.notnull; 
import java.math.bigdecimal; 
import java.util.date; 
@entity 
@table(name = "user") 
public class user { 
  @id 
  @generatedvalue(strategy = generationtype.auto) 
  private int id; 
 
  @column(name = "name", nullable = true, length = 30) 
  private string name; 
 
  @column(name = "height", nullable = true, length = 10) 
  private int height; 
 
  @column(name = "sex", nullable = true, length = 2) 
  private char sex; 
 
  @temporal(temporaltype.date) 
  private date birthday; 
 
  @temporal(temporaltype.timestamp) 
  private date sendtime; // 日期类型,格式:yyyy-mm-dd hh:mm:ss 
 
  @column(name = "price", nullable = true, length = 10) 
  private bigdecimal price; 
 
  @column(name = "floatprice", nullable = true, length = 10) 
  private float floatprice; 
 
 
  @column(name = "doubleprice", nullable = true, length = 10) 
  private double doubleprice; 
 
  public date getsendtime() { 
    return sendtime; 
  } 
 
  public void setsendtime(date sendtime) { 
    this.sendtime = sendtime; 
  } 
 
  public bigdecimal getprice() { 
    return price; 
  } 
 
  public void setprice(bigdecimal price) { 
    this.price = price; 
  } 
 
  public float getfloatprice() { 
    return floatprice; 
  } 
 
  public void setfloatprice(float floatprice) { 
    this.floatprice = floatprice; 
  } 
 
  public double getdoubleprice() { 
    return doubleprice; 
  } 
 
  public void setdoubleprice(double doubleprice) { 
    this.doubleprice = doubleprice; 
  } 
 
  public user() { } 
 
  public char getsex() { 
    return sex; 
  } 
 
  public void setsex(char sex) { 
    this.sex = sex; 
  } 
 
  public date getbirthday() { 
    return birthday; 
  } 
 
  public void setbirthday(date birthday) { 
    this.birthday = birthday; 
  } 
 
  public user(int id) { 
    this.id = id; 
  } 
 
  public int getid() { 
    return id; 
  } 
 
  public void setid(int id) { 
    this.id = id; 
  } 
 
  public string getname() { 
    return name; 
  } 
 
  public void setname(string name) { 
    this.name = name; 
  } 
 
  public int getheight() { 
    return height; 
  } 
 
  public void setheight(int height) { 
    this.height = height; 
  } 
} 

大家这里需要注意的是:实体类中的类名和字段属性都要和数据库中表和字段相互对应。下面给出一张mysql-java各种属性的对应关系图:

步骤四:编写dao层的数据操作类,dao数据操作类如下所示:

package example.dao; 
import example.entity.user; 
import org.springframework.data.repository.crudrepository; 
import javax.transaction.transactional; 
import java.math.bigdecimal; 
import java.util.date; 
import java.util.list; 
 
@transactional 
public interface userdao extends crudrepository<user, integer> { 
  public list<user> findbyname(string name); 
  public list<user> findbysex(char sex); 
  public list<user> findbybirthday(date birthday); 
  public list<user> findbysendtime(date sendtime); 
  public list<user> findbyprice(bigdecimal price); 
  public list<user> findbyfloatprice(float floatprice); 
  public list<user> findbydoubleprice(double doubleprice); 
} 

大家这里可能会有疑问,为什么要继承crudrepository<user, integer>,具体有什么作用呢?

我这里给大家简单的介绍一下jpa中一些常用的用法和使用准则:

1.首先就是要继承crudrepository这个方法,里面包含的两个参数的具体含义是:第一个参数表示所操作的实体类名称,第二个参数表示实体类中主键的类型。

2.继承完之后就可以使用一些继承自父类的方法了,比如上面所示可以使用findby+“你要查询的字段名称”,通过这样的方法就可以轻轻松松实现sql查询的功能了。

说道这里可能大家还是有点迷糊,给大家举一个例子就知道了:

例如上面的findbyname(string name)其实等价于sql语句中的 select *from user where name=?。这样一对比大家是不是马上就清楚这个方法到底代表什么含义了吧。

步骤五:编写controller这个控制类,控制类具体信息如下所示:

package example.controller; 
import example.dao.userdao; 
import example.entity.user; 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.stereotype.controller; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.responsebody; 
import java.math.bigdecimal; 
import java.text.parseexception; 
import java.text.simpledateformat; 
import java.util.date; 
import java.util.list; 
@controller 
public class usercontroller { 
  @autowired 
  private userdao userdao; 
  @requestmapping("/getname") 
  @responsebody 
  public string getbyname(string name) { 
    list<user> userlist = userdao.findbyname(name); 
    if (userlist != null && userlist.size()!=0) { 
      return "the user length is: " + userlist.size(); 
    } 
    return "user " + name + " is not exist."; 
  } 
 
  @requestmapping("/getsex") 
  @responsebody 
  public string getbysex(char sex) { 
    list<user> userlist = userdao.findbysex(sex); 
    if (userlist != null && userlist.size()!=0) { 
      return "the user length is: " + userlist.size(); 
    } 
    return "user " + sex + " is not exist."; 
  } 
 
  @requestmapping("/getbirthday") 
  @responsebody 
  public string findbybirthday(string birthday) { 
    system.out.println("birthday:"+birthday); 
    simpledateformat formate=new simpledateformat("yyyy-mm-dd"); 
    list<user> userlist = null; 
    try { 
      userlist = userdao.findbybirthday(formate.parse(birthday)); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
    } 
    if (userlist != null && userlist.size()!=0) { 
      return "the user length is: " + userlist.size(); 
    } 
    return "user " + birthday + " is not exist."; 
  } 
 
  @requestmapping("/getsendtime") 
  @responsebody 
  public string findbysendtime(string sendtime) { 
    system.out.println("sendtime:"+sendtime); 
    simpledateformat formate=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
    list<user> userlist = null; 
    try { 
      userlist = userdao.findbysendtime(formate.parse(sendtime)); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
    } 
    if (userlist != null && userlist.size()!=0) { 
      return "the user length is: " + userlist.size(); 
    } 
    return "user " + sendtime + " is not exist."; 
  } 
 
  @requestmapping("/getprice") 
  @responsebody 
  public string findbyprice(bigdecimal price) { 
    list<user> userlist = null; 
    userlist = userdao.findbyprice(price); 
    if (userlist != null && userlist.size()!=0) { 
      return "the user length is: " + userlist.size(); 
    } 
    return "user " + price + " is not exist."; 
  } 
 
  @requestmapping("/getfloatprice") 
  @responsebody 
  public string findfloatprice(float floatprice) { 
    list<user> userlist = null; 
    userlist = userdao.findbyfloatprice(floatprice); 
    if (userlist != null && userlist.size()!=0) { 
      return "the user length is: " + userlist.size(); 
    } 
    return "user " + floatprice + " is not exist."; 
  } 
 
  @requestmapping("/getdoubleprice") 
  @responsebody 
  public string findbyprice(double doubleprice) { 
    list<user> userlist = null; 
    userlist = userdao.findbydoubleprice(doubleprice); 
    if (userlist != null && userlist.size()!=0) { 
      return "the user length is: " + userlist.size(); 
    } 
    return "user " + doubleprice + " is not exist."; 
  } 
} 

大家这里可能会有一个很大的疑问,我当初也对这个问题深深的不理,那就是userdao没有实例化为什么能够直接使用呢?

现在我就为大家解释一下为什么会这样:

其实不是这个userdao没有实例化,只是实例化是由系统自动完成的。只要在userdao的上方添加@autowired属性就可以实现接口自动的实例化了,完全不需要像以前一样需要去写什么userdaoimp之类的实现类了。这样做就可以大大的挺高代码的简易程度,开发速度大大的挺高。

我知道现在可能还会有人问这样一个问题:那就是自动实例化了,可是实例化怎么知道dao类要实现什么的增删改查的功能呀,dao代码里面压根就没说啊?其实有心人可能已经发现了,上一步的时候我们解释了一下findby+“字段名”的具体作用是什么,这其实就是这个问题的答案。其实dao层中各种方法就是daoimp中各种实现类中的sql命令,具体是怎么对应的我会再下一节中给大家详细的介绍一下,现在先卖个关子。

步骤六:数据库的表名和字段信息如下所示:


 

到这里关于springboot中连接mysql数据库,并使用jpa进行数据库的相关操作就介绍完毕了,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网