SSH项目构造过程

 SSH项目构造过程

先构造Java Web项目,分两步开发:DAO部分和Web部分。

一.DAO部分:

给项目add spring capabilities

 SSH项目构造过程

SSH项目构造过程

增加Hibernate Capalities
SSH项目构造过程
SSH项目构造过程
SSH项目构造过程
SSH项目构造过程
SSH项目构造过程
SSH项目构造过程
自动生成的Dao不够灵活,可以生成看下内容,最好自己写Dao的接口和实现类

SSH项目构造过程
SSH项目构造过程

构造步骤:

1. 在数据库中新建book_sequence, 并设置BookNing.hbm.xml中的Sequence设置,

2. 将属性ptime(数据库中的类型是Date)在pojo和xml文件中改成Date,不要使用默认生成的TimeStamp

3. Xml中类标记增加:lazy=”false”

4. 自行建立DAO Interface / Class(继承HibernateDaoSupport)

public interface IBookDAO {

       public List findAll();

       public BookNing findById(Short id);

       public List findByProperty(String propertyName, Object propertyValue);

       public List findByBooktype(BooktypeNing btype);

       public boolean executeSave(BookNing book);

       public boolean executeMerge(BookNing book);

       public boolean executeDelete(BookNing book);

}

public class BookDAO extends HibernateDaoSupport implements IBookDAO {

       public List findAll() {

              String queryString = "from BookNing";

              return getHibernateTemplate().find(queryString);

       }

       public BookNing findById(Short id) {

              BookNing instance = (BookNing) getHibernateTemplate().get(

                            "com.tarena.pojo.BookNing", id);

              return instance;

       }

       public List findByProperty(String propertyName, Object propertyValue) {

              String queryString = "from BookNing as model where model." + propertyName + "= ?";

              return getHibernateTemplate().find(queryString, propertyValue);

       }

       public boolean executeMerge(BookNing book) {

              try {

                     BookNing result = (BookNing) getHibernateTemplate().merge(book);

                     return true;

              } catch (Exception e) {

                     e.printStackTrace();

                     return false;

              }

       }

       public boolean executeSave(BookNing book) {

              try {

                     getHibernateTemplate().save(book);

                     return true;

              } catch (Exception e) {

                     e.printStackTrace();

                     return false;

              }

       }

       public boolean executeDelete(BookNing book) {

              try {

                     getHibernateTemplate().delete(book);

                     return true;

              } catch (Exception e) {

                     e.printStackTrace();

                     return false;

              }

       }

       public List findByBooktype(BooktypeNing btype) {

              String queryString = "from BookNing as model where model.booktypeNing.id = ?";

       return getHibernateTemplate().find(queryString, btype.getId());

       }

5. 在applicationContext.xml中增加dao的设置

< bean id="bookDAO" class="com.tarena.dao.BookDAO">

              <property name="sessionFactory">

                     <ref bean="sessionFactory" />

              </property>

       </ bean>

6. 使用声明式的方式实现事务处理

< bean id="transactionManager"

              class="org.springframework.orm.hibernate3.HibernateTransactionManager">

              <property name="sessionFactory">

                     <ref bean="sessionFactory" />

              </property>

       </ bean>

       <bean id="baseTransactionProxy"

              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

              abstract="true">

              <property name="transactionManager" ref="transactionManager" />

              <property name="transactionAttributes">

                     <props>

                            <prop key="execute*">PROPAGATION_REQUIRED</prop>

                     </props>

              </property>

       </bean>

       <bean id="bookDaoProxy" parent="baseTransactionProxy">

              <property name="target" ref="bookDAO" />

       </bean>

7.DAO部分测试

              BookNing book = new BookNing();

              book.setName("tianlongbabu");

              book.setIsbn("7894");

              book.setPrice(60.00);

              book.setPtime(new Date());

              book.setAuthor("bruce");

              BooktypeNing btype = new BooktypeNing();

              btype.setId((byte)1);

              book.setBooktypeNing(btype);

             

              ApplicationContext ctx

                     = new ClassPathXmlApplicationContext("applicationContext.xml");

              IBookDAO dao = (IBookDAO)ctx.getBean("bookDaoProxy");

