SSH整合开发基本步骤
第一步 创建表结构
根据需求创建表结构如:用户(user)用户名,用户密码。。。。
一般是BI把表都创建好了
第二步 创建实体
根据表结构创建实体类(一般采用注解方式)
@Entity :实体标识
@Table(name = "ia_sys_user") :如果实体名和数据库表名不同时,在这要说明是哪一个表与这个实体相关联
@Cache(usage =CacheConcurrencyStrategy.READ_WRITE) :给实体类配置读写缓存
@Temporal(TemporalType.TIMESTAMP) :时间类型
@Column(name = "user_name", length = 60) :属性名和字段名不同时,要关联属性对应的字段名
一般在属性的get方法上
@ManyToMany(fetch=FetchType.EAGER):多对多的定义
// 中间表定义,表名采用默认命名规则
@JoinTable(name = "ia_sys_user_role",joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name= "role_id")}) :中间表的定义,注意表名
@Fetch(FetchMode.SUBSELECT):fetch策略
@OrderBy("id") : 按id排序
@Transient :非持久化的属性
@SuppressWarnings("unchecked") :忽略警告
第三步 创建dao类
@Component /@Repository :组件注入
public class UserDao extends HibernateDao<User,Long> : Long是User的主键
写hql语句等如下
1) public booleanisExist(String userCode){
Stringhql="select count(u) from User u whereu.userCode=?";
Longcount = (Long) createQuery(hql, userCode).uniqueResult();
return count>0;
}
2)public VehicleretrievedDataByDeviceId(String deviceId){
Stringhql="fromVehicle v where v.deviceId=?";
return (Vehicle)createQuery(hql,deviceId).uniqueResult();
}
第四步 创建service类
配置@Service :注入
@Transactional
@Autowired 注入dao类(一般写在set方法上)
写一些逻辑在方法里,调用dao中的方法,处理一些业务,spring是通过属性注入值
@Resource spring是通过名称注入值
如:public booleansave(List<Enterprise>enterprises) {
Enterprise enterprise = null;
try {
if (enterprises.size() == 0) {
return false;
}
for (int i = 0; i < enterprises.size(); i++) {
enterprise= enterprises.get(i);
enterpriseDao.save(enterprise);
}
} catch (Exception e) {
e.printStackTrace();
log.info("保存企业" + enterprise.getName() + "出错");
return false;
}
return true;
}
第五步 创建action类
@Namespace("/account") : 配置命名空间
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "student.action",type = "redirect") }) : 结果返回到那个action中
@Action("student") : 返回的action的名字(其中@Result可以配置多个表示,action按照return 值匹配name值,跳转对应的location地址)
action要继承CrudActionSupport
public classStudentAction extendsCrudActionSupport<Student>
注意要注入service类
如:
public String list() throwsException {
List<PropertyFilter> filters =PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
for(PropertyFilter filter:filters){
if(filter.getPropertyName().equals("studentNo")) {
studentNo=(String) filter.getMatchValue();
}
}
studentService.queryStudent(page, filters);
return SUCCESS;
}
第六步 创建jsp文件
form的创建注意其属性名,样式,还有相应的事件
<form id="studentForm" action="student.action"method="post">
<div id="filter">
工号: <input type="text" name="filter_LIKES_studentNo" value="${studentNo}"size="9"/>
</div>
<input id="student_query"type="button"value="查询"/>
<div id="content">
<table id="contentTable" style="border: 0px;">
<tr>
<td>姓名</td>
<td>成绩</td>
</tr>
<s:iterator value="page.result">
<tr>
<td>${studentName}</td>
<td>${studentScore}</td>
</tr>
</s:iterator>
</table>
</div>
</form>
第七步 调试程序
设置断点调式,用火狐firebug调式jsp文件