(摘录笔记)JAVA学习笔记SSH整合搭建项目

1:当然是导jar包啦;

struts2:

spring:

hibernate:

至于这些jar包是什么作用,我想就不必我解释了,大家都懂得,ssh2基本的jar包;

还有一些其他jar包:struts2-spring-plugin-2.1.8.1.jar(struts2-spring整合使用的jar包) , c3p0-0.9.2-pre1.jar(使用链接池链接数据库)

2:添加struts.xml文件

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- Overwrite Convention -->
    <package name="users" namespace="/lishun" extends="struts-default">
    <!-- 这里的action是叫spring管理了,所以这里的class属性值是写的是该被注入Action的id名称,后面的action类会有提到 -->
        <action name="users" class="usersAction" method="execute">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>
复制代码

配置web.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 定义struts的核心监听器 -->
    <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>action2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
        
        <!-- 把struts与spring整合;;一定不要忘记导struts2-spring-plugin-2.1.8.1.jar 包 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application.xml</param-value>
    </context-param>
</web-app>
复制代码


配置hibernate.cfg.xml

复制代码
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 执行数据库操作时显示sql语句 --> <property name="show_sql">true</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 自动建表:若表不存在就创建表,若存在就不创建,若实体类发生改变就重新创建表 --> <property name="hbm2ddl.auto">update</property> <!-- 导入映射文件 --> <mapping resource="com/lishun/domian/Users.hbm.xml" /> </session-factory> </hibernate-configuration>

复制代码


配置application.xml

 

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    <!-- 自动扫描com.lishun包下的所有bean,再通过注解来对这些bean进行注入值 -->
    <context:component-scan base-package="com.lishun"></context:component-scan>
    
    <!-- 加载属性文件来读取数据库链接字符串  -->
    <context:property-placeholder location="claspath:DataBaseConnection.properties"/>
    
    <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 把hibernate给spring管理 -->
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="dataSource">
            
            <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <!-- 前面四项是数据库链接的四大字符串,这里是通过属性文件来读取数据库链接字符串 -->
                <property name="jdbcUrl" value="${jdbcUrl}"></property>
                <property name="user" value="${user}"></property>
                <property name="password" value="${password}"></property>
                <property name="driverClass" value="${driverClass}"></property>
                <!-- 其他配置 -->
                <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
                <property name="initialPoolSize" value="3"></property>
                <!--连接池中保留的最小连接数。Default: 3 -->
                <property name="minPoolSize" value="3"></property>
                <!--连接池中保留的最大连接数。Default: 15 -->
                <property name="maxPoolSize" value="5"></property>
                <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
                <property name="acquireIncrement" value="3"></property>
                <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
                <property name="maxStatements" value="8"></property>
                <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
                <property name="maxStatementsPerConnection" value="5"></property>
                <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
                <property name="maxIdleTime" value="1800"></property>
            </bean>
        </property>
    </bean>
    <!-- 配置sessionFactory的事务,基于注解的方式 -->
    <bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

复制代码

DataBaseConnection.properties属性文件

 

3:测试

这是我的目录结构,是一层调用着一层

控制器Action:

复制代码
@Controller("usersAction")//控制器注解,并定义改bean的id是usersAction,
@Scope("prototype")//声明bean的作用域为prototype:每次从容器获取bean都是新的对象。
public class UsersAction extends ActionSupport {
    
    @Resource
    private IUserService servise;
    public String execute(){
        servise.sava();
        return "success";
    }
}
复制代码

业务层seivice:

复制代码
@Service//service层注解
public class UserServiceBean implements IUserService {

    //给属性注入值
    @Resource
    public IUserDao userDao;

    @Override
    @Transactional //这个方法会自动开启事务和提交事务,当有异常时就回滚事务
    public void sava() {
        userDao.sava();
    }
}
复制代码

数据操作层dao:

复制代码
public class UserDaoBean implements IUserDao {

    //从配置文件注入sessionfactory值
    @Resource
    private SessionFactory seesionFactory;

    @Override
    public void sava() {
        Session session = seesionFactory.getCurrentSession();
        System.out.println(session);
        Users u=new Users();
        u.setUname("王尼玛");
        session.save(u);
    }
}
复制代码

最有一个javabean实体,这个实体很简单就只有id和name两个字段,测试用的没弄很多字段上去

复制代码
public class Users {
    private Integer uid;
    private String uname;
    public Integer getUid() {
        return uid;
    }
    public void setUid(Integer uid) {
        this.uid = uid;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    @Override
    public String toString() {
        return "Users [uid=" + uid + ", uname=" + uname + "]";
    }
}
复制代码

对应的映射文件

复制代码
<?xml version="1.0"?>
<!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.lishun.domian" >
 <!-- 定义类名和对应的表名 -->
  <class name="Users" table="tb_users">
  
    <id name="uid" type="integer">
        <!-- 主键是自增的 -->
         <generator class="native"></generator>
    </id>
    <property name="uname"  type="string">
    </property>
  </class>
</hibernate-mapping>
复制代码
 
文章摘自:http://www.cnblogs.com/lishun1005/p/4412252.html
posted @ 2015-04-09 22:27  长风傲天  阅读(318)  评论(0编辑  收藏  举报
[h1[ [B]天涯雪[/B] [/h1]