写了好多次SSH现在对于框架还是有一定的基础了,但是对于框架下我们该如何进行操作呢???

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

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

web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>news</display-name>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

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

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

写了好多次SSH现在对于框架还是有一定的基础了,但是对于框架下我们该如何进行操作呢???

例如下面我们定义的一个项目(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中使用了注解,如果不熟悉注解的可以进入这个网址
 package product.entity;

 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="t_product")
public class Product {
private Integer proId;
private String proName;
private Float proPrice;
private Integer proCount;
private String proDesc; @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getProId() {
return proId;
}
public void setProId(Integer proId) {
this.proId = proId;
} @Column(name="proName",nullable=false,length=50)
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
} @Column(name="proPrice",nullable=false)
public Float getProPrice() {
return proPrice;
}
public void setProPrice(Float proPrice) {
this.proPrice = proPrice;
} @Column(name="proCount",nullable=false)
public Integer getProCount() {
return proCount;
}
public void setProCount(Integer proCount) {
this.proCount = proCount;
} @Column(name="proDesc",nullable=false,length=250)
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
} }

product.dao包(包含接口和实现类)
【接口】
 package product.dao;

 import java.util.List;

 import product.entity.Product;

 public interface ProDao {
public List<Product> getAllPro(); public void delPro(Integer proId[]);
}
 可以看到,和数据库进行交互的就是dao包,在dao包中,我们可以定义最原始的方法,然后通过service进行逻辑处理,最后通过action的返回字符串在struts中进行跳转到对应的jsp或者其他的页面.
【实现类】
 package product.dao;

 import java.util.List;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository; import product.entity.Product; @Repository
@Scope("prototype")
public class ProDaoImpl implements ProDao {
@Autowired
private SessionFactory sf; @Override
public List<Product> getAllPro() {
// TODO Auto-generated method stub Session session = sf.getCurrentSession(); @SuppressWarnings("unchecked")
Query<Product> query = session.createQuery("from Product"); return query.getResultList();
} @Override
public void delPro(Integer proId[]) {
// TODO Auto-generated method stub
Session session = sf.getCurrentSession(); @SuppressWarnings("unchecked")
Query<Product> query = session.createQuery("from Product where proId=:myid");
//System.out.println("proId:"+proId);
for(Integer id : proId){
query.setParameter("myid", id); if(query.getResultList().size()>0){
Product pro = query.getResultList().get(0);
session.delete(pro);
}
}
}
}
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[]);
}

【实现类】

 package product.service;

 import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import product.dao.ProDao;
import product.entity.Product; @Transactional
@Service
@Scope("prototype")
public class ProServiceImpl implements ProService {
@Autowired
private ProDao pd; @Override
@Transactional(readOnly=true)
public List<Product> getAllPro() {
// TODO Auto-generated method stub
return pd.getAllPro();
} @Override
public void delPro(Integer proId[]) {
//System.out.println("proId:"+proId);
pd.delPro(proId);
}
}

还有我们数据库的一些信息。我们使用什么数据库,以及它的参数
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
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" /> <constant name="struts.ui.theme" value="simple"></constant> <!-- 先定义一个包 -->
<package name="mypck001" extends="struts-default">
<action name="ProAction_*" class="proAction" method="{1}">
<result name="data">/WEB-INF/jsp/index.jsp</result>
<result name="deleteOK" type="redirectAction">ProAction_showAllPro</result>
</action>
</package>
</struts>

最后面就是最容易,但也是最容易出错的applicationContext.xml文件-----(但是我们可以复制粘贴,唯独需要改动的就是自动扫描包的【包名】)
applicationContext.xml
 <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 原理:自动注入processor解析器,用来解析注解 -->
<!-- <context:annotation-config/> --> <!-- 自动扫描包,也会自动注入解释器,所以不需要 context:annotation-config -->
<context:component-scan base-package="product" /> <!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
<property name="dataSource" ref="myDataSource" /> <!-- 配置Hibernate的其他的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<!-- 开机自动生成表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list> </list>
</property> <property name="packagesToScan">
<list>
<value>product.*</value>
</list>
</property> </bean> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<!-- 每300秒检查所有连接池中的空闲连接 -->
<property name="idleConnectionTestPeriod" value="300"></property>
<!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
<property name="maxIdleTime" value="900"></property>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="2"></property> </bean>
    <!-- 事务管理-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
    <!-- 注解驱动加载 -->
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>

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

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

    如果错误请指正!
上一篇:jQuery 操作input select,checkbox


下一篇:新手SSH基础框架搭建