1.在html/jsp中使用Struts标签
<span style="font-size:18px;"><%@ taglib prefix="s" uri="/struts-tags" %></span>
需要在<head>之前加上Struts的标签申明,然后可以使用s标签
<span style="font-size:18px;"><span style="color:#3333ff;"><img src="imageFile/<s:property value="uploadFileName"/>"></img></span></span>
<s:property value="变量名"/>
2.action层
<span style="color:#3333ff;">public class UserInfoAction extends postAction {
private static final long serialVersionUID = -6144396976094110475L;
@Resource
private UserInfoService userInfoService;
private UserInfo result;
@Override
public String execute() throws Exception{
this.getResponse().setContentType("text/html;charset=utf-8");
this.getResponse().setCharacterEncoding("UTF-8");
</span><span style="color:#990000;">String phoneNum = this.getRequest().getParameter("phoneNum");
String password = this.getRequest().getParameter("password");</span><span style="color:#3333ff;">//从url获得数据,url传值为&password=xxx
result = userInfoService.GetUserInfo(phoneNum, password);
JSONObject json = new JSONObject();
json.accumulate("UserInfo", result);
if (result != null) {
printJson(json.toString());
return SUCCESS;
}
else{
printError();
return ERROR;
}
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
}</span>
其中service必须要有set方法,注意标识
service层
public interface UserInfoService {
public UserInfo GetUserInfo(String PhoneNum,String Password);
}
impl
@Component
public class UserInfoServiceImpl implements UserInfoService {
@Resource
private UserInfoDao userInfoDao;
public void setUserInfoDao(UserInfoDao userInfoDao) {
this.userInfoDao = userInfoDao;
}//必须要有set方法
@Override
public UserInfo GetUserInfo(String PhoneNum,String Password) {
return userInfoDao.getUserInfoByPhoneNum(PhoneNum,Password);
}
}
dao层
public interface UserInfoDao {
public UserInfo getUserInfoByPhoneNum(String PhoneNum,String password);
}
impl
<span style="color:#3333ff;">@Component
public class UserInfoDaoImpl implements UserInfoDao {
//把这个类注入bean
@Resource
//bean
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public UserInfo getUserInfoByPhoneNum(String PhoneNum,String Password) {
Session session = sessionFactory.getCurrentSession();
String hql="</span><span style="color:#cc0000;">from UserInfo u where u.PhoneNum=:phonenum and u.Password=:password</span><span style="color:#3333ff;">";
Query query =session.createQuery(hql);
query.setString("phonenum", PhoneNum);
query.setString("password", Password);
UserInfo u=(UserInfo) query.uniqueResult();
if(u!=null){
System.out.println(u.getId()+u.getPhoneNum());
return u;
}
else{
return null;
}
}
}</span>
3.数据库增删改查
查询
String hql="from MonthDataInfo u where u.phoneNum=:phonenum";
Query query =session.createQuery(hql);
query.setString("phonenum", monthDataInfo.getPhoneNum());
answer=(MonthDataInfo) query.uniqueResult();
HibernateTemplate ht = new HibernateTemplate(sessionFactory);
ht.save(monthDataInfo);
String hql_u="update MonthDataInfo u set u.monthDataVolume=:dataVolume where u.phoneNum=:phoneNum";
Query query_u =session.createQuery(hql_u);
query_u.setString("phoneNum",monthDataInfo.getPhoneNum());
query_u.setFloat("dataVolume",monthDataVolume);
int ret=query_u.executeUpdate();//ret=1时更新成功
删除
String hql_d = "delete from FriendshipsInfo u where u.friendPhoneNum=:friendPhoneNum and u.phoneNum=:phoneNum";
Query query_d = session.createQuery(hql_d);
query_d.setString("friendPhoneNum", friendshipsInfo.getFriendPhoneNum());
query_d.setString("phoneNum", friendshipsInfo.getPhoneNum());
int ret = query_d.executeUpdate();<span style="font-family:Arial, Helvetica, sans-serif;">//ret=1时删除成功</span>