SSH整合
来源:互联网 发布:mac 游戏排行 编辑:程序博客网 时间:2024/06/15 10:35
1.整合Hibernate
1.1.在pom.xml中配置Hibernate依赖包和My_sql数据库驱动包
<!-- 引入Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- 引入MySQL依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
1.2 在项目中建包
com.entity
com.dao
com.biz
com.action
1.3 创建表的实体类 Student 进行封装,添加构造方法
1.4 创建表的对象映射文件 student.hbm.xml
修改主键生成策略 关系
1.5 创建hibernate.cfg.xml核心配置文件
设置数据库配置
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<!-- 设置hibernate配置 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 加载映射文件 -->
<mapping resource="com/entity/Fenlei.hbm.xml"/>
1.6 测试一下 (创建测试类)
@Test
public void testHibernate(){
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
try{
Transaction tx = session.beginTransaction();
//测试添加一条记录
session.save(new Student("小明","男",18));
tx.commit();
}catch(){
tx.roolback;
}finally{
session.close();
sessionFactory.close();
}
2.整合Spring
<!-- 引入Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
2.1 创建Spring配置文件
applicationContext-public.xml
这个配置文件约束需要配置多个
aop 选最后一个4.3.xsd
beans 选最后一个4.3.xsd
context 选最后一个4.3.xsd
applicationContext-dao.xml
applicationContext-entity.xml
applicationContext-biz.xml
applicationContext-action.xml
//每一个包配置一个xml文件
2.2.创建数据库连接池配置文件
使用数据库连接池配置文件 需要使用第三方插件c3p0
db.properties //框架中会用一个优秀的数据库框架c3p0
<!-- 引入c3p0数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
配置:
uname=root
upass=tiger //不要写password 因为 applicationContext-public.xml 配置数据源有一个name是password
url=jdbc:mysql://localhost:3306/test
driver_Class=com.mysql.jdbc.Driver
//c3p0设置
initPoolSize=3 //最小连接数
maxPoolSize=20 //最大连接数
注意:整合时使用数据库连接池技术,hibernate.cfg.xml中的设置数据库配置代码可以删除。
2.3 在applicationContext-public.xml中配置(Spring整合重点)
<!-- 1引入db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<!-- 引入C3p0中的ComboPooledDataSource类 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 读取db.properties中的值 这样hibernate.cfg.xml中的设置数据库配置就可以删掉了 -->
<property name="user" value="${uname}"></property>
<property name="password" value="${upass}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver_Class}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
2.4 <!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!--加载hibernate核心配置文件 (hibernate核心文件交给spring来加载)-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!--加载hibernate映射文件 这样hibernate.cfg.xml中的加载映射文件可以删除了-->
<property name="mappingLocations" value="classpath:com/entity/*.hbm.xml"></property>
</bean>
2.5 <!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
2.6 配置事务的属性
配置事务的属性需要使用到tx标签
<!--
在<beans>标签中复制xmlns:aop="http://www.springframework.org/schema/aop"
修改成:xmlns:tx="http://www.springframework.org/schema/tx"
复制最后一行:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
改成:http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
这样<tx:>就可以使用。
<tx:advice>表示通知/增强,AOP在项目中是通过事务来体现,而事务的底层是通知。
-->
事务的属性
<tx:advice id="myadvice" transaction-manager="transactionManager">
<tx:attributes>
//当方法以add开头,自动属性,这里就明确了如果做增加,方法就要以add开头。
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
//除了增删改,其它方法没有事务
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
2.7 配置事务的切点
<!-- 配置事务的切点 //指明哪些包里的哪些类,哪些类中的哪些方法要事务-->
<aop:config>
<aop:pointcut expression="execution(* com.dao.*.*(..))" id="myCut"/>
<!--注意:需要再次引入一个依赖(在下面):spring-aspects,用于解析上面这个表达式-->
<!-- 关联事务的属性 -->
<aop:advisor advice-ref="myadvice" pointcut-ref="myCut"/>
</aop:config>
<!-- spring 的AspectJ依赖,解析事务切点 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
2.8 编写底层代码
2.8.1 在dao包中:
1. 编写:IStudentDao
public void add(Student stu);
2. 编写实现类:StudentdaoImpl,实现接口IStudentDao
2.1.声明sessionFactory
private SessionFactory sessionFactory; //写set和get方法
2.2 . 写得到session的方法
public Session getSession(){
return sessionFactory.getCurrentSession();
}
2.3 //重写方法
public void add(Fenlei fenlei) {
//d.通过session做保存操作
getSession().save(fenlei);
}
3. 在applicationContext-dao.xml中配置对象
<bean id="fenleidaoImpl" class="com.dao.FenleidaoImpl">
<!-- //注入:ref="sessionFactory"引用的是跨文件的id,引用applicationContext-public.xml中id="sessionFactory"对象 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
4.测试类
@Test
public void test(){
//加载spring文件
ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"applicationContext-public.xml","applicationContext-dao.xml"});
//测试能否得到sessionFactory
// Object o=ac.getBean("sessionFactory");
// System.out.println(o);
//通过getBean()取出实现类对象后不是给实现类,而是给实现类的父接口
IFenleidao ifenleidao=(IFenleidao) ac.getBean("fenleidaoImpl");
ifenleidao.add(new Fenlei("哈哈哈", 1));
}
//注意:如果运行报错sessionFactory错误,那么有可能是数据源、hibernate核心配置文件、映射文件出了问题。因为这三者出问题,都会报sessionFactory错误,applicationContext-public.xml配置文件中已表现出。
3.整合Struts2
1.在pom.xml中导入struts依赖
<!-- 引入struts2依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
<!-- struts2整合Spring的整合包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
2.创建struts.xml
3.配置web.xml文件
<!-- 加载Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
4.创建表单
<form action="fenleiActionadd.action" method="post">
<input type="text" name="fenlei.dafenleiname">
<!-- //注意:这里的stu.sname中stu一定要与action中实体类名相同 -->
<input type="submit" value="添加">
</form>
5.在biz包中配置IFenleiBiz(FenleiDao的biz接口),里面的方法与IFenleiDao一致
public void add(Fenlei fenlei);
5.2 创建biz的实现类 Fenleiimpl 实现 IFenleiBiz接口
//dao包中FenleiDaoImpl的父接口
private IFenleidao ifenleidao;
public IFenleidao getIfenleidao() {
return ifenleidao;
}
public void setIfenleidao(IFenleidao ifenleidao) {
this.ifenleidao = ifenleidao;
}
重写方法
public void add(Fenlei fenlei) {
ifenleidao.add(fenlei);
}
//同时需要在applicationContext-biz.xml中配置FenleiBizImpl,为ifenleidao注入一个对象
<bean id="fenleibizimpl" class="com.biz.Fenleibizimpl">
<property name="ifenleidao" ref="fenleidaoImpl"></property>
<!--name放实现类里声明的父接口的 ref放applicationContext-dao.xml中的对象 -->
</bean>
7.在resources中创建struts.xml配置文件,配置文件的跳转
准备一个success.jsp,先确保流程跳转无误。
<struts>
<package name="myPackage" extends="struts-default">
<action name="fenleiAction*" class="fenleiAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
8.在web.xml中加载Spring
<!--加载Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
//注意:<context-param>在web.xml中的dtd约束中有要求:放在过滤器的上面。
<!--添加监听-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- SSH 整合
- SSH整合
- ssh整合
- SSH整合
- SSH 整合
- SSH整合
- ssh整合
- SSH整合
- ssh整合
- SSH整合
- SSH整合
- SSH整合
- ssh整合
- SSH整合
- Ssh整合
- ssh 整合
- SSH整合
- SSH整合
- SSH整合出现的一些问题
- 堆排序
- MFC中复选框Check控件用法
- Editor.md和markdown实现Sg一样的截图粘贴上传
- Oracle05--日期运算
- SSH整合
- 【Oracle】创建用户配额总是不足的解决问题 quota
- git status .查看时,显示乱码,解决办法
- 小example说明java的值传递和引用传递,基础变量和对象在内存里的区别,堆栈
- java抽象类和接口的区别
- 安装Tensorflow(入坑指南)
- js和jquery实现监听键盘事件示例代码
- 微信JS SDK使用权限签名算法 jsapi_ticket
- Spring和Hibernate的整合