SSH环境搭建(一)使用配置文件整合SSH
SSH开发环境搭建:
第一个版本:保留spring,struts2,hibernate的配置文件.
一,准备实体类业务层和持久层
实体类Customer
public class Customer {
private int custId;
private String custName;
private String custSource;
private String custIndustry;
private String custLevel;
private String custAddress;
private String custPhone;
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return "Customer [custid=" + custId + ", custName=" + custName
+ ", custSource=" + custSource + ", custIndustry="
+ custIndustry + ", custLevel=" + custLevel + ", custAddress="
+ custAddress + ", custPhone=" + custPhone + "]";
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
}
持久层CustomerDao
public interface CustomerDao {
/*
*
* 查询所有客户
*/
public List<Customer> findAll();
/*
* 保存客户
*
*/
public void save(Customer customer);
}
/*
* 持久层实现类
*/
public class CustomerDaoImpl implements CustomerDao {
@Override
public List<Customer> findAll() {
System.out.println("实现了查询客户列表");
return null;
}
@Override
public void save(Customer customer) {
System.out.println("实现了保存客户");
}
}
业务层CustomerService
public interface CustomerService {
/*
*
* 查询所有客户
*/
public List<Customer> findAll();
/*
* 保存客户
*
*/
public void save(Customer customer);
}
/*
* 业务层实现类
*/
public class CustomerServiceImpl implements CustomerService {
CustomerDao customerDao = new CustomerDaoImpl();
@Override
public List<Customer> findAll() {
customerDao.findAll();
return null;
}
@Override
public void save(Customer customer) {
customerDao.save(customer);
}
}
二,搭建spring的ioc开发环境
在src目录下创建bean.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置service -->
<bean id="customerService" class="com.service.impl.CustomerServiceImpl">
</bean>
</beans>
搭建完环境后,测试是否搭建成功
public class Test02 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
CustomerService customerService = (CustomerService) ac.getBean("customerService");
customerService.findAll();
}
}
在成功搭建spring环境后,搭建hibernate环境
三 , 搭建hibernate开发环境
1, 在src目录下创建hibernate.fcg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--
配置SessionFactory
SessionFactory用于创建Session对象
Session对象就是hibernate中操作数据库的核心对象
创建SessionFactory必须的三个部分
第一部分:
连接数据库的信息
第二部分:
hibernate的可选配置
第三部分:
映射文件的位置
-->
<session-factory>
<!-- 第一部分:连接数据库的信息 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh?characterEncoding=UTF-8 </property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<!-- 显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化输出sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 采用哪种方式生成DDL语句
update:表示检查实体类的映射配置和数据库的表结构是否一致,如果不一致,更新表结构
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 配置连接池参数信息 -->
<property name="hibernate.c3p0.max_size">5</property> <!-- 最大连接数 -->
<property name="hibernate.c3p0.min_size">2</property> <!-- 最小连接数 -->
<property name="hibernate.c3p0.timeout">5000</property> <!-- 超时时间 -->
<property name="hibernate.c3p0.max_statements">100</property> <!-- 最大执行的命令格个数 -->
<property name="hibernate.c3p0.idle_test_period">30000</property> <!-- 空闲测试时间 -->
<property name="hibernate.c3p0.acquire_increment">2</property> <!-- 连接不够用时,每次增加的个数 -->
<!-- 把session和线程绑定,从而实现一个线程只有一个session -->
<!-- hibernate的session -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 映射配置文件的位置 -->
<mapping resource="com/entity/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
2, 在实体类目录下创建 实体类名.hbm.xml配置文件
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.entity">
<!-- name:类名 table:表名 -->
<class name="Customer" table="Customer">
<id name="custId" column="custId">
<!-- 主键生成方式
native : 使用本地数据库的自动增长能力
-->
<generator class="native"></generator>
</id>
<property name="custName" column="custName"></property>
<property name="custSource" column="custSource"></property>
<property name="custIndustry" column="custIndustry"></property>
<property name="custLevel" column="custLevel"></property>
<property name="custAddress" column="custAddress"></property>
<property name="custPhone" column="custPhone"></property>
</class>
</hibernate-mapping>
搭建完环境后,测试是否搭建成功
public class Test01 {
public static void main(String[] args) {
Customer customer = new Customer();
customer.setCustName("asd");
//加载配置文件
Configuration cfg = new Configuration();
cfg.configure();
//创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//获取session
Session session = factory.getCurrentSession();
//开启事务
Transaction tx = session.beginTransaction();
//执行操作
session.save(customer);
//提交事务
tx.commit();
//关闭资源
factory.close();
}
}
hibernate能够成功运行
在hibernate环境搭建成功后,需要改写持久层的代码,使其可以操作数据库
在持久层中使用HibernateTemplate代替session
使用HibernateTemplate,有一个很重要的原因就在于我们不想直接控制事务,不想直接去获取,打开Session,开始一个事务,处理异常,提交一个事务,最后关闭一个SessionHibernateTemplate 是Hibernate操作进行封装,我们只要简单的条用HibernateTemplate 对象,传入hql和参数,就获得查询接口,至于事务的开启,关闭,都交给HibernateTemplate 对象来处理我们自己只专注于业务,不想去作这些重复而繁琐的操作。我们把这些责任全部委托给了 HibernateTemplate,然后使用声明式的配置来实现这样的功能。
四,整合hibernate和spring
将hibernate的配置交给spring
即在spring里写hibernate的配置文件,这里是将hibernate的配置文件的位置告诉给spring,使spring获取hibernate配置文件的内容.
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 配置service -->
<bean id="customerService" class="com.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<!-- 配置Dao -->
<bean id="customerDao" class="com.dao.impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 配置hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- spring接管sessionFactory的创建
用spring提供的提高sessionFactory:LocalSessionFactoryBean
创建sessionFactory有三部分信息
就是hibernate配置文件的信息
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入hibernate配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="pt1"/>
<!-- 建立切入点表达式和事务通知的关联 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
</beans>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--
配置SessionFactory
SessionFactory用于创建Session对象
Session对象就是hibernate中操作数据库的核心对象
创建SessionFactory必须的三个部分
第一部分:
连接数据库的信息
第二部分:
hibernate的可选配置
第三部分:
映射文件的位置
-->
<session-factory>
<!-- 第一部分:连接数据库的信息 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh?characterEncoding=UTF-8 </property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<!-- 显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化输出sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 采用哪种方式生成DDL语句
update:表示检查实体类的映射配置和数据库的表结构是否一致,如果不一致,更新表结构
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 配置连接池参数信息 -->
<property name="hibernate.c3p0.max_size">5</property> <!-- 最大连接数 -->
<property name="hibernate.c3p0.min_size">2</property> <!-- 最小连接数 -->
<property name="hibernate.c3p0.timeout">5000</property> <!-- 超时时间 -->
<property name="hibernate.c3p0.max_statements">100</property> <!-- 最大执行的命令格个数 -->
<property name="hibernate.c3p0.idle_test_period">30000</property> <!-- 空闲测试时间 -->
<property name="hibernate.c3p0.acquire_increment">2</property> <!-- 连接不够用时,每次增加的个数 -->
<!-- 把session和线程绑定,从而实现一个线程只有一个session -->
<!-- <property name="hibernate.current_session_context_class">thread</property> -->
<!-- spring和hibernate整合时,绑定需改为下面这种方式,也可以不写,spring会自动帮我们绑定 -->
<property name="current_session_context_class">
org.springframework.orm.hibernate5.SpringSessionContext
</property>
<!-- 映射配置文件的位置 -->
<mapping resource="com/entity/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
持久层实现类:使用set注入的方式
/*
* 持久层实现类
*/
public class CustomerDaoImpl implements CustomerDao {
//spring提供的类似于session的对象
private HibernateTemplate hibernateTemplate;
@Override
public List<Customer> findAll() {
return (List<Customer>) hibernateTemplate.find("from Customer");
}
@Override
public void save(Customer customer) {
hibernateTemplate.save(customer);
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
业务层实现类:使用set注入方式
/*
* 业务层实现类
*/
public class CustomerServiceImpl implements CustomerService {
CustomerDao customerDao;
@Override
public List<Customer> findAll() {
customerDao.findAll();
return null;
}
@Override
public void save(Customer customer) {
customerDao.save(customer);
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
}
五, 整合spring和struts2
在src目录下创建struts2的配置文件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>
<package name="customer-action" extends="struts-default" namespace="/customer">
<action name="customerAction_*" class="com.action.CustomerAction" method="{1}">
<result name="{1}">/WEB-INF/{1}.jsp</result>
</action>
</package>
</struts>
在web.xml中配置spring监听器和struts2核心过滤器
<?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" version="3.0">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- struts2核心过滤器 -->
<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>
<!-- 配置spring监听器,用于监听servletContext对象创建,同时为我们创建spring容器
默认情况下:只能加载web-inf目录的spring配置文件,同时文件名为:applicationContext.xml
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
将src目录下spring的配置文件bean.xml复制到WEB-INF目录下,并改名为
applicationContext.xml
Action
/*
*
* 客户的动作类
*/
public class CustomerAction {
CustomerService customerService ;
/*
* 此种方式解决了容器多例问题,保证了容器每一个应用只有一个,但是代码变臃肿了
* 可以导入struts2-spring-plugin-2.3.34.jar,当引入这个插件后,原先所struts创建的action类,交给了spring创建。
在struts2-spring-plugin.jar中有一个struts-plugin.xml,里面声明了action类由spring工厂创建。
*
*/
/* public CustomerAction(){
ServletContext application = ServletActionContext.getServletContext();
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(application);
System.out.println(ac);
CustomerService customerService = (CustomerService) ac.getBean("customerService");
this.setCustomerService(customerService);
}*/
public String AddUI(){
return "AddUI";
}
public String ListUI(){
List<Customer> list = customerService.findAll();
System.out.println(list);
return "ListUI";
}
/*
* 提供set方法,让spring注入
*/
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
上一篇: OTA升级
下一篇: 图论算法——SPFA算法