나는 전에 .NET 관련 웹 개발과 접촉했지만 지금은 Javaweb을 사용하여 여전히 불편합니다.
1. 프로젝트 아키텍처 <br />에 대한 이야기 . 일어나서 Java를 백엔드, 즉 서비스 제공 업체로 사용하여 Java 플랫폼의 모든 비즈니스 로직을 완료하고 웹 개발에 더 익숙한 .NET을 사용하십시오. 이런 식으로 Android 앱과 웹면이 있습니다. 클라이언트는 분산 프레임 워크를 통해 서비스를 균일하게 호출합니다. 오랫동안 검색 한 후, 나는 마침내 가볍고, 교차-언어, 크로스 플랫폼, 비 침습적, 고성능 동적 원격 객체 호출 엔진 라이브러리 인 HProse를 선택했습니다. 내가 그것을 선택한 이유는 학습 비용이 낮기 때문입니다. 반면에, 교차 플랫폼 호출은 매우 쉽고 효율적입니다. 왜냐하면 우리는 .net을 사용하여 웹을 사용하여 Java가 게시 한 서비스를 호출해야하기 때문입니다! HPROSE 문서를 대략 살펴본 후, 내장 된 HPROSESERVLET 릴리스 서비스를 사용하는 것이 더 빠르고 개발하기가 더 간단하다는 것을 알았 으므로이 방법을 사용하여 서비스를 게시 할 계획입니다. 그러나 문제는 전통적인 SSH 아키텍처가 약간 무거워진다는 것입니다. .
2. 데이터베이스 설계
작은 온라인 서점이므로 디자인은 여전히 부족하며 주로 실용적이며 주로 Java 개발을 실천합니다 ~~. 그래서 Navicat을 사용하여 디자인 테이블 상관 관계가 없었습니다.
실제로, 테이블 상관 관계를 한눈에 볼 수 있습니다.
약간 수정 된 것과 같습니다.
3. Spring3+Hibernate4 구성 <br /> 모델 및 매핑 파일이 자동으로 생성되므로 복합 기본 키의 설정은 A에 해당합니다 복합 모델. 예를 들어, 제품 검토 테이블의 복합 기본 키 유형 :
package com.book.model; // 2015-11-2 9:07:06 Hibernate Tools 4.0.0.finalimport java.util.date;/** * commentId hbm2java */publi c class commentspk java IO.Serializable {개인 도서; .commentsdate = heport getbook () {public void setbook (this.book) {public void setUser (replare void setUser; user) {user = user getCommentsDate () {return void setCommentsDate (date CommentSdate) {verec.commentsDate} public boolean equals = 기타) castother.getBook ()) || ) == castother.getUser ()) || .GetCom MentsDate () == Castother.GetCommentsDate ()) ||. } int hashcode () {int result = 17; null? 0 : hashcode ()); 제품 검토 테이블 모델 :
package com.book.model; // 2015-10-30 14:56:21 Hibernate Tools 4.0.0.finalimport java.sql.date;/** * hbm2java */public class comments Java를 구현합니다. io.serializable {private string pic; } public string getpic () {return pic; } public commentspk getId (return id); 내용; 해당 최대 절전 모드 매핑 파일 :
<? xml 버전 = "1.0"?> <! doctype hibernate 맵핑 공개 "-// hibernate/hibernate 매핑 dtd 3.0 // en" "http://www.hibernate.org/dtd/hibernate-mapp ing-3.0 Dtd "> <!-Hibernate Tools 4.0.0.final-> <Hibernate-Mapping> <class name ="com.book.model.comments "table = "댓글"카탈로그 = "서점"> <composite-id name = "id"> <key-many-to-one name = "book"> <column name = "bookid" /> < /key-many-to- 하나> <key-many-to-one name = "user"> <column name = "userId" /> < /key-many-to-one> <key-property name = "commentsDate"type = "timestamp"> <column name = "commentDate"length = "19"/> </key-property> </composite-id> <속성 이름 = "content"type = "string"> <column name = "content"length = "65535 " /> < /property> <property name ="pic "type ="string "> <열 이름 ="pic "length ="65535 " /> < /property> <속성 이름 ="client "type ="java. lan g .integer "> <column name ="client "/> </property> </class> </hibernate-mapping>
제품 검토 테이블에는 두 개의 외국 키가 있기 때문에 키 마니에서 하나의 기본 태그가 사용됩니다.
Spring3.2+Hibernate4.1이 사용되므로 SessionFactory를 얻는 방법은 SessionFactory.getCurrentSess ()로 제한됩니다.
<bean id = "transactactionManager"> <속성 이름 = "sessionFactory"ref = "sessionFactory" /> < /bean> <!-트랜잭션 전파 특성-> <TX : 조언 ID = "TXADVICE"TRANSAC TION-MANAGER = "transactionManager"> <TX : attributes> <tx : 메소드 이름 = "add*"propagation = "필수" /> <tx : 메소드 이름 = "delete*"propagation = "필수" /> <tx : 메소드 이름 = " 수정*"전파 ="필수 " /> <!-hibernate4는 트랜잭션을 활성화하도록 구성되어야합니다. 그렇지 않으면 getCurrentSession ()를 얻을 수 없습니다-> <tx : method name ="*"propagation ="read-on ly = "true"/> </tx : 속성> </tx : 조언>
위는 내가 구성했을 때 발생한 문제입니다. 스프링 구성 파일은 다음과 같습니다.
<? xml 버전 = "1.0"encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://www.w3.org /2001/xmlschema-instance "xmlns : aop ="http://www.springframework.org/schema/aop "xmlns : context ="http://www.springframework.org/schema/ context "xmlns : tx =" http://www.springframework.org/schema/tx "xsi : schemalocation ="http://www.springframework.org/schema/beans http://www.sprampramework.org/schema/beans/spring-beans- 3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http : //www.springframewor k.org/schema/ 컨텍스트 http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http : //www.springframegram/tx/sprams -tx-3.2.xsd "> <!-스프링 주석 지원 활성화-> <context : annotation-config /> <bean id ="dataSource "Destroy-method ="close "> <property name ="driver classnam e " value = "com.mysql.jdbc.driver"/> <속성 이름 = "url"value = "jdbc : mysql : //127.0.0.1/bookstore? useUnicode = true & characterencoding = utf-8"/> <property name = " 사용자 이름 "value ="root " /> <property name ="password "value ="Yangyang " /> < /bean> <!-보조 캐시의 구성-> <bean id ="sessionFactory "의 구성을 추가 할 수 있습니다. > <property name = "dataSource"ref = "dataSource"/> <property name = "mappingDirectoryLocations"> <list> <value> classpath : config </value> </list> </property> <property name = "HibernateProperties "> <props> <prop key ="hibernate.dialect "> org.hibernate.dialect.mysqldialect </prop> <prop key ="hibernate.show_sql "> true </prop> <prop key ="hibernate.show_sql " > true </prop> <prop key = "hibernat e.hbm2ddl.auto"> 업데이트 </prop> <prop key = "hibernate.temp.use_jdbc_metadata_defaults"> false </prop> <prop key = "current_session_context_class"> 스레드 </prop> </props> </property> </bean> <!-구성 트랜잭션 관리자-> <bean id = "TransactionManager"> <속성 이름 = "sessionFactory"ref = "sessionFactory"/> </ Bean> <!-트랜잭션의 전파 특성-> <TX : 조언 ID = "TXADVICE"TRANSACTE-MANAGER = "TRANSACTIONMANAGER"> <TX : attributes> <TX : "Add*"Propagation = "필수" /> <tx : 메소드 이름 = "delet e*"propagation = "필수" /> <tx : 메소드 이름 = "modify*"propagation = "필수" /> <!-hibernate4는 트랜잭션을 활성화하도록 구성되어야합니다. getCurrentsession ()는 얻을 수 없습니다-> <tx : 메소드 이름 = "*"propagation = "필수"read-only = "true"/> </tx : attributes> </tx : 조언> <!-해당 클래스 그리고 메소드는 트랜잭션을 사용합니다-> <aop : config> <!-비즈니스 로직 계층에 대한 트랜잭션 만 구현됩니다-> <aop : pointcut id = "AllManagerMethod"expression = "execution (* com.book.test.*. .*(..)) " /> <aop : Advisor Pointcut-Ref ="AllManagerMethod "adv Ice-Ref ="TXADVICE " /> < /aop : config> <bean name ="baseddao " /<bean name = "Orderdao" /> < /beans>
모든 것이 준비된 후에는 서블릿 테스트를 사용합니다.
<Servlet> <Servlet-name> 테스트 </servlet-name> <servlet-class> com.book.test.test </servlet-class> </servlet> <servlet-mapping> <servlet-name> test </ servlet-name> <Url-pattern>/index </url-pattern> </servlet-mapping>
Protected void doget (httpservletrequest 요청, httpservletresponse 응답)은 servletexception, ioexception {// todo auto-regended method stub를 introfory factor = webapplicationcontextutils.getRequiredWebApplicationContxt (getservletcontext ()); OrderDao.우리는 스트러크를 사용하지 않기 때문에, 우리는 Dao Bean을 얻기 위해 봄의 콩나무를 찾아야합니다.
실행 결과 :
성공적으로로드 된 주문 테이블 주문 1 품목 주문 수량.
결국, Java를 사용하여 이러한 프로젝트를 천천히 개발 한 것은 Javaweb가 구축 한 온라인 책 프레임 워크를 좋아할 것입니다.