我正在尝试将后端项目myonlinebackend与前端项目myonlineshopping链接起来,在添加“private session factory session factory;”之前,它工作得很好去CategoryDAOImpl.java。但是在声明了sessionFactory变量之后,我得到了下面这个错误。
这是我的课程:
类别.java
package src.main.java.myshoppingbackend.dto;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Category{
/*
* Private fields
* */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String description;
@Column(name = "image_url")
private String imageURL;
@Column(name = "is_active")
private boolean active = true;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + ", description=" + description + ", imageURL=" + imageURL
+ ", active=" + active + "]";
}
}
CategoryDAOImpl.java
package src.main.java.myshoppingbackend.daoimpl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import src.main.java.myshoppingbackend.dao.CategoryDAO;
import src.main.java.myshoppingbackend.dto.Category;
@Repository("categoryDAO")
public class CategoryDAOImpl implements CategoryDAO {
@Autowired
private SessionFactory sessionFactory;
private static List<Category> categories = new ArrayList<>();
static {
Category item = new Category();
item.setId(1);
item.setDescription("Des 1");
item.setImageURL("TV.jpg");
item.setName("TV1");
item.setActive(true);
categories.add(item);
item = new Category();
item.setId(2);
item.setDescription("Des 21");
item.setImageURL("mobile.jpg");
item.setName("mobile1");
item.setActive(true);
categories.add(item);
}
@Override
public List<Category> list() {
return categories;
}
@Override
public Category get(int id) {
for (Category category : categories) {
if (category.getId() == id) {
return category;
}
}
return null;
}
@Override
@Transactional
public boolean add(Category category) {
try {
// add the category to the database table
sessionFactory.getCurrentSession().persist(category);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
}
页面控制器.java
package src.main.java.myonlineshopping.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import src.main.java.myshoppingbackend.dao.CategoryDAO;
@Controller
public class PageController {
@Autowired
private CategoryDAO categoryDAO;
@RequestMapping(value = { "/", "/home", "/index" })
public ModelAndView index() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "Home");
mv.addObject("categories", categoryDAO.list());
mv.addObject("userClickHome", true);
return mv;
}
@RequestMapping(value = { "/about"})
public ModelAndView about() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "About-Us");
mv.addObject("userClickAbout", true);
return mv;
}
@RequestMapping(value = { "/contact"})
public ModelAndView contact() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "Contact-Us");
mv.addObject("userClickContact", true);
return mv;
}
@RequestMapping(value = { "/show/all/products"})
public ModelAndView showProduct() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "Show-Product");
mv.addObject("categories", categoryDAO.list());
mv.addObject("userClickshowAllProducts", true);
return mv;
}
@RequestMapping(value = { "/show/category/{id}/products"})
public ModelAndView showCategoryProducts(@PathVariable("id") int id) {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", categoryDAO.get(id).getName());
mv.addObject("categories", categoryDAO.list());
mv.addObject("category", categoryDAO.get(id));
mv.addObject("userClickshowCategoryProducts", true);
return mv;
}
/*@RequestMapping(value ="/test") public ModelAndView
test(@RequestParam(value="Greeting", required=false) String Greeting) { if
(Greeting == null) { Greeting = "helloo"; } ModelAndView mv = new
ModelAndView("page"); mv.addObject("Greeting", Greeting); return mv; }*/
/*
* @RequestMapping(value ="/test/{Greeting}") public ModelAndView
* test(@PathVariable(value="Greeting") String Greeting) { if (Greeting == null)
* { Greeting = "helloo"; } ModelAndView mv = new ModelAndView("page");
* mv.addObject("Greeting", Greeting); return mv; }
*/
}
HibernateConfig.java
package src.main.java.myshoppingbackend.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@ComponentScan(basePackages="src.main.java.myshoppingbackend.dto")
@EnableTransactionManagement
public class HibernateConfig {
// Change the below based on the DBMS you choose
private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/onlineshopping";
private final static String DATABASE_DRIVER = "org.h2.Driver";
private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
private final static String DATABASE_USERNAME = "sa";
private final static String DATABASE_PASSWORD = "";
// dataSource bean will be available
@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
// Providing the database connection information
dataSource.setDriverClassName(DATABASE_DRIVER);
dataSource.setUrl(DATABASE_URL);
dataSource.setUsername(DATABASE_USERNAME);
dataSource.setPassword(DATABASE_PASSWORD);
return dataSource;
}
// sessionFactory bean will be available
@Bean
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addProperties(getHibernateProperties());
builder.scanPackages("src.main.java.myshoppingbackend.dto");
return builder.buildSessionFactory();
}
// All the hibernate properties will be returned in this method
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", DATABASE_DIALECT);
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
//properties.put("hibernate.hbm2ddl.auto", "create");
return properties;
}
// transactionManager bean
@Bean
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
}
pom公司。xml(用于前端项目)
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>src.main.java</groupId>
<artifactId>myonlineshopping</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myonlineshopping Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>com.springsource.repository.bundles.release</id>
<name>SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases</name>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.external</id>
<name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
<url>http://repository.springsource.com/maven/bundles/external</url>
</repository>
</repositories>
<!-- Version information will be stored here -->
<properties>
<javaee.version>7.0</javaee.version>
<spring.version>4.3.6.RELEASE</spring.version>
</properties>
<dependencies>
<!-- link backend with frontEnd -->
<dependency>
<groupId>src.main.java</groupId>
<artifactId>myshoppingbackend</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- JUNIT -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- SPRING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- JAVA EE -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
<finalName>myonlineshopping</finalName>
</build>
</project>
pom.xml (后端)
<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>
<groupId>src.main.java</groupId>
<artifactId>myshoppingbackend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myshoppingbackend</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.6.RELEASE</spring.version>
<hibernate.version>5.2.7.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- JUNIT VERSION 4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- SPRING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- H2 Database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<!-- Hibernate Dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Database Connection Pooling -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<!-- Updated for the latest version of JAVA -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
创建名为“pageController”的bean时出错:通过字段“categoryDAO”表示的依赖项不满足要求;嵌套异常是org.springframework.beans.factory。UnsatisfiedDependencyException:创建名为“categoryDAO”的bean时出错:通过字段“sessionFactory”表示的依赖关系不满足;嵌套异常是org.springframework.beans.factory。NoSuchBeanDefinitionException:没有类型为“org.hibernate”的限定bean。SessionFactory可用:需要至少1个bean,它可以作为autowire候选。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
>
Hibernate配置
类下面是一个基于代码的 Web 方法的示例.xml
public class WebAppConfig extends
AbstractAnnotationConfigDispatcherServletInitializer {
//Tell spring to use HibernateConfig as a root application context
where your business services are located
protected Class<?>[] getRootConfigClasses() {
return new Class[]{HibernateConfig.class};
}
//Tell spring to use MvcAppConfig as a dispatcher servlet context
where your controllers are located
protected Class<?>[] getServletConfigClasses() {
return new Class[]{MvcAppConfig.class};
}
// dispatcher servlet mapping
protected String[] getServletMappings() {
return new String[]{"/mvc/*"};
}
}
这里是dispatcher servlet上下文类的一个例子
@Configuration
@EnableWebMvc
@ComponentScan("package where your controllers are located")
public class MvcAppConfig {
}
我有一个使用Maven创建的spring项目。下面是我的目录结构。 我想添加css图像到这个项目。为此,我在web-inf目录中创建了一个resources文件夹,并在其中放置了一个图像。对于我的dispatcher servlet xml,我添加了3行与MVC有关的内容。其中一行位于xmlns:mvc中,另外2行位于模式位置的最后2行: http://www.springframework.or
我是Maven和Jenkins的新手,所以请耐心等待我... 我有一个Maven项目,当我从项目文件夹中运行mvn install时,它可以正确构建,但是当我尝试通过Jenkins构建时,根本无法解决任何依赖项。我是否在我的 Maven 或 Jenkins 设置中遗漏了某些内容/搞砸了某些内容? 项目POM和. m2/settings.xml如下... pom.xml .m2/设置.xml
在项目首页的onShow中注册JSSDK的时候把当前域名存入本地:判断当前域名和本地存储的域名是否一致时再进行JSSDk注册,当微信公众号网页版项目某一页刷新会影响JSSDK吗?(刷新之后进不去wx.ready和wx.error)
我试图使用Eclipse的从maven开始。但如果我尝试将现有项目更改为Maven项目(),结果是: null 问题可能出在哪里? 更新(生成的pom.xml):
我有一个父POM项目,它将spring版本声明为3.2,将hiberate实体管理器版本声明为4.1.1(让我们称之为parent,P)。我有另一个项目,它是这个父级的子级,并继承了提到的依赖项管理(让我们将子级称为C1)。我正在构建一个新的项目,我希望该项目对所提到的父级的子级有影响,并且也依赖于子级C1,(让我们把这个新的子级称为C2)。 父P1(父Pom项目,声明spring 3.2和hib
我想在我的linux终端上编译一个Netbeans Maven-Java应用程序。 这是因为我想使用Git CVS并能够在独立于IDE的服务器中编译项目。 但我很难真正理解依赖关系是如何工作的,或者我就是这么想的。 我正在使用一个简单的java应用程序代码连接到mysql数据库并获取一些信息。 我正在使用此代码: 然后将mysql连接器java添加到项目依赖项pom中。xml如下所示: 如果我将n