SSH配置

来源:互联网 发布:偶像活动美月卡片淘宝 编辑:程序博客网 时间:2024/06/05 20:31
DAO接口-------------------------------------------------------------------------
public interface ChannelDao {
public void addChannel(Channel c);
public void delChannels(String[] ids);
public void updateChannel(Channel c);
public PagerVO findChannels();
}






DAO实现---------------------------------------------------------------------------
@Transactional
@Repository("channelDao")
public class ChannelDaoImpl implements ChannelDao {


@Resource
private SessionFactory sessionFactory;


private Session getSession() {
return this.sessionFactory.getCurrentSession();
}


@Override
public void addChannel(Channel c) {
this.getSession().save(c);
}
}






ACTION---------------------------------------------------------------------------
@Controller("channelAction")
public class ChannelAction extends ActionSupport {


@Resource
private ChannelDao channelDao;

private Channel channel;

// 在这个方法中执行查询工作
@Override
public String execute() throws Exception {


HttpServletRequest request = ServletActionContext.getRequest();


PagerVO pv = channelDao.findChannels();


request.setAttribute("pv", pv);


// forward到channel_list.jsp
return SUCCESS;
}
        
        public Channel getChannel() {
return channel;
}


public void setChannel(Channel channel) {
this.channel = channel;
}
}










实体类--------------------------------------------------------------------------------


@Entity
@Table(name="t_article")
public class Article implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String title;//标题
   
    /* 
     * @ManyToMany 注释表示Article 是多对多关系的一端。 
     * @JoinTable 描述了多对多关系的数据表关系,name属性指定中间表名称。 
     * joinColumns 定义中间表与Article 表的外键关系,
     * 中间表t_Channel_Articles的article_id 列是article 表的主键列对应的外键列。 
     * inverseJoinColumns 属性定义了中间表与另外一端(Channel)的外键关系。 
     */  
    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)  
    @JoinTable(name = "T_Channel_Articles",   
            joinColumns ={@JoinColumn(name = "article_id", referencedColumnName = "id") },   
            inverseJoinColumns = { @JoinColumn(name = "channel_id", referencedColumnName = "id")   
    })  
private Set<Channel> channels; //所属频道
private int topicId; //文章所属的主题,如果不属于某个主题,则此值为0
private Date createTime; //创建时间
private Date updateTime; //更新时间
private Date deployTime; //发布时间
private int adminId; //本篇文章是由哪个管理员创建的

@OneToMany
@JoinColumn(name="articleId")
private List<Attachment> attachments; //文章对应的附件



public void addAttachment(Attachment attachment){
if(attachments == null){
attachments = new ArrayList();
}
attachments.add(attachment);
}

public void addChannel(Channel channel){
if(channels == null){
channels = new HashSet<Channel>();
}
channels.add(channel);
}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public List<Attachment> getAttachments() {
return attachments;
}
public void setAttachments(List<Attachment> attachments) {
this.attachments = attachments;
}
}




WEB.XML----------------------------------------------------------------------------------

 <!-- spring配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
  
  <filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>


<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>










与WEB.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: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-3.2.xsd  
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  


<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/cms_ssh1?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="minPoolSize" value="10"></property>
<property name="maxPoolSize" value="50"></property>
</bean>

<!-- Hibernate配置 -->
<bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" /> 
        <!-- 指定命名策略,例如相同的类名和表名自动建立上对应关系-->
        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
        </property>
         
        <property name="hibernateProperties">  
            <props>     
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
            </props>  
        </property> 
        <!-- 指定自动扫描包 -->
<property name="packagesToScan">
<list>
<value>cn.com.leadfar.cms.backend.model</value>
</list>
</property>
</bean>

<!-- 定义事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 申明annotation 加载事务驱动 -->
<tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true" />


<!-- 自动扫描包  -->
<context:component-scan base-package="cn.com.leadfar.cms"/>
</beans>
0 0
原创粉丝点击