67,549
社区成员




package com.annotation.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.annotation.dao;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.annotation.entity.User;
@Component("u")
public class UserDao {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void save(User user) {
// this.getHibernateTemplate().save(user);
System.out.println("user saved");
}
}
package com.annotation.service;
import javax.annotation.Resource;
//import org.springframework.stereotype.Component;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.annotation.dao.UserDao;
import com.annotation.entity.User;
@Component("userService")
public class UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
@Resource(name="u")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(User user) {
userDao.save(user);
}
}
<?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"
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
">
<context:annotation-config />
<context:component-scan base-package="com.annotation" />
<!-- more bean definitions go here -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/spring" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.annotation.entity.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
package com.annotation.dao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.annotation.entity.User;
import com.annotation.service.UserService;
public class UserDaoTest {
@Test
public void testSave() {
ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) act.getBean("userService");
service.add(new User());
}
}
1、定义接口
public interface UserService {
public void add(User user);
}
2、定义接口实现类
@Component("userService")
public class UserServiceImpl implements UserService {
@AutoWired
private UserDAO userDao;//此处dao的定义跟service一样,需要dao接口和dao实现类
public void add(User user){
userDao.save(user);
}
}