• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot入门系列之JPA mysql

springboot 搞代码 4年前 (2022-01-05) 16次浏览 已收录 0个评论

这篇文章主要介绍了SpringBoot入门系列之JPA mysql的相关资料,需要的朋友可以参考下

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

1、http://start.spring.io/

     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配置为

  <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->  %d %p (%file:%line\)- %m%nGBK log/base.log log/base.log.%d.i% <!-- or whenever the file size reaches 64 MB -->64 MB  %d %p (%file:%line\)- %m%n UTF-8<!-- 此处设置字符集 -->  

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

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

http://localhost:8080/

http://localhost:8080/hello/上帝

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

1、在包com.exa来源gao.dai.ma.com搞@代*码网mple下建立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 { List findByName(String name); List findByAddress(String address); List findByNameAndAddress(String name,String address); @Query("select p from Person p where p.name=:name and p.address=:address") List 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 q1(String address){ logger.debug("q1 开始"); logger.debug("q1 接收参数address={}",address); List people=personRepository.findByAddress(address); return people; } @RequestMapping("/q2") public List q2(String name,String address){ logger.debug("q2 开始"); logger.debug("q2接收参数name={},address={}",name,address); return personRepository.findByNameAndAddress(name, address); } @RequestMapping("/q3") public List q3(String name,String address){ logger.debug("q3 开始"); logger.debug("q3接收参数name={},address={}",name,address); return personRepository.withNameAndAddressQuery(name, address); } @RequestMapping("/sort") public List sort(){ logger.debug("sort 开始"); List people=personRepository.findAll(new Sort(Direction.ASC,"age")); return people; } @RequestMapping("/page") public Page page(){ logger.debug("page 开始"); Page 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、先保存数据

http://localhost:8080/save?name=aa&&address=北京&&age=1
http://localhost:8080/save?name=ab&&address=北京&&age=2
http://localhost:8080/save?name=cq1&&address=重庆&&age=50
http://localhost:8080/save?name=cq2&&address=重庆&&age=51

B、查询q1

http://localhost:8080/q1?address=北京

C、查询q2

http://localhost:8080/q2?address=北京&&name=aa

D、查询q3

http://localhost:8080/q3?address=北京&&name=aa

E、排序

http://localhost:8080/sort

F、分页

http://localhost:8080/page

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

以上所述是小编给大家介绍的SpringBoot入门系列之JPA mysql,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对gaodaima搞代码网网站的支持!

以上就是SpringBoot入门系列之JPA mysql的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot入门系列之JPA mysql

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址