Spring jpa配置错误

时间:2016-07-28 22:13:19

标签: spring jpa

Error on Console Picture : Error.png



package com.journaldev.spring.jdbc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.journaldev.spring.jdbc.model.Employee;

public class EmployeeDAOJDBCTemplateImpl implements EmployeeDAO {

	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	@Override
	public void save(Employee employee) 
	{
		String query = "insert into Employee (id, name, role) values (?,?,?)";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		Object[] args = new Object[] {employee.getId(), employee.getName(), employee.getRole()};
		int out = jdbcTemplate.update(query, args);
		if(out !=0)
		{
			System.out.println("Employee saved with id="+employee.getId());
		}
		else System.out.println("Employee save failed with id="+employee.getId());
	}
}






package com.journaldev.spring.jdbc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import com.journaldev.spring.jdbc.model.Employee;

public class EmployeeDAOImpl implements EmployeeDAO
{

	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) 
	{
		this.dataSource = dataSource;
	}

	@Override
	public void save(Employee employee)
	{
		String query = "insert into Employee (id, name, role) values (?,?,?)";
		Connection con = null;
		PreparedStatement ps = null;
		try
		{
			con = dataSource.getConnection();
			ps = con.prepareStatement(query);
			ps.setInt(1, employee.getId());
			ps.setString(2, employee.getName());
			ps.setString(3, employee.getRole());
			int out = ps.executeUpdate();
			if(out !=0)
			{
				System.out.println("Employee saved with id="+employee.getId());
			}
			else System.out.println("Employee save failed with id="+employee.getId());
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				ps.close();
				con.close();
			} 
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}






package com.journaldev.spring.jdbc.dao;

import java.util.List;
import com.journaldev.spring.jdbc.model.Employee;

public interface EmployeeDAO 
{
	public void save(Employee employee);
}






package com.journaldev.spring.jdbc.main;

import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.journaldev.spring.jdbc.dao.EmployeeDAO;
import com.journaldev.spring.jdbc.model.Employee;

public class SpringMain 
{
	public static void main(String[] args)
	{
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
		EmployeeDAO employeeDAO = ctx.getBean("employeeDAOJDBCTemplate", EmployeeDAO.class);
		Employee emp = new Employee();
		emp.setName("Sarsoura Ena");
		emp.setRole("PDG");
		employeeDAO.save(emp);
		ctx.close();
		System.out.println("DONE");
	}
}




我用jpa和spring创建了一个动态的web项目,但是我遇到了这个错误: 无法找到或加载主要类com.journaldev.spring.jdbc.main.SpringMain 你能告诉我我错过了什么吗? 贝娄是:

的persistence.xml



<?xml version="1.0" encoding="UTF-8"?>  
<persistence xmlns="http://java.sun.com/xml/ns/persistence"  
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   
                              http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">  
  <persistence-unit name="HelloSpring" transaction-type="RESOURCE_LOCAL">  
        <class>com.journaldev.spring.jdbc.model.Employee</class>  
  <provider>org.hibernate.ejb.HibernatePersistence</provider>  
  <properties>  
          <property name="hibernate.show_sql" value="true" />  
  <property name="hibernate.hbm2ddl.auto" value="update" />  
  </properties>  
  </persistence-unit>  
</persistence>  
&#13;
&#13;
&#13;

spring.xml

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
      http://www.springframework.org/schema/beans/spring-beans.xsd">  
  <bean id="employeeDAO" class="com.journaldev.spring.jdbc.dao.EmployeeDAOImpl">  
  <property name="dataSource" ref="dataSource" />  
  </bean>  
  
  <bean id="employeeDAOJDBCTemplate" class="com.journaldev.spring.jdbc.dao.EmployeeDAOJDBCTemplateImpl">  
  <property name="dataSource" ref="dataSource" />  
  </bean>  
  
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  
  
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  <property name="url" value="jdbc:mysql://localhost:3306/springdb" />  
  <property name="username" value="root" />  
  <property name="password" value="root" />  
  </bean>  
  
  
  <bean id="persistenceUnitManager"  
   class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">  
  <property name="persistenceXmlLocations">  
  <list>  
  <value>persistence.xml</value>  
  </list>  
  </property>  
  <property name="defaultDataSource" ref="dataSource"></property>  
  </bean>  
  
  
  <bean id="entityManagerFactory"  
   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  <property name="persistenceUnitManager" ref="persistenceUnitManager"></property>  
  <property name="persistenceUnitName" value="HelloSpring"></property>  
  </bean>  
  
  
  
  
  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
     <property name="entityManagerFactory" ref="entityManagerFactory"></property>  
  </bean>  
  
  
  
  
</beans>  
&#13;
&#13;
&#13;

提前致谢

0 个答案:

没有答案