hibernate整合spring是,控制台没包任何异常,但是就是不能自动创建表

化合物 发布于 2016/04/23 13:22
阅读 179
收藏 0

一下是习惯的xml配置跟Java代码

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>myshop</display-name>
  <welcome-file-list>
    <welcome-file>customer_login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 用到的xml文件配置 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:config/springAnnotation-*.xml</param-value>
  </context-param>
  
  <!-- spring配置 -->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
 <!-- springMVC配置-->
 <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- 字符集配置 -->
 <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
  <param-name>forceEncoding</param-name>
  <param-value>true</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <filter>
  <filter-name>openSession</filter-name>
  <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>openSession</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>
关于hibernate的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>


<beans>
<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/myshop"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="get*">PROPAGATION_NEVER</prop>
</props>
</property>
</bean>
</beans>

关于spring的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 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-3.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 扫描注解包 -->
<context:component-scan base-package="cn.zmf"/>
<!-- 开启注解 -->
<context:annotation-config/>
<mvc:annotation-driven/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <property name="defaultEncoding" value="utf-8"/>
      <!-- <property name="maxUploadSize" value="10485760000"/>
      <property name="maxInMemorySize" value="40960"/>
      -->
      </bean>
</beans>
Java代码实体类

package cn.zmf.entity;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


import org.hibernate.annotations.GenericGenerator;


@Entity
@Table(name="t_customer")
public class Customer {

@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid",strategy="uuid")
@Column(length=36)
private String customerId;
@Column(length=32)
private String customerName;
@Column(length=32)
private String customerSex;
@Column(length=32)
private String customerAge;
@Column(length=32)
private String customerPassword1;
@Column(length=32)
private String customerPassword2;

public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerSex() {
return customerSex;
}
public void setCustomerSex(String customerSex) {
this.customerSex = customerSex;
}
public String getCustomerAge() {
return customerAge;
}
public void setCustomerAge(String customerAge) {
this.customerAge = customerAge;
}
public String getCustomerPassword1() {
return customerPassword1;
}
public void setCustomerPassword1(String customerPassword1) {
this.customerPassword1 = customerPassword1;
}
public String getCustomerPassword2() {
return customerPassword2;
}
public void setCustomerPassword2(String customerPassword2) {
this.customerPassword2 = customerPassword2;
}
}


Java代码Dao

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Service;


import cn.zmf.entity.Customer;


@Service(value="customerLoginDao")
public class CustomerLoginDao implements ICustomerLoginDao {

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
@Override
public void register(Customer customer) {
try {
Session session = sessionFactory.getCurrentSession();
session.save(customer);
session.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

service代码

import javax.annotation.Resource;


import org.springframework.stereotype.Service;


import cn.zmf.dao.CustomerLoginDao;
import cn.zmf.entity.Customer;


@Service(value="customerLoginService")
public class CustomerLoginService implements ICustomerLoginService {

@Resource(name="customerLoginDao")
private CustomerLoginDao customerLoginDao;
@Override
public void register(Customer customer) {
customerLoginDao.register(customer);
}


}

controller代码

import javax.annotation.Resource;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import cn.zmf.entity.Customer;
import cn.zmf.service.CustomerLoginService;


@Controller
@RequestMapping("/customer")
public class CustomerLoginController {
@Resource(name="customerLoginService")
private CustomerLoginService customerLoginService;

@RequestMapping("/register")
public String register(Customer customer){
customerLoginService.register(customer);
return "/customerregister";
}
}


加载中
返回顶部
顶部