解决方案 »

  1.   


    下面是基本的几个项目文件:<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"   
           xmlns:aop="http://www.springframework.org/schema/aop" 
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context    
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
         <context:annotation-config/>
         <context:component-scan base-package="muyu"/>
         <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="false">
         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
         <property name="username" value="root"/>
         <property name="password" value="123"/>
         <property name="initialSize" value="1"/>
         <property name="maxIdle" value="2"/>
         <property name="minIdle" value="1"/>
         </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
     <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    <property name="loadTimeWeaver">  
                 <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>  
             </property>
    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
       <property name="entityManagerFactory" ref="entityManagerFactory"/>
      </bean>
     
         <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans><?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/persistence_1_0.xsd" version="1.0">  <persistence-unit name="muyu" transaction-type="RESOURCE_LOCAL">  
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <properties>  
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />  
        <property name="hibernate.max_fetch_depth" value="3" />  
        <property name="hibernate.show_sql" value="false" />  
        <property name="hibernate.format_sql" value="false" />  
        <property name="hibernate.hbm2ddl.auto" value="update"/>  
       </properties>  
    </persistence-unit>
    </persistence>package muyu.service.product.impl;import java.util.UUID;import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;import muyu.bean.product.Brand;
    import muyu.dao.Daosuppot;
    import muyu.service.product.BrandService;@Service
    @Transactional
    public class BrandServiceBean extends Daosuppot implements BrandService { @Override
    public void sava(Object entity) {
    ((Brand)entity).setCode(UUID.randomUUID().toString());
    super.sava(entity);
    }
    }
    package muyu.service.product;import muyu.dao.DAO;public interface BrandService extends DAO{}
    package muyu.bean.product;import java.io.Serializable;import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;@Entity
    public class Brand implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public Brand(String name, String logopath) {
    this.name = name;
    this.logopath = logopath;
    }
    public Brand() {
    }
    @Id @Column(length=36)
    public String getCode() {
    return code;
    }
    public void setCode(String code) {
    this.code = code;
    }
    @Column(nullable=false,length=40)
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    @Column(nullable=false)
    public Boolean getVisible() {
    return visible;
    }
    public void setVisible(Boolean visible) {
    this.visible = visible;
    }
    @Column(length=3000)
    public String getLogopath() {
    return logopath;
    }
    public void setLogopath(String logopath) {
    this.logopath = logopath;
    }
    private String code;
    private String name;
    private Boolean visible=true;
    private String logopath;
    }
      

  2.   

    interface BrandService extends DAO ?? 你这service继承 dao??  应该是在你的 service 实现类 
    注入需要 使用的dao把
     
    @Service
    @Transactional
    public class BrandServiceBean extends Daosuppot implements BrandService {
        @Autowired
        Dao dao;
     
        @Override
        public void sava(Object entity) {
            ((Brand)entity).setCode(UUID.randomUUID().toString());
            dao.sava(entity);
        }
    }