一,场景:编写一个用户管理的服务层,包括增加用户、删除用户和查询用户等等,而且用户信息以关系型数据库形式存储。
分析:
这是一个典型的应用服务,涉及到业务层(business objects layer)、数据库访问层(db access)和存储层(eis)。
采用典型的架构:bo+dao
dao采用spring提供的jdbctemplate来进行封装。
二,具体实现
1)用户类user
public class user{
private string username;
private string email;
private date birthday;
public user(){}
…
相应的setter/getter方法。
}
这是一个简单的bean类,记录用户的信息。
2)数据库访问类:userdao
public interface userdao {
public void save(user user);
public void remove(string username);
public user searchbyname(string username);
}
这是数据库访问的接口类,定义了对用户的数据库操作,其实现类是userdaojdbc
userdaojdbc类
public class userdaojdbc extends jdbcdaosupport implements userdao {
public userdaojdbc() {
super();
}
public void save(user user) {
getjdbctemplate().update(
"insert into sample_user values (?,?,?)",
new object[] { user.getusername(), user.getemail(),
user.getbirthday() });
}
...
}
3)业务类usermanageservice
public interface usermanageservice {
public void saveuser(user user);
public void removeuser(string username);
public user getuserbyname(string username);
}
这是用户的业务类接口,定义了和用户相关的一些业务操作,其实现类usermanageserviceimpl
实现类usermanageserviceimpl
public class usermanageserviceimpl implements usermanageservice {
private userdao udao;
public usermanageserviceimpl() {
super();
}
public void saveuser(user user) {
udao.save(user);
}
public userdao getudao() {
return udao;
}
public void setudao(userdao udao) {
this.udao = udao;
}
}
4)spring bean描述文件配置
<beans>
<bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
<property name="driverclassname">
<value>oracle.jdbc.driver.oracledriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@172.16.4.219:1521:orcl</value>
</property>
<property name="username">
<value>zhangbo</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
<bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
<property name="datasource">
<ref bean="datasource"/>
</property>
</bean>
<bean id="userdao" class="sample.spring.dao.userdaojdbc">
<property name="datasource">
<ref bean="datasource"/>
</property>
</bean>
<bean id="usermanageservice" class="sample.spring.service.usermanageserviceimpl">
<property name="udao">
<ref bean="userdao"/>
</property>
</bean>
</beans>
5)数据中存储用户的表sample_user
username varchar2(30) not null,
email varchar2(50) not null,
birthday date
6)通过单元测试来验证usermanageservice中的操作:
public class usermanageservicetest extends testcase {
private usermanageservice service;
private static log logger = logfactory.getlog(usermanageservicetest.class);
protected void setup() throws exception {
super.setup();
service = (usermanageservice) springcontextutil
.getbean("usermanageservice");
}
public void testsaveandsearchandremoveuser() throws exception {
user user = new user();
user.setusername("zhangsan");
user.setemail("[email protected]");
user.setbirthday(new date());
service.saveuser(user);
logger.debug("存入一个新用户:" + user);
user userfromdb = service.getuserbyname("zhangsan");
assertnotnull(userfromdb);
assertequals(userfromdb.getemail(), "[email protected]");
logger.debug("从数据库中查到用户:" + userfromdb);
service.removeuser("zhangsan");
logger.debug("从数据库删除用户:" + userfromdb);
}
protected void teardown() throws exception {
super.teardown();
}
}
新闻热点
疑难解答