SSH纪实--最基本最基本的知识(二)

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>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值