欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot入门系列之JPA mysql

程序员文章站 2024-03-02 08:27:46
一,准备工作,建立spring-boot-sample-mysql工程 1、      a、artifact中输入spring-...

一,准备工作,建立spring-boot-sample-mysql工程

1、

     a、artifact中输入spring-boot-sample-mysql
     b、勾选web下的web
     c、勾选sql下的jpa mysql

2、eclips中导入工程spring-boot-sample-mysql

     a、解压快捷工程spring-boot-sample-mysql到某文件夹

     b、eclips中file->import->import existing maven projects-->select maven projects-->finish导入工程

3、工程导入之后,文件结构如下图

4、在包com.example下建立web文件夹

5、便于测试,引入spring-boot-sample-helloworld的hellocontroller及配置文件logback.xml

hellocontroller代码为

package com.example.web; 
import org.slf4j.logger; 
import org.slf4j.loggerfactory; 
import org.springframework.web.bind.annotation.pathvariable; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.restcontroller; 
@restcontroller 
public class hellocontroller { 
  protected static logger logger=loggerfactory.getlogger(hellocontroller.class); 
  @requestmapping("/") 
  public string helloworld(){ 
    logger.debug("访问hello"); 
    return "hello world!"; 
  } 
  @requestmapping("/hello/{name}") 
  public string helloname(@pathvariable string name){ 
    logger.debug("访问helloname,name={}",name); 
    return "hello "+name; 
  } 
} 

logback.xml配置为

<configuration>  
  <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->  
  <appender name="stdout" class="ch.qos.logback.core.consoleappender">  
    <encoder>  
      <pattern>%d %p (%file:%line\)- %m%n</pattern> 
      <charset>gbk</charset>  
    </encoder>  
  </appender>  
  <appender name="baselog"  
    class="ch.qos.logback.core.rolling.rollingfileappender">  
    <file>log/base.log</file>  
    <rollingpolicy class="ch.qos.logback.core.rolling.timebasedrollingpolicy">  
      <filenamepattern>log/base.log.%d.i%</filenamepattern>  
      <timebasedfilenamingandtriggeringpolicy class="ch.qos.logback.core.rolling.sizeandtimebasedfnatp">  
        <!-- or whenever the file size reaches 64 mb -->  
        <maxfilesize>64 mb</maxfilesize>  
      </timebasedfilenamingandtriggeringpolicy>  
    </rollingpolicy>  
    <encoder>  
      <pattern>  
        %d %p (%file:%line\)- %m%n 
      </pattern>  
      <charset>utf-8</charset> <!-- 此处设置字符集 -->  
    </encoder>  
  </appender>  
  <root level="info">  
    <appender-ref ref="stdout" />  
  </root>  
  <logger name="com.example" level="debug">  
    <appender-ref ref="baselog" />  
  </logger>  
</configuration> 

注:logback.xml文件位于src/main/resources下

6、启动工程,通过浏览器查看正确性


上帝

二,使用jpa,构建业务对象及访问库

1、在包com.example下建立domain文件夹

2、在domain中建立类person

package com.example.domain; 
import javax.persistence.entity; 
import javax.persistence.generatedvalue; 
import javax.persistence.id; 
@entity 
public class person { 
  @id 
  @generatedvalue 
  private long id; 
  private string name; 
  private integer age; 
  private string address; 
  public person() { 
    super(); 
  } 
  public person(long id, string name, integer age, string address) { 
    super(); 
    this.id = id; 
    this.name = name; 
    this.age = age; 
    this.address = address; 
  } 
  public long getid() { 
    return id; 
  } 
  public void setid(long id) { 
    this.id = id; 
  } 
  public string getname() { 
    return name; 
  } 
  public void setname(string name) { 
    this.name = name; 
  } 
  public integer getage() { 
    return age; 
  } 
  public void setage(integer age) { 
    this.age = age; 
  } 
  public string getaddress() { 
    return address; 
  } 
  public void setaddress(string address) { 
    this.address = address; 
  } 
} 

注意:构造函数

3、在包com.example下建立repository文件夹

4、在repository中建立接口personrepository

package com.example.repository; 
import java.util.list; 
import org.springframework.data.jpa.repository.jparepository; 
import org.springframework.data.jpa.repository.query; 
import org.springframework.data.repository.query.param; 
import org.springframework.stereotype.repository; 
import com.example.domain.person; 
@repository 
public interface personrepository extends jparepository<person,long> { 
  list<person> findbyname(string name); 
  list<person> findbyaddress(string address); 
  list<person> findbynameandaddress(string name,string address); 
  @query("select p from person p where p.name=:name and p.address=:address") 
  list<person> withnameandaddressquery(@param("name")string name,@param("address")string address); 
} 

5、在web中建立datacontroller

package com.example.web; 
import java.util.list; 
import org.slf4j.logger; 
import org.slf4j.loggerfactory; 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.data.domain.page; 
import org.springframework.data.domain.pagerequest; 
import org.springframework.data.domain.sort; 
import org.springframework.data.domain.sort.direction; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.restcontroller; 
import com.example.domain.person; 
import com.example.repository.personrepository; 
@restcontroller 
public class datacontroller { 
  protected static logger logger=loggerfactory.getlogger(datacontroller.class); 
  @autowired 
  personrepository personrepository; 
  @requestmapping("/save") 
  public person save(string name,string address,integer age){ 
    logger.debug("save 开始"); 
    person p=personrepository.save(new person(null,name,age,address)); 
    logger.debug("save 结束"); 
    return p; 
  } 
  @requestmapping("/q1") 
  public list<person> q1(string address){ 
    logger.debug("q1 开始"); 
    logger.debug("q1 接收参数address={}",address); 
    list<person> people=personrepository.findbyaddress(address); 
    return people; 
  } 
  @requestmapping("/q2") 
  public list<person> q2(string name,string address){ 
    logger.debug("q2 开始"); 
    logger.debug("q2接收参数name={},address={}",name,address); 
    return personrepository.findbynameandaddress(name, address); 
  } 
  @requestmapping("/q3") 
  public list<person> q3(string name,string address){ 
    logger.debug("q3 开始"); 
    logger.debug("q3接收参数name={},address={}",name,address); 
    return personrepository.withnameandaddressquery(name, address); 
  } 
  @requestmapping("/sort") 
  public list<person> sort(){ 
    logger.debug("sort 开始"); 
    list<person> people=personrepository.findall(new sort(direction.asc,"age")); 
    return people; 
  } 
  @requestmapping("/page") 
  public page<person> page(){ 
    logger.debug("page 开始"); 
    page<person> people=personrepository.findall(new pagerequest(1,2)); 
    return people; 
  } 
} 

6、配置数据库连接,在application.properties(src/main/resources下)

spring.datasource.url=jdbc:mysql://192.168.56.201:3306/bootsample?useunicode=true&characterencoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

7、运行测试

a、先保存数据

=北京&&age=1
=北京&&age=2
=重庆&&age=50
=重庆&&age=51

b、查询q1

=北京

c、查询q2

=北京&&name=aa

d、查询q3

=北京&&name=aa

e、排序


f、分页

运用hibernate访问mysql,基本也是老技术,只是用jpa简化了dao层代码,对于业务对象基本没有变化。

以上所述是小编给大家介绍的springboot入门系列之jpa mysql,希望对大家有所帮助