hibernate3

hibernate3
(整合到spring中的core核心配置中的hibernate3)
<!-- 基于hibernate的Session工厂 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<!-- 数据源,在这里使用了第三方的连接池当做数据源 ,该参考类要实现了sql.dateSource接口 -->
		<property name="dataSource" ref="dbcp_dateSource">
		</property>

		<!-- 将hibernate的设置参数引入Spring配置,如此就不用单独设置hibernate配置文件 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
			</props>
		</property>

		<!-- 要扫描的包,导入包内注解好映射的实体类 -->
		<property name="packagesToScan" value="com.twogold.dto"></property>

	</bean>
1.创建实体类,在实体类中注解每一项属性,并对对应关系进行描述(一对一,一对多,多对多)。

2.根据业务接口层中的方法给出dao层接口。

3.实现dao层接口中的方法,通过
@Repository(value=“projectDao”)
(这是为这一个实现类取名字,以便后面注解根据名字调用);然后在接口实现类中聚合一个
@Resource(name="sessionFactory")
priavte  SessionFactory sf;(这样就能得到session工厂。(但是为了提高session的利用率  就通过sf.getCrrentSession()这样每次都用的是当前的session,避免了每次都得到一个

新session)


4.封装业务逻辑层接口;
5.给出业务逻辑层接口的实现,
接口实现类中的注解
(类外注解)
@Service() 
@Transactional(propagation=propagation.REQUIRED)(事务注解)

(类中聚合一个dao层接口)
(类内注解)
@Resource(name=“projectDao”)(这那么是dao层事项类中为类开始取的名字,如果么有取名字,那么就该和实现类的名字一样。)
priavet IProjectDao dao;


6.前后台之间中央控制类,该类对前台页面的请求调用对应的方法;
(类外注解)
@Controller
(类内注解)
@Resource
private IProjectservice service;(聚合业务逻辑接口类)
@Resource(name=“systemParameSetting”)
private ISysParSettingService sss;

@RequestMapping(value=“{id}/view”)
//@RequestBody 返回一个json格式的数据   
public String viewProject(
			@PathVariable("id") int id,({id}大括号用@PathVariable方法参数)
			HttpServletRequest req
		){
		
		Project project = service.getProjectById(id);
		//return 吧对象转为json对象
		req.setAttribute("project", project);
		
		return "projectDeclare/view";


@RequestMapping("/update")
	public String update(
			@ModelAttribute Project p,
			HttpServletRequest req
		){
		
		
		service.updateProject(p);
		return null;
	}


@RequestMapping("/year")
	public String getYear(
		@RequestParam(value="areaId", defaultValue="2") int areaId,(为参数指定默认值和类型)
		HttpServletRequest req
	){
		List<String> year=service.findYear(areaId);
		req.setAttribute("year",year);
		return "projectDeclare/reqquisition_collect_list";
	}

@RequestMapping("/{id}/update")
	public String updateProject(
			@PathVariable("id") int id,
			@RequestParam("assistanceFinancing") String af,(应该是前台传过来时的name)
			@RequestParam("otherFinancing") String of,
			@RequestParam("selfFinancing") String cf,
			HttpServletRequest req
		){
		int iaf = new Integer(af);
		int iof = new Integer(of);
		int icf = new Integer(cf);
		int totil = iaf + iof+ icf;
		
		Project p = service.getProjectById(id);
		
		p.setAssistanceFinancing(iaf);
		p.setOtherFinancing(iof);
		p.setSelfFinancing(icf);
		p.setTotal(totil);
		
		service.updateProject(p);
		
		
		return "redirect:/1/check";
	}



posted @ 2016-04-10 20:59  goumingming  阅读(281)  评论(0编辑  收藏  举报