入门demo,简述 使用springJPA 的步骤
1、引入jpa; compile “org.springframework.boot:spring-boot-starter-data-jpa”
2、配置文件application.properties
#服务器访问端口
server.port=8089
# 数据库基本配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yszd
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
# 显示后台处理的SQL语句
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true
# 自动检查实体和数据库表是否一致,如果不一致则会进行更新数据库表
spring.jpa.hibernate.ddl-auto=update
3、modal 层
@TypeChecked
@CompileStatic
@Entity
@Table(name = "t_product")
class TProduct implements Serializable{
@Id
@Column(name="id")
Integer id
@Column(name="product_no")
String productNo
@Column(name="product_name")
String productName
@Column(name="asset_type")
Integer assetType
@Column(name="finance_money")
BigDecimal financeMoney
@Column(name="period_count")
Integer periodCount //周期
@Column(name="rate")
BigDecimal rate //利率
@Column(name="money_back_type")
Integer moneyBackType //还款方式
@Column(name="risk_level")
Integer riskLevel //风险等级
}
4、dao 层
/**
* Created by yszd2017 on 2018/3/27.
*/
interface ProductRepository extends JpaRepository<TProduct,Integer>{
TProduct findByProductNo(String productNo)
@Query("select s from com.yszd.core.domain.TProduct s where s.productName like %:name% ")
List<TProduct>findListLikeName(@Param(value = "name") String name)
}
5、controller 层
package com.yszd.api
import com.yszd.core.dao.ProductRepository
import com.yszd.core.domain.TProduct
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
/**
* Created by yszd2017 on 2018/3/27.
* 接口测试
*/
@RestController
@RequestMapping(value = "/api/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@TypeChecked
@CompileStatic
@Api(value = "测试接口", description = "测试接口")
class TestApi {
@GetMapping("test1")
test1() {
[resCode: "0000"]
}
@Autowired
private ProductRepository productRepository
@GetMapping("getPageList")
@ApiOperation(value = "分页查询列表", notes = "分页查询列表")
getProductPageList(
@ApiParam(required = true, name = "page", value = "查询页数") @RequestParam(value = "page") Integer page,
@ApiParam(required = true, name = "pageSize", value = "每页数量") @RequestParam(value = "pageSize") Integer pageSize) {
page = page ?: 1
pageSize = pageSize ?: 10
Page<TProduct> all = productRepository.findAll(new PageRequest(page <= 0 ? 1 : page - 1, pageSize))
return all
}
@GetMapping("getProductByProductNo")
@ApiOperation(value = "根据产品编号查询", notes = "根据产品编号查询")
getProductByProductNo(
@ApiParam(required = true, name = "productNo", value = "产品编号") @RequestParam(value = "productNo") String productNo) {
productRepository.findByProductNo(productNo)
}
@GetMapping("getListLikeName")
@ApiOperation(value = "根据产品名称模糊匹配查询", notes = "根据产品名称模糊匹配查询")
getListLikeName(
@ApiParam(required = true, name = "productName", value = "产品名称模糊匹配") @RequestParam(value = "productName") String productName) {
productRepository.findListLikeName(productName)
}
}