hibernate3
生活随笔
收集整理的這篇文章主要介紹了
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";}
轉載于:https://www.cnblogs.com/gjmfg/p/5375445.html
總結
以上是生活随笔為你收集整理的hibernate3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 团队
- 下一篇: atitit.userService 用