我是春天的新手,我尝试使用hibernate从实体类创建表但它永远不会工作,这是我的spring-cinfig.xml:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:8889/testeleve"/>
<property name="username" value="root"/>
<property name="password" value="yassine"/>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="database" value="MYSQL"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- spring based scanning for entity classes-->
<property name="packagesToScan" value="Class"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>'
这是我的测试类:
@Autowired
private static CrudRepository repository;
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
createEleve(22, "Saint", "Peter");
createEleve(23, "Jack", " Dorsey");
createEleve(24, "Sam", "Fox");
}'
以下是例外:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [TestEleve2/resources/spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [TestEleve2/resources/spring-config.xml] cannot be opened because it does not exist
答案 0 :(得分:0)
您应该指定 spring-config.xml 所在的位置。尝试这样的事情:
@Autowired
private static CrudRepository repository;
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/resources/spring-config.xml");
createEleve(22, "Saint", "Peter");
createEleve(23, "Jack", " Dorsey");
createEleve(24, "Sam", "Fox");
}
<强>更新强>
MVC-调度-servlet.xml中
<import resource="spring-config.xml"/>
答案 1 :(得分:0)
只需做一件事就是将你的文件spring-config.xml放入 TestEleve2 / resources / 路径并尝试再次运行程序,必须运行它。
答案 2 :(得分:0)
在web.xml中添加以下代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>YourApp</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>YourApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>YourApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在这里,web.xml尝试查看实际上是您的web.xml的YourApp-servlet.xml文件。 您需要将spring-config.xml替换为YourApp-servlet.xml。
此处YourApp是您的应用程序名称。将其放在 webapps 文件夹中。