有人猜我错了吗??
package com.yyy.subject.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Subject {
@Id
private int id;
private String code;
private String name;
private String medium;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package com.yyy.subject.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.yyy.subject.model.Subject;
@Repository
public interface SubjectRepository extends JpaRepository<Subject, Integer> {
@Query("select medium,count(medium) from subject group by medium")
public List<Object[]> findTypeAndTypeCount();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package com.yyy.subject.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.yyy.subject.model.Subject;
@Service
public interface SubjectService {
public Subject create(Subject subject);
public Subject update(Subject subject);
public void delete(int id);
public Subject readOne(int id);
public List<Subject> readAll();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package com.yyy.subject.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yyy.subject.model.Subject;
import com.yyy.subject.repository.SubjectRepository;
@Service
public class SubjectServiceImpl implements SubjectService {
@Autowired
private SubjectRepository subjectRepository;
@Override
public Subject create(Subject subject) {
return subjectRepository.save(subject);
}
@Override
public Subject update(Subject subject) {
return subjectRepository.save(subject);
}
@Override
public void delete(int id) {
subjectRepository.deleteById(id);
}
@Override
public Subject readOne(int id) {
return subjectRepository.findById(id).get();
}
@Override
public List<Subject> readAll() {
return (List<Subject>) subjectRepository.findAll();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package com.yyy.subject.util;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public interface ReportUtil {
public void generatePieChart(String path,List<Object[]> data);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package com.yyy.subject.util;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.stereotype.Component;
@Component
public class ReportUtilImpl implements ReportUtil {
@Override
public void generatePieChart(String path,List<Object[]> data) {
DefaultPieDataset dataset=new DefaultPieDataset();
for (Object[] objects : data) {
dataset.setValue(objects[0].toString(), new Double(objects[1].toString()));
}
JFreeChart chart = ChartFactory.createPieChart3D("Report", dataset);
try {
ChartUtilities.saveChartAsJPEG(new File(path+"pieChart.jpeg"), chart, 300, 300);
} catch (IOException e) {
e.printStackTrace();
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package com.yyy.subject.controller;
import java.util.List;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.yyy.subject.model.Subject;
import com.yyy.subject.repository.SubjectRepository;
import com.yyy.subject.service.SubjectService;
import com.yyy.subject.util.ReportUtil;
@Controller
public class SubjectController {
@Autowired
private SubjectService subjectService;
@Autowired
private SubjectRepository subjectRepository;
@Autowired
private ReportUtil report;
@Autowired
private ServletContext sc;
@RequestMapping("/form33")
public String create(ModelMap model)
{
model.addAttribute("subject", new Subject());
return "page1";
}
@RequestMapping("/form34")
public String create2(ModelMap model,@ModelAttribute("subject") Subject subject)
{
subjectService.create(subject);
model.addAttribute("msz", "Database created with id:"+subject.getId());
return "page1";
}
@RequestMapping("/display")
public String read(ModelMap model)
{
List<Subject> subject = subjectService.readAll();
model.addAttribute("subject", subject);
return "page2";
}
@RequestMapping("/deleteSubject")
public String delete(@RequestParam("id") int id,ModelMap model)
{
subjectService.delete(id);
List<Subject> subject = subjectService.readAll();
model.addAttribute("subject", subject);
return "page2";
}
@RequestMapping("/updateSubject")
public String edit(@RequestParam("id") int id,ModelMap model)
{
Subject subject = subjectService.readOne(id);
model.addAttribute("subject", subject);
return "editpage";
}
@RequestMapping("/updated")
public String update(@ModelAttribute("subject") Subject subject1,ModelMap model)
{
subjectService.update(subject1);
List<Subject> subject = subjectService.readAll();
model.addAttribute("subject", subject);
return "page2";
}
@RequestMapping("/generateReport")
public String generateReport()
{
String path=sc.getRealPath("/");
List<Object[]> data= subjectRepository.findTypeAndTypeCount();
report.generatePieChart(path,data);
return "report";
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package com.yyy.subject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class YyySubjectApplication {
public static void main(String[] args) {
SpringApplication.run(YyySubjectApplication.class, args);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
server.port=9090
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yyy.subject</groupId>
<artifactId>YYYSubject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>YYYSubject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate.hql</groupId>
<artifactId>hibernate-hql-parser</artifactId>
<version>1.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2><i>Enter Details</i></h2>
<form:form action="form34" modelAttribute="subject" method="post">
Id:<input type="text" name="id"/><br><br>
Code:<form:input path="code"/><br><br>
Name:<form:input path="name"/><br><br>
Medium:<br>
English<form:radiobutton path="medium" value="ENGLISH"/><br>
Hindi<form:radiobutton path="medium" value="HINDI"/><br>
Punjabi<form:radiobutton path="medium" value="PUNJABI"/>
<br><br>
<input type="submit" value="Submit"/>
<br><br>
${msz}
<br><br>
<a href="display">View All</a>
</form:form>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
错误:
Error
创建名称为'subjectController'的bean:通过字段'subjectService'表示的不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'subjectServiceImpl'的bean时出错:通过字段'subjectRepository'表示的不满足的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为'subjectRepository'的bean时出错:调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:方法公共抽象java.util.List com.yyy.subject.repository.SubjectRepository.findTypeAndTypeCount()的查询验证失败!
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: subject is not mapped [select medium,count(medium) from subject group by medium]
答案 0 :(得分:0)
1)JPQL区分大小写,并且2)您必须使用别名:
@Query("select s.medium, count(s.medium) from Subject s group by s.medium")