ssh基本搭建,利用spring注解开发
来源:互联网 发布:淘宝民族风女裤 编辑:程序博客网 时间:2024/06/05 00:53
以下内容包括简单的ssh环境搭建和spring注解的简单使用。
把下面的配置文件复制过去改改内容就可以用了。
首先导入ssh所需的包
第一步、配置beans.xml (*)
spring+hibernate,beans.xml配置
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scanbase-package="pers"/>
<aop:aspectj-autoproxy/>
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<propertyname="driverClassName"value="org.gjt.mm.mysql.Driver"/>
<propertyname="url"value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
<propertyname="username"value="root"/>
<propertyname="password"value=""/>
<!-- 连接池启动时的初始值 -->
<propertyname="initialSize"value="1"/>
<!-- 连接池的最大值 -->
<propertyname="maxActive"value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<propertyname="maxIdle"value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<propertyname="minIdle"value="1"/>
</bean>
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="mappingResources">
<list>
<value>pers/po/Clazz.hbm.xml</value>
<value>pers/po/Student.hbm.xml</value>
</list>
</property>
<propertyname="hibernateProperties">
<value>
<!-- 方言 -->
hibernate.dialect=org.hibernate.dialect.MySQLDialect
<!-- 在SessionFactory创建时,自动检查数据库结构,或者将数据库schema的DDL导出到数据库.使用 create-drop时,在显式关闭SessionFactory时,将drop掉数据库schema.取值 validate | update | create | create-drop -->
hibernate.hbm2ddl.auto=update
<!-- 输出所有SQL语句到控制台.有一个另外的选择是把org.hibernate.SQL这个log category设为debug。eg. true | false -->
hibernate.show_sql=false
<!-- 在log和console中打印出更漂亮的SQL。取值 true |false -->
hibernate.format_sql=false
<!-- 能用来完全禁止使用二级缓存.对那些在类的映射定义中指定<cache>的类,会默认开启二级缓存.取值 true|false -->
hibernate.cache.use_second_level_cache=true
<!--允许查询缓存,个别查询仍然需要被设置为可缓存的取值true|false -->
hibernate.cache.use_query_cache=false
</value>
</property>
</bean>
<!-- 事务管理 -->
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
<!-- 事物注解支持 -->
<tx:annotation-driventransaction-manager="txManager"/>
</beans>
Beans.xml中有这几个需要修改的地方
1、
<context:component-scanbase-package="pers"/>
这是一个spring注解的扫描器,在xml配置了这个标签后,spring可以自动去扫描pers下面或者子包下面的java文件,如果扫描到有@Component@Controller@Service等这些注解的类,则把这些类注册为bean
2、
<propertyname="url"value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
<propertyname="username"value="root"/>
<propertyname="password"value=""/>
这里是数据库配置, url是数据库路径,Username是用户名,Password是密码。 我这个配置的是mysql
3、
<propertyname="mappingResources">
<list>
<value>pers/po/Clazz.hbm.xml</value>
<value>pers/po/Student.hbm.xml</value>
</list>
</property>
spring加载hibernate映射文件。Value值需要改成自己的映射文件路径
第二步、配置struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="test"namespace="/"extends="struts-default">
<actionname="stu"class="stuaction"method="show">
<resultname="list">/index.jsp</result>
</action>
</package>
</struts>
简单的标签、属性介绍:
"<package>"中
name:包名,要唯一,自己随便定义
namespace:虚拟路径
extends:每个包都应该继承struts-default包,因为struts2很多核心功能都是拦截器来实现。
"<action>中"
name:名字,路径的一部分,自己随便定义
class:处理类
method:用哪个方法处理
"<result>":视图
action的class可以写自己配制的actionbean名称,也可以写相对路径(如:cn.itcast.action.HelloWorldAction)。
第三步、配置web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
没有特别需要修改的地方,注意一下:
<param-value>classpath:beans.xml</param-value>
这个要写src下配置文件的名字,如果修改了文件名,这里记得改
第四步、给类加注解
简单介绍,bean注解分为这么几个:
@Service用于标注业务层组件、@Controller用于标注控制层组件(如struts中action)、
@Repository用于标注数据访问组件、即DAO组件。而@Component泛指组件,当组件不好归类的时候
这几个注解作用其实是一样,都是把这些类注册为bean,只是名字不一样。比如你全用@Service也ok,但不推荐这么做
1、首先给dao、action、service分别加上注解
Dao:
@Repository("studao")@Transactional
public class StudentDaoImpl {
@Resource
SessionFactory sessionFactory;
public boolean add(Student stu)
{
try{
sessionFactory.getCurrentSession().save(stu);
return true;
}catch (Exception e) {
// TODO: handleexception
return false;
}
}
public List<Student> list()
{
String hql = "fromStudent";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
returnquery.list();
// returnsessionFactory.getCurrentSession().createQuery("fromStudent").list();
}
public boolean remove(Student stu)
{
Session session = sessionFactory.getCurrentSession();
session.delete(stu);
return true;
}
public Student getStuById(int id)
{
String hql = "fromStudent stu where stu.id=:id";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setInteger("id",id);
return (Student)query.uniqueResult();
}
}
简单说明:
1、@Repository("studao"): 括号中是我们给这个bean起的名字,以便我们操作。
2、DAO比其他的组件多一个‘@Transactional’,在执行方法时自动处理一些事物功能,比如开启事物和提交事务,必写。
3、@Resource
SessionFactory sessionFactory;
@Resource用于获取bean,是先name匹配,如果没有,自动type匹配。
例子:根据name匹配:
@Resource(name=“beanName”)
这里没有制定name,自动进行了type匹配。获取的是我们在beans.xml中配置的sessionFactory
基于sessionFactory的基本的增删查改操作已经写出。
Service:
@Service("stuservice")
public class StudentServiceImpl {
@Resource
StudentDaoImpl studao;
public boolean add(Student stu)
{
returnstudao.add(stu);
}
public List<Student> getlist()
{
returnstudao.list();
}
public boolean remove(Student stu)
{
returnstudao.remove(stu);
}
public Student getStuById(int id)
{
returnstudao.getStuById(id);
}
}
简单说明:
之前说过@Resource的作用,比如这里需要获取前面的StudentDaoImpl,其实也可以使用name获取:@Resource(name=”studao”)。
studao是我们刚才配置的StudentDaoImpl的bean名
Action:
@Controller("stuaction")
public class StudentAction {
@Resource(name="stuservice")
StudentServiceImpl stuservice;
private Studentstu;
public Student getStu() {
returnstu;
}
public void setStu(Student stu) {
this.stu = stu;
}
public String show()
{
//获取所有的学生列表集合,并存到session里
ServletActionContext.getRequest().getSession().setAttribute("list",stuservice.getlist());
return"list";
}
}
- ssh基本搭建,利用spring注解开发
- SSH:Spring框架(利用注解实现spring基本配置详解)
- 利用注解实现spring基本配置详解
- 利用注解实现spring基本配置详解
- 利用注解实现spring基本配置详解
- Spring(四)利用注解实现spring基本配置详解
- SSH笔记-注解开发(1)开发环境搭建
- spring+springMVC+MyBatis注解开发框架搭建
- SSH框架搭建 笔记 (含spring注解驱动)
- Spring自动注入,利用注解实现spring基本配置详解,Spring注解快速入门
- 利用Maven搭建Spring的开发环境
- 利用Maven搭建Spring开发环境
- 利用Maven搭建Spring开发环境
- ssh项目搭建时,Spring的bean的基本配置
- SSH 注解开发
- SSH注解开发
- SSH注解方式开发
- SSH开发常用注解
- CopyOnWriteArrayList并发理解
- 20个有用的代码功能片段
- 初识nginx服务器配置之location配置规则
- 第十一周 阅读程序(5d)
- VIM 分屏 过滤技巧 + source insight 快捷键
- ssh基本搭建,利用spring注解开发
- 集成环信遇到的问题
- 怎么解决tomcat占用8080端口问题
- SVN常见问题及相关原因,供各位查阅
- React 入门实例教程
- Mysql 入门篇
- iOS Crash文件分析(二)
- Beyond Compare 如何进行韩文比较
- imagePicker 拍照时报错