我收到了问题中的错误。我的道实现类如下:
package com.argus.intenew;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Autowired
AnnotationSessionFactoryBean sessionFactory;
public UserDaoImpl() {
}
public AnnotationSessionFactoryBean getCurrentSessionFactory() {
return sessionFactory;
}
public void setCurrentSessionFactory(AnnotationSessionFactoryBean sessionfactory) {
this.sessionFactory = sessionfactory;
}
@Override
public void addUser(UserMap userMap) {
System.out.println("33333333333333333333");
getHibernateTemplate().save(userMap);
}
@Override
public List<User> findAllUser() {
return getHibernateTemplate().find("from User");
}
@Override
public void deleteUser(UserMap user) {
getHibernateTemplate().delete(user);
}
@Override
public void updateUser(UserMap user) {
getHibernateTemplate().update(user);
}
}
而我的配置类如下:
package com.argus.intenew;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
@Configuration
@ComponentScan(basePackages = {"com.argus.intenew"})
public class Webconfig {
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "org.hibernate.dialect.MySQLDialect ";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "true";
@Bean
public DataSource dataSource() {
System.out.println("----------InDATAsource------------");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/MyUser");
dataSource.setUsername("root");
dataSource.setPassword("XXX");
System.out.println("----------OutofDATAsource------------");
return dataSource;
}
@Bean
public AnnotationSessionFactoryBean sessionFactory() {
System.out.println("----------InsessionFactory------------");
AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
String[] pckage={"com.argus.intenew"};
sessionFactory.setPackagesToScan(pckage);
sessionFactory.setHibernateProperties(hibProperties());
System.out.println("----------Outof session------------");
return sessionFactory;
}
private Properties hibProperties() {
System.out.println("----------InhipProp------------");
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, "org.hibernate.dialect.MySQLDialect ");
properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, "true");
System.out.println("----------outofhip------------");
return properties;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory().getObject());
return transactionManager;
}
@Bean
public UserBoImpl userBo() {
System.out.println("----------InUserBo------------");
UserBoImpl userBo= new UserBoImpl();
userBo.setUserDao(userDao());
System.out.println("----------OutofUserbo------------");
return userBo;
}
@Bean
public UserDaoImpl userDao() {
System.out.println("----------InUserDao------------");
UserDaoImpl userDao=new UserDaoImpl();
userDao.setCurrentSessionFactory(sessionFactory());
System.out.println("----------OutofUserDao------------");
return userDao;
}
}
任何人都请帮助并告诉我使用注释执行此操作的正确方法是什么。这里我没有使用任何 xml 文件。