1、导入spring 及Mysql's jdbc包2、代码的输入
AOP类package cc.apl330.aspect; public class UserAspect { public void before() { System.out.println("method before!"); } }
数据库操作package cc.apl330.dao; import cc.apl330.model.User; public interface IDAO { public void save(User user) ; }
数据库操作package cc.apl330.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.sql.DataSource; import cc.apl330.model.User; public class UserDAO implements IDAO { private DataSource dataSource ; public void save(User user) { int id = user.getId(); String name = user.getName() ; String sql = "INSERT INTO USER(_id,_name) VALUE(?,?)" ; Connection conn = null ; PreparedStatement ps = null ; try { conn = dataSource.getConnection() ; ps = conn.prepareStatement(sql) ; ps.setInt(1, id) ; ps.setString(2, name) ; ps.executeUpdate() ; } catch (SQLException e) { e.printStackTrace(); }finally{ try { ps.close() ; conn.close() ; } catch (SQLException e) { e.printStackTrace(); } } } public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } }
业务层package cc.apl330.service; import cc.apl330.dao.IDAO; import cc.apl330.dao.UserDAO; import cc.apl330.model.User; public class UserService { private UserDAO userdao = null ; public void add(User user) { userdao.save(user) ; } public UserDAO getUserdao() { return userdao; } public void setUserdao(UserDAO userdao) { this.userdao = userdao; } }
测试类package cc.apl330.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cc.apl330.model.User; public class UserServiceTest { @Test public void save() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml") ; UserService userService = (UserService) ctx.getBean("userService"); User user = new User() ; user.setName("apl330") ; userService.add(user); } }
sping配置文件<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="dao" class="cc.apl330.dao.UserDAO" > <property name="dataSource" ref="dataSource"></property> </bean> <bean id="user" class="cc.apl330.model.User" scope="prototype"></bean> <bean id="userService" class="cc.apl330.service.UserService"> <property name="userdao" ref="dao"></property> </bean> <bean id="addbefore" class="cc.apl330.aspect.UserAspect"></bean> <aop:config> <aop:pointcut expression="execution(public * cc.apl330.service.*.add(..))" id="userpoincut"/> <aop:aspect id="useraspect" ref="addbefore" > <aop:before method="before" pointcut-ref="userpoincut"/> </aop:aspect> </aop:config> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring"></property> <property name="username" value="root"></property> <property name="password" value="359848184"></property> </bean> </beans>
posted on 2010-12-11 19:09 jack zhai 阅读(345) 评论(0) 编辑 收藏 所属分类: 百味人生
Powered by: BlogJava Copyright © jack zhai