spring boot POM文件中加入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.yml配置:
spring:
datasource:
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: system
password: 123456
jta:
enabled: true
jpa:
show-sql: true
database-platform: org.hibernate.dialect.Oracle12cDialect
hibernate:
ddl-auto: none
实体类:(@Table和@Column注解中name的值全部小写或全部大写,否则映射会出错)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
@Entity
@Table(name = "templatetable")
public class TemplateDto implements Serializable {
private static final long serialVersionUID = -7409827055841916106L;
@Id
private String id;
@Column(name = "PRODUCTCODE")
private String productCode;
@Column(name = "PRODUCTNAME")
private String productName;
@Column(name = "CREATETIME")
private Date createTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
dao层:
import com.linkus.dto.TemplateDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
public interface TemplateDao extends JpaRepository<TemplateDto,String> {
public TemplateDto findByProductCode(String productCode);
}
JpaRepository接口中有一些基本的增删改查方法,findByProductCode方法是我们自定义的,框架会帮我们根据方法名称生成查询sql,
findByProductCode这个方法就会生成根据productCode字段查询的sql(select * from templatetable where productcode = ''),
详细使用说明可查看官方文档:https://docs.spring.io/spring-data/jpa/docs/2.7.15/reference/html/#jpa.query-methods
service层使用dao:
import com.linkus.dao.TemplateDao;
import com.linkus.dto.TemplateDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TemplateService{
private static final Logger LOGGER = LoggerFactory.getLogger(TemplateService.class);
@Autowired
TemplateDao templateDao;
public List<TemplateDto> findList(){
List<TemplateDto> templateDtos = templateDao.findAll();
return templateDtos;
}
public TemplateDto findByCode(String code){
TemplateDto templateDto = templateDao.findByProductCode(code);
return templateDto;
}
}
标签:String,jpa,spring,boot,org,import,public
From: https://www.cnblogs.com/mmcode/p/17691338.html