Struts2.3+Spring3.2.8+Hibernate4.1全注解配置

简介:

帮朋友搭建个简单的helloworld的平台,较简单,但是能用了。


给ssh的“零配置”一个正解。看我这个就够了ok了!


1
2
3
4
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


1
2
3
4
5
6
7
8
9
10
11
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<? 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: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" >
     < context:property-placeholder  location = "classpath*:jdbc.properties" />
     < context:component-scan  base-package = "com.lavasoft.demo.dao"  />
     < context:component-scan  base-package = "com.lavasoft.demo.service" />
     < context:component-scan  base-package = "com.lavasoft.demo.web.action" />
     <!-- 配置系统的数据源 -->
     < bean  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" />
     </ bean >
     < bean  id = "sessionFactory"  class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
         < property  name = "dataSource"  ref = "dataSource" />
         < property  name = "hibernateProperties" >
             < props >
                 < prop  key = "hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</ prop >
                 < prop  key = "hibernate.current_session_context_class" >thread</ 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"  value = "com.lavasoft.demo.entity" />
     </ bean >
     < bean  id = "transactionManager"  class = "org.springframework.orm.hibernate4.HibernateTransactionManager" >
         < property  name = "sessionFactory"  ref = "sessionFactory" />
     </ bean >
     < tx:advice  id = "txAdvice"  transaction-manager = "transactionManager" >
         < tx:attributes >
             < tx:method  name = "select*"  read-only = "true" />
             < tx:method  name = "get*"  read-only = "true" />
             < tx:method  name = "load*"  read-only = "true" />
             < tx:method  name = "find*"  read-only = "true" />
             < tx:method  name = "query*"  read-only = "true" />
             < tx:method  name = "read*"  read-only = "true" />
             < tx:method  name = "sync*" />
             < tx:method  name = "*"  propagation = "REQUIRED"  rollback-for = "Exception" />
         </ tx:attributes >
     </ tx:advice >
     < aop:config >
         < aop:pointcut  id = "pointcut"  expression = "execution(* com.lavasoft.demo.service.*Impl.*(..))" />
         < aop:advisor  advice-ref = "txAdvice"  pointcut-ref = "pointcut" />
     </ aop:config >
</ beans >


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<? 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 >
     < 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.jsp</ result >
             < result  name = "error" >/error.jsp</ result >
         </ global-results >
     </ package >
</ struts >


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< 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.html</ welcome-file >
     </ welcome-file-list >
     < filter >
         < filter-name >encodingFilter</ filter-name >
         < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class >
         < init-param >
             < param-name >encoding</ param-name >
             < param-value >UTF-8</ param-value >
         </ init-param >
     </ filter >
     < filter-mapping >
         < filter-name >encodingFilter</ filter-name >
         < url-pattern >/*</ url-pattern >
     </ filter-mapping >
     < 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 >
     < context-param >
         < param-name >log4jConfigLocation</ param-name >
         < param-value >classpath:log4j.properties</ param-value >
     </ context-param >
     < listener >
         < listener-class >org.springframework.web.util.Log4jConfigListener</ listener-class >
     </ listener >
     < context-param >
         < param-name >contextConfigLocation</ param-name >
         < param-value >classpath:spring-config.xml</ param-value >
     </ context-param >
     < listener >
         < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
     </ listener >
     < listener >
         < listener-class >org.springframework.web.context.request.RequestContextListener</ listener-class >
     </ listener >
     < listener >
         < listener-class >org.springframework.web.util.IntrospectorCleanupListener</ listener-class >
     </ listener >
</ web-app >


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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<User> 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<User> getUserList() {
         return  userList;
     }
     public  void  setUserList(List<User> 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;
     }
}


1
2
3
4
5
6
7
8
9
10
11
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 {
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 + '\ ''  +
                 '}' ;
     }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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<User> queryUserAll() {
         return  userDAO.findAll( "from User" , User. class );
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%--
   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 >


1
2
3
4
5
6
CREATE TABLE `t_demo` (
   `id` bigint( 20 ) NOT NULL AUTO_INCREMENT,
   `username`  var char( 32 ) DEFAULT NULL,
   `password`  var char( 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




本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/1401630,如需转载请自行联系原作者

相关文章
|
14天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
16 0
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——TestStu.java
hibernate正向生成数据库表以及配置——TestStu.java
18 1
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
13 1
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.java
hibernate正向生成数据库表以及配置——Teacher.java
11 0
|
1月前
|
Java 数据库连接
hibernate注解实体类(Dept.java)
hibernate注解实体类(Dept.java)
13 1
|
1月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Dept.java)
Hibernate中使用Criteria查询及注解——(Dept.java)
14 1
|
6天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
16 0
|
6天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
29 0
|
8天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
19 0
|
8天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置