简单搭建ssh框架(annotation)
首先导入所需要的jar包。。。
进行web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>sshanno</display-name> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
接下来再配置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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example"/> </beans>
<context:component-scan base-package="org.example"/>自动包扫描,要用注解必须写上这句,它会自动扫描你所写包下的所有类,具体应该是用递归扫的吧。
来写自己的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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.bjsxt" /> </beans>
建立action、service、dao
在action类上写@Controller
service类上写@Service
dao类上写@Repository
//这样就是告诉spring这是一个bean,默认的名字是你的类名首字母小写,你还可以通过value属性指定别名
当然你也可以用通用的@Component 不过不推荐这样写
@Controller、@Service、@Repository是@Repository的细分
action里用到了service,service里又用到dao,那么,在你声明类的时候写上@Resource
如:
@Controller public class EmpAction { @Resource private EmpServiceImpl empService; public EmpServiceImpl getEmpService() { return empService; } public void setEmpService(EmpServiceImpl empService) { this.empService = empService; } public void fandAll(){ empService.fandAll(); } }
@Service public class EmpServiceImpl { @Resource private EmpDaoImpl empDao; public EmpDaoImpl getEmpDao() { return empDao; } public void setEmpDao(EmpDaoImpl empDao) { this.empDao = empDao; } public void fandAll(){ empDao.fandAll(); } }
这样spring就会把EmpDaoImpl注给EmpServiceImpl,把EmpServcieImpl注给EmpAction
以上代码相当于在xml这样写:
<bean id="empDao" class="com.bjsxt.dao.EmpDaoImpl" ></bean> <bean id="empService" class="com.bjsxt.service.EmpServiceImpl" > <property name="empDao" ref="empDao"></property> </bean> <bean id="empAction" class="com.bjsxt.action.EmpAction" > <property name="empService" ref="empService"></property> </bean>
接下来测试一下
public class Test { @org.junit.Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpAction empAction = (EmpAction) ac.getBean("empAction"); empAction.fandAll(); }
控制台里打印出来了"dao",说明没有问题
再配置Struts2
web.xml里面加入
<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>
配置Struts2.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> <!-- 这句非常重要,把控制权交给Spring --> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <!-- 让struts2支持动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="default" namespace="/" extends="struts-default"> </package> <!-- <include file="example.xml" /> --> <!-- Add packages here --> </struts>
启用Struts2的动态方法调用。并且一定要有这个default package继承struts-default,不然会报错
有关于Struts2的配置项如下:
<?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> <!-- 指定由spring负责action对象的创建 --> <constant name="struts.objectFactory" value="spring" /> <!-- 所有匹配*.action的请求都由struts2处理 --> <constant name="struts.action.extension" value="action" /> <!-- 是否启用开发模式 --> <constant name="struts.devMode" value="true" /> <!-- struts配置文件改动后,是否重新加载 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 设置浏览器是否缓存静态内容 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 请求参数的编码方式 --> <constant name="struts.i18n.encoding" value="utf-8" /> <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 --> <constant name="struts.i18n.reload" value="true" /> <!-- 文件上传最大值 --> <constant name="struts.multipart.maxSize" value="104857600" /> <!-- 让struts2支持动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- Action名称中是否还是用斜线 --> <constant name="struts.enable.SlashesInActionNames" value="false" /> <!-- 允许标签中使用表达式语法 --> <constant name="struts.tag.altSyntax" value="true" /> <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true --> <constant name="struts.dispatcher.parametersWorkaround" value="false" /> <package name="basePackage" extends="struts-default"> </package> </struts>
接下来用annotation的方式配置action,在这之前不要忘记导入struts2-convention.jar 这个包
在action类名上写@Action(value="xxx")
再写上@Namespace("/"),"/"可以换成其他的
差不多就完成了,接下来部署项目启动,然后在浏览器输入http://localhost:8080/sshanno/empAction!fandAll
!后面是你要调用的方法名,不要带(),
看控制台里打印出"dao"就成功了
接下来再加入hibernate
不要忘记导入hibernate-commons-annotations.jar这个包
说是纯注解,不过一些功能就得在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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="scott"></property> <property name="password" value="tiger"></property> </bean> <!-- 配置sessionFactory --> <!-- 要用annotation就要指定这个类名 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- hibernate的设置 --> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="show_sql">true</prop> </props> </property> <!-- 自动扫描注解方式配置的hibernate类文件 --> <property name="packagesToScan"> <list> <value>com.bjsxt.entity</value> </list> </property> </bean> <!-- 事务管理器配置, Hibernate单数据源事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 自动包扫描 --> <context:component-scan base-package="com.bjsxt" /> </beans>
事务最直观的做法是加在dao层,不过最正确的做法是加在service层
在service类上面写@Transactional(propagation=Propagation.REQUIRED)
有事务就用,没事务就创建一个,默认的就是这个,可以不写
具体的用法请百度吧
附上service和dao的代码
EmpServiceImpl:
@Service @Transactional(propagation=Propagation.REQUIRED) public class EmpServiceImpl { @Resource private EmpDaoImpl empDao; public EmpDaoImpl getEmpDao() { return empDao; } public void setEmpDao(EmpDaoImpl empDao) { this.empDao = empDao; } public void fandAll(){ empDao.fandAll(); } }
EmpDaoImpl:
@Repository public class EmpDaoImpl { @Resource private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void fandAll(){ sessionFactory.openSession(); Query query = sessionFactory.getCurrentSession().createQuery("from Emp e"); List<Emp> emps = query.list(); for (Emp emp : emps) { System.out.println(emp.getEname()); } System.out.println("dao"); } }
忘了,还没有用注解的方式配置bean呢
在bean类名上加@Entity
再加@Table(name="你表名")
这两个一定要加入javax.persistence下的,不好加hibernate的,否则跟没加一样
主键加@Id
详细内容请参考http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html
下面是Emp实体类
Emp:
@Entity @Table(name="EMP") public class Emp { @Id private Integer empno; private String ename; private String job; private Integer mgr; private Date hireDate; private Integer sal; private Integer comm; private Integer deptno; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public Integer getSal() { return sal; } public void setSal(Integer sal) { this.sal = sal; } public Integer getComm() { return comm; } public void setComm(Integer comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } }
部署启动程序,输入http://localhost:8080/sshanno/empAction!fandAll
控制台里如果打印出来emp表里的ename就成功了
如果启动中出现以下错误:
java.lang.NoClassDefFoundError: javax/persistence/Entity
缺少ejb3-persistence.jar
java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
缺少
slf4j-api-1.5.8.jar
slf4f-log4j12-1.5.8.jar
你得有log4j的jar
当然你也不用加这俩,加入slf实现类的jar,不过还是推荐使用log4f。不为什么,主流。
唉,我就是个渣渣