转载自:http://lavasoft.blog.51cto.com/62575/1401630/
SSH2全注解
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sshfw?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=round
jdbc.username=root
jdbc.password=leizm
log4j.properties
log4j.rootLogger=debug, stdout
log4j.logger.java.sql.Connection=info, stdout
log4j.logger.java.sql.Statement=debug, stdout
log4j.logger.java.sql.PreparedStatement=debug, stdout
log4j.logger.org.hibernate=error
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.tool.hbm2ddl=debug
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss} %c:%L - %m%n
log4j.category.org.springframework = ON
application.xml (Spring)
version="1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
property-placeholder location="classpath*:jdbc.properties"/>
"com.lavasoft.demo.dao" />
"com.lavasoft.demo.service"/>
"com.lavasoft.demo.web.action"/>
-- 配置系统的数据源 -->
id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="filters" value="stat"/>
<property name="maxActive" value="10"/>
<property name="initialSize" value="1"/>
<property name="maxWait" value="60000"/>
<property name="minIdle" value="1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="50"/>
<property name="maxOpenPreparedStatements" value="100"/>
id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialectprop>
<prop key="hibernate.current_session_context_class">threadprop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
property>
<property name="packagesToScan" value="com.lavasoft.demo.entity"/>
id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
id="txAdvice" transaction-manager="transactionManager">
name="select*" read-only="true"/>
name="get*" read-only="true"/>
name="load*" read-only="true"/>
name="find*" read-only="true"/>
name="query*" read-only="true"/>
name="read*" read-only="true"/>
name="sync*"/>
name="*" propagation="REQUIRED" rollback-for="Exception"/>
id="pointcut" expression="execution(* com.lavasoft.demo.service.*Impl.*(..))"/>
ref="txAdvice" pointcut-ref="pointcut"/>
struts.xml
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.convention.result.path" value="/WEB-INF/pages"/>
<constant name="struts.convention.package.locators" value="web,action"/>
<constant name="struts.objectFactory" value="spring"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="demo" extends="struts-default" namespace="/demo">
<global-results>
<result name="login">/index.jspresult>
<result name="error">/error.jspresult>
global-results>
package>
struts>
web.xml
<web-app version="2.5" 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_2_5.xsd">
<welcome-file-list>
<welcome-file>login.htmlwelcome-file>
welcome-file-list>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<context-param>
<param-name>log4jConfigLocationparam-name>
<param-value>classpath:log4j.propertiesparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-config.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
listener>
web-app>
Action.java
package com.lavasoft.demo.web.action;
import com.lavasoft.demo.entity.User;
import com.lavasoft.demo.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by Administrator on 14-4-23.
*
* @author leizhimin 14-4-23 下午3:09
*/
@Namespace("/demo")
@Component
@Scope("prototype")
public class UserAction extends ActionSupport {
@Resource
private UserService userService;
private User user;
private List userList;
@Action(value = "regUser", results = {
@Result(name = "success", location = "/WEB-INF/pages/login.jsp"),
@Result(name = "input", location = "/WEB-INF/pages/error.jsp")})
public String reg() {
System.out.println("----reg page----");
return SUCCESS;
}
@Action(value = "saveUser", results = {
@Result(name = "success", location = "/WEB-INF/pages/list.jsp"),
@Result(name = "input", location = "/WEB-INF/pages/error.jsp")})
public String save() {
System.out.println("----save----");
System.out.println(user);
userService.saveUser(user);
return SUCCESS;
}
public String list() {
System.out.println("----list----");
System.out.println(user);
userList = userService.queryUserAll();
return SUCCESS;
}
public List getUserList() {
return userList;
}
public void setUserList(List userList) {
this.userList = userList;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
UserDao.java
package com.lavasoft.demo.dao;
import com.lavasoft.sshfw.core.BaseDaoImpl;
import org.springframework.stereotype.Repository;
/**
* Created by Administrator on 14-4-23.
*
* @author leizhimin 14-4-23 下午6:43
*/
@Repository
public class UserDAO extends BaseDaoImpl {
}
User.java
package com.lavasoft.demo.entity;
import javax.persistence.*;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name = "t_demo")
public class User implements java.io.Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "username", length = 32)
private String username;
@Column(name = "password", length = 16)
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserService.java
package com.lavasoft.demo.service;
import com.lavasoft.demo.dao.UserDAO;
import com.lavasoft.demo.entity.User;
import com.lavasoft.sshfw.core.BaseDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by Administrator on 14-4-23.
*
* @author leizhimin 14-4-23 下午6:46
*/
@Service
public class UserService {
@Resource
private UserDAO userDAO;
public void saveUser(User user) {
userDAO.save(user);
}
public List queryUserAll() {
return userDAO.findAll("from User", User.class);
}
}
<%--
Created by IntelliJ IDEA.
User: leizhimin 14-4-23 下午5:51
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>title>
head>
<body>
<form action="/sshfw/demo/saveUser" method="get"><br>
用户名:<input type="text" name="user.username"/><br>
密码:<input type="text" name="user.password"/>
<input type="submit" name="保存"/>
form>
body>
html>
CREATE TABLE `t_demo` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(32) DEFAULT NULL,
`password` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
webcontent部署为:sshfw
访问地址:
http://localhost:8080/sshfw/demo/regUser
http://localhost:8080/sshfw/demo/saveUser?user.username=wer&user.password=666666666
转载自:http://lavasoft.blog.51cto.com/62575/1401630/