Home>

I have been in contact before.Net related web development, now suddenly using javaweb is still very unaccustomed,It was the first time that even a frame was built.

I. Talk about the project structure

contact Contact at first.net related development so for.Net related development is still relatively familiar,But the development of java direction I studied in school,And I plan to combine these two platforms,Using java as the backend is the service provider,Complete all business logic on the java platform and use what I am familiar with.Net does web development.In this way, the Android app is available on the web.Clients uniformly call services through a distributed framework.After a long search, I finally chose hprose, a lightweight, cross-language, cross-platform, non-intrusive, high-performance dynamic remote object call engine library.I chose it because of the low cost of learning,On the other hand, its cross-platform calling is very easy and efficient,Because we want to use.Net to do web needs to call the service published by java! I took a look at the documentation of hprose and found that using the built-in hproseservlet to publish services is faster and easier.So I am going to use this method to publish the service.But here comes the problem,The traditional ssh architecture feels a bit heavy,Ready to use.Net development web end, so it feels unnecessary to integrate struts, so it is hibernate + spring + hprose.

Database design

A small online bookstore,So the design is still lacking,Practical,Mainly to practice java development ~~. So I used navicat for a simple design,But there is no design table association,Instead, the relationships were added one by one later.I found this design tool a bit problematic,Icon:

In fact, the table association can be seen at a glance ~~ Next is some mapping of hibernate,Also use the plugin to generate model and mapping files

A slight modification is like this-

Third, spring3 + hibernate4 configuration

Since the model and mapping files are automatically generated, a little configuration is fine.Note that the setting of the composite primary key,The automatically generated composite primary key corresponds to a composite model.Such asComposite primary key type for product review table:

package com.book.model;
//generated 2015-11-2 9:07:06 by hibernate tools 4.0.0.final
import java.util.date;
/**
 * commentsid generated by hbm2java
 * /
public class commentspk implements java.io.serializable {
  private book book;
  private user user;
  private date commentsdate;
  public commentspk () {
  }
  public commentspk (book book, user user, date commentsdate) {
    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=commentsdate;
  }
  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
            &&this.getuser (). Equals (castother.getuser ())))
        &&((this.getcommentsdate () == castother.getcommentsdate ())
            || (this.getcommentsdate ()!=null&&castother.getcommentsdate ()!=null
                &&this.getcommentsdate (). Equals (castother.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.final
import 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;
  }
  public 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, 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-mapping-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.lang.integer">
      <column name="client" />
    </property>
  </class>
</hibernate-mapping>

Because the product review table has two foreign keys, the key-many-to-one tag is used.

Since spring3.2 + hibernate4.1 is used, the way to get the sessionfactory is limited to sessionfactory.getcurrentsession ();but things must be turned on:

<bean
   >
    <property name="sessionfactory" ref="sessionfactory" />
  </bean>
  <!-Propagation characteristics of transactions->
  <tx:advice transaction-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 start transactions, otherwise getcurrentsession () will not get->
      <tx:method name="*" propagation="required" read-only="true" />
    </tx:attributes>
  </tx:advice>

The above are all the problems when I configure.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.springframework.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 destroy-method="close">
    <property name="driverclassname" 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>
  <!-Additional secondary cache can be configured->
  <bean>
    <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.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>
    <!-Configure transaction manager->
  <bean
   >
    <property name="sessionfactory" ref="sessionfactory" />
  </bean>
  <!-Propagation characteristics of transactions->
  <tx:advice transaction-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 start transactions, otherwise getcurrentsession () will not get->
      <tx:method name="*" propagation="required" read-only="true" />
    </tx:attributes>
  </tx:advice>
  <!-Those classes and methods use transactions->
  <aop:config>
   <!-Implement transactions only for the business logic layer->
    <aop:pointcut
      expression="execution (* com.book.test. *. * (..))" />
    <aop:advisor pointcut-ref="allmanagermethod" advice-ref="txadvice" />
  </aop:config>
    <bean name="basedao" />
    <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
    beanfactory factor=webapplicationcontextutils.getrequiredwebapplicationcontext (request.getsession (). getservletcontext ());
    orderdao dao=factor.getbean (orderdao.class);
    object [] list=dao.get (1) .getorderitems (). toarray ();
    system.out.println (((orderitem) list [0]). getordercount ());
  }

Because structs are not used, we need to find the spring beanfactory to get the dao bean. This is also something to pay attention to.Tangled for a long time.

operation result:

Successfully loaded order table order 1 item order quantity.

  • Previous Java submits a form by simulating a post to implement an image upload function example
  • Next Method to get the first record in SQL (sqlserver, oracle, mysql database)