              //1.save

              //dao.executeSave(book);

              //2.update

//            book.setId((short)10);

//            book.setName("programm");

//            dao.executeMerge(book);

              //3.delete

              book.setId((short)10);

              dao.executeDelete(book);

              //4.findById

//            BookNing book1 = dao.findById((short)1001);

//            System.out.println(book1.getName());

              //5.findByProperty

//            List<BookNing> books = dao.findByProperty("name", "tianlongbabu");

//            for(Iterator<BookNing> it = books.iterator();it.hasNext();){

//                   System.out.println(it.next().getName());

//            }

              //6.findAll

//            List<BookNing> books = dao.findAll();

//            for(Iterator<BookNing> it = books.iterator();it.hasNext();){

//                   System.out.println(it.next().getName());

//            }

              //7.findByBooktype

//            BooktypeNing type = new BooktypeNing();

//            type.setId((byte)1);

//            List<BookNing> books = dao.findByBooktype(type);

//            for(Iterator<BookNing> it = books.iterator();it.hasNext();){

//                   System.out.println(it.next().getName());

//            }

 

 

 

二、Web部分

1.先准备调试工具:log4j

1)Log4j.properties:(同时输出到控制台和文件)

log4j.rootLogger=info, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.appender.console.layout=org.apache.log4j.PatternLayout

log4j.appender.console.layout.ConversionPattern=%p %m %n

log4j.appender.file = org.apache.log4j.FileAppender

log4j.appender.file.File = info.log

log4j.appender.file.layout = org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern =%p (%d) -%m%n

2)web.xml

<context-param>

              <param-name>log4jConfigLocation</param-name>

              <param-value>/WEB-INF/log4j.properties</param-value>

       </context-param>

       <listener>

              <listener-class>

                     org.springframework.web.util.Log4jConfigListener

              </listener-class>

       </listener>

2. web.xml加载spring部分:

<context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>/WEB-INF/applicationContext.xml</param-value>

       </context-param>

       <listener>

            <listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

       </listener>

3. add struts capabilities

SSH项目构造过程
SSH项目构造过程

4. 增加业务处理:增删改查:

增: add.jsp ->BookAdmin.add() --> list.action --> list.jsp

删:delete.action?id=1 - > BookAdmin.delete()--> list.action

改:update.action?id=1 ->BookAdmin.findById()

 –> updateConfirm.action?id=1&name=tij&…--> BookAdmin.update()--> list.action

查:按id查findById.action --> BookAdmin.findById()

按属性查 findByProperty.action--> BookAdmin.findByProperty

查找全部findAll.action--> BookAdmin.findAll

 

1)  在struts.xml中增加国际化属性文件的定义 / 异常定义

<struts>

       <constant name="struts.custom.i18n.resources" value="com.tarena.i18n.MyResource">

</constant>

       <package name="action" namespace="/" extends="struts-default">

              <global-exception-mappings>

                     <exception-mapping result="ex" exception="java.lang.Exception">

                     </exception-mapping>

              </global-exception-mappings>

       </package>

</struts>   

2)  构造两个属性文件MyResource.properties / MyResource_zh_CN.properties

item.welcome = welcome, {0}

item.userid = userid

item.password = password

item.submit = submit

item.add=add

userid.required = userid cann't be empty!

password.required = password can't be empty!

password.length =password'length must be ${minLength} and ${maxLength}\!

3) struts.xml

<action name="book*" class="bookAction" method="{1}">

                     <interceptor-ref name="params"></interceptor-ref>

                     <!--  <interceptor-ref name="authInterceptor">

                     </interceptor-ref>-->

                     <result name="success">/index.jsp</result>

                     <result name="list">/list.jsp</result>

                     <result name="update">/update.jsp</result>

                     <result name="failure">/error.jsp</result>

                     <result name="input">/{1}.jsp</result>

                     <result name="ex">/error.jsp</result>

                     <result name="login">/login.jsp</result>

              </action>

applicationContext.xml:

<bean id="bookAction" class="com.tarena.action.BookAction">

              <property name="bookDao" ref="bookDaoProxy"></property>

       </bean>












 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值