码迷,mamicode.com
首页 > 其他好文 > 详细

ssh注解整合

时间:2017-01-07 21:12:04      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:entity   ppi   tostring   strong   sea   local   name   全局对象   --   

ssh注解整合

导入java

 

配置struts2环境

1. 创建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="user" extends="struts-default" namespace="/">

<action name="user_*" class="userAction" method="{1}">

</action>

</package>

</struts>

 

2. 配置web.xml

   <!-- 配置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>

 

3. 创建SuperAction

public class SuperAction extends ActionSupport

implements ServletRequestAware, ServletResponseAware, ServletContextAware {

 

private static final long serialVersionUID = 1L;

HttpServletRequest request;// 请求对象

HttpServletResponse response;// 相应对象

HttpSession session;// 会话对象

ServletContext application;// 全局对象

 

public void setServletContext(ServletContext application) {

this.application = application;

}

 

public void setServletResponse(HttpServletResponse response) {

this.response = response;

}

 

public void setServletRequest(HttpServletRequest request) {

this.request = request;

this.session = this.request.getSession();

}

 

}

 

4. 创建UserAction

public class UserAction extends SuperAction{

 

public String execute() throws Exception {

return NONE;

}

}

 

搭建hibernate环境

1. 创建实体类

@Entity

@Table(name="user")

public class User {

@Id

@GeneratedValue

private Integer id;

 

private String username;

 

private String password;

//get set toString 省略

}

 

2. 创建hibernate核心映射文件

<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License:

GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the

lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->

<!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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 配置显示SQL -->

<property name="hibernate.show_sql">true</property>

<!-- 配置格式化SQL -->

<property name="hibernate.format_sql">true</property>

<!-- 配置hbm2ddl -->

<property name="hibernate.hbm2ddl.auto">update</property>

 

</session-factory>

</hibernate-configuration>

 

搭建Spring环境

1. 配置spring核心配置文件

 

<?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.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">
    <!-- 配置druid连接池 -->
<!---->

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.ityuhao"></context:component-scan>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
        <!-- 指定对哪个实体类进行映射配置 -->
        <property name="packagesToScan" value="com.ityuhao.domain"></property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 配置hibernate模板对象 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

 

2. 配置监听器

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:bean.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

创建actionservicedao,完成注入

1. action对象创建,注入service

@Component(value="userAction")

@Scope(value="prototype")

public class UserAction extends SuperAction{

private static final long serialVersionUID = 1L;

@Resource(name="userService")

private UserService userService;

 

public String execute() throws Exception {

userService.add();

return NONE;

}

}

 

2. service对象创建,注入dao

@Service

@Transactional

public class UserService {

@Resource(name = "userDAO")

private UserDAO userDAO;

 

public void add() {

// TODO Auto-generated method stub

userDAO.add();

}

 

}

 

3. dao注入hibernate模板对象

@Repository

public class UserDAO {

@Resource(name="hibernateTemplate")

private HibernateTemplate hibernateTemplate;

 

public void add() {

User user = new User();

user.setUsername("admin");

user.setPassword("admin");

hibernateTemplate.save(user);

}

}

 

ssh注解整合

标签:entity   ppi   tostring   strong   sea   local   name   全局对象   --   

原文地址:http://www.cnblogs.com/ityuhao/p/6260260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!