首先,对于一个老手来说,我们最快捷的就是ctrl+c和ctrl+v,但是我们自己应该复制哪一些代码呢?

1、在我们导完包之后,我们需要写的就是web.xml,在其中,我们要有过滤器及映射和监听器.

web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>news</display-name>
 4   <welcome-file-list>
 5     <welcome-file>default.jsp</welcome-file>
 6   </welcome-file-list>
 7   <filter>
 8     <filter-name>struts2</filter-name>
 9     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
10   </filter>
11   <filter-mapping>
12     <filter-name>struts2</filter-name>
13     <url-pattern>/*</url-pattern>
14   </filter-mapping>
15   <context-param>
16     <param-name>contextConfigLocation</param-name>
17     <param-value>classpath:applicationContext.xml</param-value>
18   </context-param>
19   <listener>
20     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
21   </listener>
22 </web-app>

2、在我们做好所有的准备之后,就是定义所有的包、类、接口、applicationContext.xml等。

按照一般的习惯我们定义的包名也有一定的技巧的.如图:

例如下面我们定义的一个项目(jsp----->struts----->action----->service----->dao----->sessionFactory----->applicationContext.xml)

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

[这是我自己从中总结出来的-----在此之外,还有product.entity实体类]

product.action包

package product.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import product.entity.Product;
import product.service.ProService;

@SuppressWarnings("serial")
@Controller("proAction")
@Scope("prototype")
public class ProAction extends ActionSupport {
    @Autowired
    private ProService ps;
    
    private Integer[] proId;
    public void setProId(Integer[] proId) {
        this.proId = proId;
    }

    public String showAllPro(){
        List<Product> proList = ps.getAllPro();
        ActionContext.getContext().put("proList", proList);
        return "data";
    }
    
    public String delPro(){
        ps.delPro(proId);
        return "deleteOK";
    }
}


product.entity包(大家也可以看到,我在entity中使用了注解,如果不熟悉注解的可以进入这个网址
 1 package product.entity;
 2 
 3 import javax.persistence.Column;
 4 import javax.persistence.Entity;
 5 import javax.persistence.GeneratedValue;
 6 import javax.persistence.GenerationType;
 7 import javax.persistence.Id;
 8 import javax.persistence.Table;
 9 
10 @Entity
11 @Table(name="t_product")
12 public class Product {
13     private    Integer proId;
14     private String proName;
15     private Float proPrice;
16     private Integer proCount;
17     private String proDesc;
18     
19     @Id
20     @GeneratedValue(strategy=GenerationType.IDENTITY)
21     public Integer getProId() {
22         return proId;
23     }
24     public void setProId(Integer proId) {
25         this.proId = proId;
26     }
27     
28     @Column(name="proName",nullable=false,length=50)
29     public String getProName() {
30         return proName;
31     }
32     public void setProName(String proName) {
33         this.proName = proName;
34     }
35     
36     @Column(name="proPrice",nullable=false)
37     public Float getProPrice() {
38         return proPrice;
39     }
40     public void setProPrice(Float proPrice) {
41         this.proPrice = proPrice;
42     }
43     
44     @Column(name="proCount",nullable=false)
45     public Integer getProCount() {
46         return proCount;
47     }
48     public void setProCount(Integer proCount) {
49         this.proCount = proCount;
50     }
51     
52     @Column(name="proDesc",nullable=false,length=250)
53     public String getProDesc() {
54         return proDesc;
55     }
56     public void setProDesc(String proDesc) {
57         this.proDesc = proDesc;
58     }
59 
60 
61 }

 

product.dao包(包含接口和实现类)
【接口】
 1 package product.dao;
 2 
 3 import java.util.List;
 4 
 5 import product.entity.Product;
 6 
 7 public interface ProDao {
 8     public List<Product> getAllPro();
 9     
10     public void delPro(Integer proId[]);
11 }

 可以看到,和数据库进行交互的就是dao包,在dao包中,我们可以定义最原始的方法,然后通过service进行逻辑处理,最后通过action的返回字符串在struts中进行跳转到对应的jsp或者其他的页面.

【实现类】
 1 package product.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.hibernate.Session;
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.query.Query;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.context.annotation.Scope;
10 import org.springframework.stereotype.Repository;
11 
12 import product.entity.Product;
13 
14 @Repository
15 @Scope("prototype")
16 public class ProDaoImpl implements ProDao {
17     @Autowired
18     private SessionFactory sf;
19     
20     @Override
21     public List<Product> getAllPro() {
22         // TODO Auto-generated method stub
23         
24         Session session = sf.getCurrentSession();
25         
26         @SuppressWarnings("unchecked")
27         Query<Product> query = session.createQuery("from Product");
28         
29         return query.getResultList();
30     }
31 
32     @Override
33     public void delPro(Integer proId[]) {
34         // TODO Auto-generated method stub
35         Session session = sf.getCurrentSession();
36         
37         @SuppressWarnings("unchecked")
38         Query<Product> query = session.createQuery("from Product where proId=:myid");
39         //System.out.println("proId:"+proId);
40         for(Integer id : proId){
41             query.setParameter("myid", id);
42 
43             if(query.getResultList().size()>0){
44                 Product pro = query.getResultList().get(0);
45                 session.delete(pro);
46             }
47         }
48     }
49 }

product.service包
【接口】--------在这里我们可以对比于上面dao包中的接口,我们清晰的看到代码一模一样
package product.service;

import java.util.List;

import product.entity.Product;

public interface ProService {
    public List<Product> getAllPro();
    
    public void delPro(Integer proId[]);
}

 

【实现类】

 1 package product.service;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.context.annotation.Scope;
 7 import org.springframework.stereotype.Service;
 8 import org.springframework.transaction.annotation.Transactional;
 9 
10 import product.dao.ProDao;
11 import product.entity.Product;
12 
13 @Transactional
14 @Service
15 @Scope("prototype")
16 public class ProServiceImpl implements ProService {
17     @Autowired
18     private ProDao pd;
19     
20     @Override
21     @Transactional(readOnly=true)
22     public List<Product> getAllPro() {
23         // TODO Auto-generated method stub
24         return pd.getAllPro();
25     }
26 
27     @Override
28     public void delPro(Integer proId[]) {
29         //System.out.println("proId:"+proId);
30         pd.delPro(proId);
31     }
32 }

 

还有我们数据库的一些信息。我们使用什么数据库,以及它的参数
jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/news
jdbc.user=root
jdbc.password=123456


    如果是用mysql,则复制楼上代码,
    如果是oracle则复制楼下代码。

#oracle jdbc_oracle.driver
=oracle.jdbc.driver.OracleDriver jdbc_oracle.url=jdbc:oracle:thin@127.0.0.1:1521:orcl jdbc_oracle.user=news jdbc_oracle.password=123456

 

而struts中,我们可以进行跳转,对应action中不同返回额字符串
struts.xml
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.objectFactory" value="spring" />
 7 
 8     <constant name="struts.ui.theme" value="simple"></constant>
 9     
10     <!-- 先定义一个包 -->
11     <package name="mypck001" extends="struts-default">
12         <action name="ProAction_*" class="proAction" method="{1}">
13             <result name="data">/WEB-INF/jsp/index.jsp</result>
14             <result name="deleteOK" type="redirectAction">ProAction_showAllPro</result>
15         </action>        
16     </package>
17</struts>

 

最后面就是最容易,但也是最容易出错的applicationContext.xml文件-----(但是我们可以复制粘贴,唯独需要改动的就是自动扫描包的【包名】)
applicationContext.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="    
 7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
 8             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
 9             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
10             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
12 
13     <!-- 原理:自动注入processor解析器,用来解析注解 -->
14     <!-- <context:annotation-config/> -->
15 
16     <!-- 自动扫描包,也会自动注入解释器,所以不需要 context:annotation-config -->
17     <context:component-scan base-package="product" />
18 
19 
20     <!-- 引入外部属性文件 -->
21     <context:property-placeholder location="classpath:jdbc.properties" />
22 
23     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
24         <!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
25         <property name="dataSource" ref="myDataSource" />
26 
27         <!-- 配置Hibernate的其他的属性 -->
28         <property name="hibernateProperties">
29             <props>
30                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
31                 <prop key="hibernate.show_sql">true</prop>
32                 <prop key="hibernate.format_sql">true</prop>
33                 <prop key="hibernate.connection.autocommit">false</prop>
34                 <!-- 开机自动生成表 -->
35                 <prop key="hibernate.hbm2ddl.auto">update</prop>
36             </props>
37         </property>
38         <property name="mappingResources">
39             <list>
40                 
41             </list>
42         </property>
43         
44         <property name="packagesToScan">
45             <list>
46                 <value>product.*</value>
47             </list>
48         </property>
49 
50     </bean>
51 
52     <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
53         <property name="driverClass" value="${jdbc.driver}" />
54         <property name="jdbcUrl" value="${jdbc.url}" />
55         <property name="user" value="${jdbc.user}" />
56         <property name="password" value="${jdbc.password}" />
57         <!-- 每300秒检查所有连接池中的空闲连接 -->
58         <property name="idleConnectionTestPeriod" value="300"></property>
59         <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
60         <property name="maxIdleTime" value="900"></property>
61         <!-- 最大连接数 -->
62         <property name="maxPoolSize" value="2"></property>
63 
64     </bean>
65     <!-- 事务管理-->
66     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
67         <property name="sessionFactory" ref="sessionFactory"></property>
68     </bean>
69     <!--  注解驱动加载 -->
70     <tx:annotation-driven transaction-manager="transactionManager"/>
71 
72 
73 </beans>
74             

 

当你完成复制之后,你的代码也算是完成可九成,注意修改一些需要通过加载包名或者类名进行操作的步骤代码。

今天难得静下心来写了一个博客,求打赏求关注!!!

    如果错误请指正!