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

来源:互联网 发布:淘宝专柜小样是真的吗 编辑:程序博客网 时间:2024/05/22 01:28

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


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


jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/sshfw?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=roundjdbc.username=rootjdbc.password=xxxxxx

 


1234567891011log4j.rootLogger=debug, stdoutlog4j.logger.java.sql.Connection=info, stdoutlog4j.logger.java.sql.Statement=debug, stdoutlog4j.logger.java.sql.PreparedStatement=debug, stdoutlog4j.logger.org.hibernate=errorlog4j.logger.org.hibernate.SQL=debuglog4j.logger.org.hibernate.tool.hbm2ddl=debuglog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss} %c:%L - %m%nlog4j.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
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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-placeholderlocation="classpath*:jdbc.properties"/>
    <context:component-scanbase-package="com.lavasoft.demo.dao"/>
    <context:component-scanbase-package="com.lavasoft.demo.service"/>
    <context:component-scanbase-package="com.lavasoft.demo.web.action"/>
    <!-- 配置系统的数据源 -->
    <beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close">
        <propertyname="driverClassName"value="${jdbc.driver}"/>
        <propertyname="url"value="${jdbc.url}"/>
        <propertyname="username"value="${jdbc.username}"/>
        <propertyname="password"value="${jdbc.password}"/>
        <propertyname="filters"value="stat"/>
        <propertyname="maxActive"value="10"/>
        <propertyname="initialSize"value="1"/>
        <propertyname="maxWait"value="60000"/>
        <propertyname="minIdle"value="1"/>
        <propertyname="timeBetweenEvictionRunsMillis"value="60000"/>
        <propertyname="minEvictableIdleTimeMillis"value="300000"/>
        <propertyname="validationQuery"value="SELECT 'x'"/>
        <propertyname="testWhileIdle"value="true"/>
        <propertyname="testOnBorrow"value="false"/>
        <propertyname="testOnReturn"value="false"/>
        <propertyname="poolPreparedStatements"value="true"/>
        <propertyname="maxPoolPreparedStatementPerConnectionSize"value="50"/>
        <propertyname="maxOpenPreparedStatements"value="100"/>
    </bean>
    <beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <propertyname="dataSource"ref="dataSource"/>
        <propertyname="hibernateProperties">
            <props>
                <propkey="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <propkey="hibernate.current_session_context_class">thread</prop>
                <propkey="hibernate.show_sql">true</prop>
                <propkey="hibernate.format_sql">true</prop>
                <propkey="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <propertyname="packagesToScan"value="com.lavasoft.demo.entity"/>
    </bean>
    <beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <propertyname="sessionFactory"ref="sessionFactory"/>
    </bean>
    <tx:adviceid="txAdvice"transaction-manager="transactionManager">
        <tx:attributes>
            <tx:methodname="select*"read-only="true"/>
            <tx:methodname="get*"read-only="true"/>
            <tx:methodname="load*"read-only="true"/>
            <tx:methodname="find*"read-only="true"/>
            <tx:methodname="query*"read-only="true"/>
            <tx:methodname="read*"read-only="true"/>
            <tx:methodname="sync*"/>
            <tx:methodname="*"propagation="REQUIRED"rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcutid="pointcut"expression="execution(* com.lavasoft.demo.service.*Impl.*(..))"/>
        <aop:advisoradvice-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
<?xmlversion="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>
    <constantname="struts.devMode"value="true"/>
    <constantname="struts.i18n.encoding"value="UTF-8"/>
    <constantname="struts.convention.result.path"value="/WEB-INF/pages"/>
    <constantname="struts.convention.package.locators"value="web,action"/>
    <constantname="struts.objectFactory"value="spring"/>
    <constantname="struts.configuration.xml.reload"value="true"/>
    <packagename="demo"extends="struts-default"namespace="/demo">
        <global-results>
            <resultname="login">/index.jsp</result>
            <resultname="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
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="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 extendsActionSupport {
    @Resource
    privateUserService userService;
    privateUser user;
    privateList<User> userList;
    @Action(value ="regUser", results = {
            @Result(name ="success", location ="/WEB-INF/pages/login.jsp"),
            @Result(name ="input", location ="/WEB-INF/pages/error.jsp")})
    publicString reg() {
        System.out.println("----reg page----");
        returnSUCCESS;
    }
    @Action(value ="saveUser", results = {
            @Result(name ="success", location ="/WEB-INF/pages/list.jsp"),
            @Result(name ="input", location ="/WEB-INF/pages/error.jsp")})
    publicString save() {
        System.out.println("----save----");
        System.out.println(user);
        userService.saveUser(user);
        returnSUCCESS;
    }
    publicString list() {
        System.out.println("----list----");
        System.out.println(user);
        userList = userService.queryUserAll();
        returnSUCCESS;
    }
    publicList<User> getUserList() {
        returnuserList;
    }
    publicvoidsetUserList(List<User> userList) {
        this.userList = userList;
    }
    publicUserService getUserService() {
        returnuserService;
    }
    publicvoidsetUserService(UserService userService) {
        this.userService = userService;
    }
    publicUser getUser() {
        returnuser;
    }
    publicvoidsetUser(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 extendsBaseDaoImpl {
}


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 implementsjava.io.Serializable {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name ="id", unique =true, nullable =false)
    privateLong id;
    @Column(name ="username", length =32)
    privateString username;
    @Column(name ="password", length =16)
    privateString password;
    publicUser() {
    }
    publicUser(String username, String password) {
        this.username = username;
        this.password = password;
    }
    publicLong getId() {
        returnid;
    }
    publicvoidsetId(Long id) {
        this.id = id;
    }
    publicString getUsername() {
        returnusername;
    }
    publicvoidsetUsername(String username) {
        this.username = username;
    }
    publicString getPassword() {
        returnpassword;
    }
    publicvoidsetPassword(String password) {
        this.password = password;
    }
    @Override
    publicString 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
    privateUserDAO userDAO;
    publicvoidsaveUser(User user) {
        userDAO.save(user);
    }
    publicList<User> queryUserAll() {
        returnuserDAO.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>
<formaction="/sshfw/demo/saveUser"method="get"><br>
    用户名:<inputtype="text"name="user.username"/><br>
    密码:<inputtype="text"name="user.password"/>
    <inputtype="submit"name="保存"/>
</form>
</body>
</html>


1
2
3
4
5
6
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=6DEFAULT CHARSET=utf8


0 0