Spring注解

来源:互联网 发布:马云网络平台 编辑:程序博客网 时间:2024/05/16 00:49

dao层注解

@Repository("carDao")


Server层注解

@Service("userService")

@Transactional

自动注入

@Autowired

CarUserDao carUserDao;

控制层

@Controller("caruserAction")

public class CarUserAction extends ActionSupport {

@Autowired

private CarUserServicecarUserService;

Struts映射文件class改成事务管理的id(spring 自动注入 自动实例化)

<actionname="user_*"class="userAction"

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"><!-- 使用 注解 --><context:annotation-config /><!-- --><context:component-scan base-package="com.yzk"></context:component-scan><!-- 数据库连接池 --><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/CarSystem" /><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="mappingResources"><list><value>com/yzk/carsystem/model/CarMaster.hbm.xml</value><value>com/yzk/carsystem/model/User.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> --></props></property></bean><!-- 事务管理bean --><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>




0 0
原创粉丝点击