I have been in contact with .net-related web development before, but now I am still very uncomfortable using JavaWeb. It is the first time I even built a framework.
1. Talk about project architecture <br />I first came into contact with .net-related development, so I am quite familiar with .net-related development, but I learned Java in school, and I plan to combine these two platforms. Get up, use Java as the backend, that is, the service provider, complete all business logic on the Java platform and use .net that I am more familiar with for web development. In this way, there will be Android apps and web-sides. The client calls the service uniformly through a distributed framework. After searching for a long time, I finally chose Hprose, a lightweight, cross-language, cross-platform, non-invasive, high-performance dynamic remote object calling engine library. The reason I chose it is because of the low learning cost, and on the other hand, its cross-platform call is very easy and efficient, because we need to use .net to do the web to call the services published by Java! After roughly looking at the Hprose documentation, I found that using the built-in HproseServlet release service is faster and simpler to develop, so I plan to use this method to publish the service. But the problem is that the traditional ssh architecture feels a bit heavy, and I am going to use .net to develop the web side, so I feel that there is no need to integrate Struts, so it is a hibernate+spring+hprose architecture.
2. Database design
It is a small online bookstore, so the design is still lacking, and it is mainly practical, mainly practicing java development~~. So I used navicat to design it briefly, but there was no design table correlation. Instead, I added relationships one by one later. I found that this design tool had some problems. The illustration:
In fact, you can see the table correlation at a glance~~, next is hibernate some mappings, and also use plug-ins to generate models and mapping files.
It's like this with a slight modification-
3. Spring3+hibernate4 configuration <br />Because the model and mapping files are automatically generated, it is better to configure them a little. It is necessary to note that the settings of the composite primary key. The automatically generated will correspond to a composite model. For example, the compound primary key type of the product review table :
package com.book.model;// Generated 2015-11-2 9:07:06 by Hibernate Tools 4.0.0.Finalimport java.util.Date;/** * CommentsId generated by hbm2java */publi c class CommentsPk implements java. io.Serializable { private Book book; private User user; private Date commentsDate; public CommentsPk() { } public CommentsPk(Book book, User user, Date c omentsDate) { this.book = book; this.user = user; this.commentsDate = commentsDate; } public Book getBook() { return this.book; } public void setBook(Book book) { this.book = book; } public User getUser() { return this.user; } public void setUser(User user) { this.user = user; } public Date getCommentsDate() { return this.commentsDate; } public void setCommentsDate(Date commentsDate) { this.commentsDate = c omentsDate; } public boolean equals(Object other) { if ((this == other )) return true; if ((other == null)) return false; if (!(other instanceof CommentsPk)) return false; CommentsPk castOther = (CommentsPk) other; return ( (this.getBook() == castOther.getBook ()) || (this.getBook() != null && castOther.getBook() != null && this.getBook().equals(castOther.getBook()))) && ((this.getUser() == castOther.getUser()) || (this.getUser() != null && castOther.getUser() != null && this.getUser().equals(castOther.getUser()))) && ((this.getCom mentsDate( ) == castOther.getCommentsDate()) || (this.getCommentsDate() != null && castOther.getCommentsDate() != null && this.getCommentsDate().equals(castOth er.getCommentsDate())); } public int hashCode() { int result = 17; result = 37 * result + (getBook() == null ? 0 : this.getBook().hashCode()); result = 37 * result + (getUser() == null ? 0 : this.getUser().hashCode()); result = 37 * result + (getCommentsDate() == null ? 0 : this.getCommentsDate().hashCode()); return result; } } Product Review Table Model :
package com.book.model;// Generated 2015-10-30 14:56:21 by Hibernate Tools 4.0.0.Finalimport java.sql.Date;/** * Comments generated by hbm2java */public class Comments implements java. io.Serializable { private String content; private String pic; private Integer client; private CommentsPk id; public Comments() { } public String getContent( ) { return content; } public void setContent(String content) { this.content = content; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public Integer getClient() { return client; } publ ic void setClient(Integer client) { this.client = client; } public CommentsPk getId() { return id; } public void setId(CommentsPk id) { this.id = id; } public Comments(String content, String pic, Integer client t, CommentsPk id) { super(); this.content = content; this.pic = pic; this.client = client; this.id = id; } } The corresponding Hibernate mapping file:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapp ing-3.0. dtd"><!-- Generated 2015-10-30 14:56:21 by Hibernate Tools 4.0.0.Final --><hibernate-mapping> <class name="com.book.model.Comments" table=" comments" catalog="bookstore"> <composite-id name="id"> <key-many-to-one name="book"> <column name="BookID" /> </key-many-to-one > <key-many-to-one name="user"> <column name="UserID" /> </key-many-to-one> <key-property name="commentsDate" type="timestamp"> < column name="CommentsDate" length="19" /> </key-property> </composite-id> <property name="content" type="string"> <column name="Content" length ="65535" /> </property> <property name="pic" type="string"> <column name="Pic" length="65535" /> </property> <property name="client" type="java.lan g .Integer"> <column name="Client" /> </property> </class></hibernate-mapping>
Because there are two foreign keys in the product review table, the key-many-to-one tag is used.
Since spring3.2+hibernate4.1 is used, the way to get sessionFactory is limited to sessionFactory.getCurrentSession(); but things must be enabled:
<bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Transaction propagation characteristics--> <tx:advice id="txadvice" transac tion-manager= "transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name=" modify*" propagation="REQUIRED" /> <!--hibernate4 must be configured to enable transactions, otherwise getCurrentSession() cannot be obtained --> <tx:method name="*" propagation="REQUIRED" read-on ly="true " /> </tx:attributes> </tx:advice>
The above are the problems that occurred when I configured it. Here is the spring configuration file:
<?xml version="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.springframework.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/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework. org/schema/tx/spring- tx-3.2.xsd"> <!-- Enable spring annotation support--> <context:annotation-config /> <bean id="dataSource" destroy-method="close"> <property name="driverClassNam e" value= "com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1/bookstore?useUnicode=true&characterEncoding=UTF-8" / > <property name="username" value="root" /> <property name="password" value="yangyang" /> </bean> <!-- You can add the configuration of the secondary cache --> <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">update< /prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="current_session_context_class">thread</prop> </ props> </property> </bean> <!-- Configuration Transaction Manager --> <bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Propagation characteristics of transactions --> <tx:advice id=" txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="delet e*" propagation="REQUIRED" /> < tx:method name="modify*" propagation="REQUIRED" /> <!--hibernate4 must be configured to enable transactions, otherwise getCurrentSession() cannot be obtained --> <tx:method name="*" propagation ="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- Those classes and methods use transactions--> <aop:config> <!-- Only transactions are implemented for the business logic layer --> <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>
After everything is ready, we use servlet test:
<servlet> <servlet-name>test</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 request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Be anFactory factor = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()); OrderDao dao= factor.ge tBean(OrderDao. class); Object[] list= dao.get(1).getOrderitems().toArray(); System.out.println(((Orderitem)list[0]).getOrdercount()); }Because we do not use structs, we need to find the spring's BeanFactory to obtain dao bean. This is also something we need to pay attention to, and we have been struggling for a long time.
Running results:
Successfully loaded Order Table Order 1 Item Order Quantity.
After all, it’s the first time I’ve used Java to develop such projects. Let’s learn slowly. I hope you can like the online book mall framework built by JavaWeb, which will be helpful to everyone’s learning.