开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

时间:2024-05-26 08:33:50
一:项目下载地址(点击 Source code(zip))

https://github.com/fzxblgong/frame_2014-12-15/releases

版本:v1.2
大小:20M

二:ssm(mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3) version v1.3 功能

新增:
+8.框架在支持mybatis-3.2.8基础上又整合进hibernate4,并支持注释。
+9.使用注释ssh方式实现JqueryMiniUi多选树。实例路径:/organization/organization_tree.jsp

1.action,service,dao,支持spring业务类注释方式依赖注入。 
2.mybatis支持接口注释开发,支持sql mapper的xml配置开发。 
3.集成log4j配置输出文件。 
4.集成常用异步上传ajaxFileupload测试实例。 测试路径:/ajaxfileupload/ajaxupload.jsp
5.集成上传进度百分比进度测试实例。(ajax异步sessionkey计算)
6.集成JqueryMiniUi前端框架。
7.集成用户列表展示功能。(包括分页查询,分页排序,条件查询,按列排序)测试路径:/user/userlist.jsp

三:运行环境

1.JDK "1.6.0_10-rc2";
2.MyEclipse6.5;
3.Tomcat6.0;
4.MySql5.0;
5.Windows7 32bit.

注:
1.因为jqueryminiui分Eclipse和Myeclipse版本,我集成的是Meclipse版本,虽然我没试过eclipse是否正常,但为了测试稳定最好用Myeclipse试下。
2.另外项目下有两个Junit4.0版本的测试类,测试类路径为/src/com/mybatistest,需要引入相关的Junit4.0支持库,谢谢。

代码片段(11)[全屏查看所有代码]

1. [图片] src目录结构.jpg

开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

2. [图片] 用户管理列表.jpg

开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

3. [图片] ajaxfileupload异步上传及进度.jpg

开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

4. [图片] organization树.jpg

开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

5. [代码]bean.xml spring配置文件

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?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">
    <context:annotation-config />
    <!-- 组件的扫描包路径,如@Service,@Repository注释的类,才能被spring找到,而spring只有找到这样的单例才能将其注入给需要的其他类使用 -->
    <context:component-scan base-package="com" />
    <!--
    <context:property-placeholder location="classpath:config/jdbc.properties"/>
     -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/mybatis" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation"
            value="classpath:mybatis-config.xml"/>
        <property name="typeAliasesPackage" value="com.mybatis.model"/>
    </bean>
    <!-- 注解方式(原生)
     
    <bean id="userMapper"
        class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface"
            value="com.mybatis.dao.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="userService" class="com.mybatis.service.UserService">
        <property name="userMapper" ref="userMapper" />
    </bean>-->
     
    <!-- 事物管理 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- MapperScannerConfigurer来帮我们自动扫描和注册Mapper接口,使用逗号或者分号进行分隔 参考:http://haohaoxuexi.iteye.com/blog/1843309-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mybatis.dao"/>
        <!-- MapperScannerConfigurer将只注册继承自markerInterface的接口:http://haohaoxuexi.iteye.com/blog/1843309-->
        <!--
        <property name="markerInterface" value="com.mybatis.dao.UserMapper"/>
         --> 
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!-- 在Dao中直接使用SqlSessionTemplate来编程 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
       <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
     
      <!-- hibernate配置 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 此配置为扫描com包下的n级model包下的实体,根据具体项目修改 -->
        <property name="packagesToScan" value="com.**.model" />
        <property name="hibernateProperties">
            <props>
                <!-- Oracle的方言
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                 -->
                <!-- Mysql的方言 -->
                <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>
    </bean>
    <!-- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/> </bean> -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <tx:annotation-driven transaction-manager="txManager" />
</beans>

6. [代码]1.支持注释 UserDaoImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.mybatis.basedao;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.mybatis.model.User;
@Repository
public class UserDaoImpl {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    public User getUserById(){
        User user = sqlSessionTemplate.selectOne("com.mybatis.dao.UserMapper.selectUserById", 1);
        return user;
    }
}

7. [代码]2.mybatis支持接口注释方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.mybatis.dao;
import com.mybatis.model.User;
public interface UserMapper {
     
    public User selectUserById(Integer id2);
    public void insertUser(User user);
    /**
     * 注释方式也可使用:
     * 百度:MyBatis-Spring-1.2.2 指导手册
     * @param userId
     * @return
     */
    /*@Select("SELECT * FROM users WHERE id = #{userId}")
    User getUser(@Param("userId") String userId);*/
}

8. [代码]3.log4j日志集成

1
2
3
4
5
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=D\:\\Test_Log4j.log
log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} %p %t %c - %m%n

9. [代码]4.ajaxFileUpload ajaxupload.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<img id="loading" src="${pageContext.request.contextPath}/ajaxfileupload/loading.gif" style="display:none;">
    <form name="form" action="" method="POST" enctype="multipart/form-data">
    <input id="fileToUpload" type="file" size="45" name="fileToUpload" class="input"/>
    <button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button>
    <div id ="percent" style="border:1px solid blue;width:200px;height:15px;" >
        <div id="percontent">
        </div>
    </div>
function ajaxFileUpload()
{
//执行异步上传...
}
function getPer(){
//获得百分比例进度
}

10. [代码]OrganizationAction.java

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.myssh.action;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.myssh.model.Organization;
import com.myssh.service.OrganizationService;
import com.ssh.baseaction.BaseAction;
import com.util.BeanToMapUtil;
@Component
public class OrganizationAction extends BaseAction{
    @Autowired
    private OrganizationService organizationService;
    @Override
    public Object getModel() {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void prepare() throws Exception {
        // TODO Auto-generated method stub
         
    }
    public String toOrganizationTree(){
        return "to_organization_tree";
    }
    public void getTreeDataList() throws IOException, IntrospectionException, IllegalAccessException, InvocationTargetException{
        List<Organization> organizationList = this.organizationService.getOrgTree();
        List<Map> orgMapList = new ArrayList<Map>();
        for(Organization org : organizationList){
            Map orgMap = BeanToMapUtil.convertBean(org);
            orgMapList.add(orgMap);
            System.out.println(orgMap);
        }
        String json = com.util.JSON.Encode(organizationList);
        System.out.println(json);
        this.setAjax(json);
    }
}

11. [代码]organization_tree.jsp JqueryMiniUi的tree实现实例

1
2
3
4
5
6
<ul id="tree2" class="mini-tree" url="${pageContext.request.contextPath}/organization/getTreeDataList.do" style="width:300px;height:250px;padding:5px;"
       showTreeIcon="true" textField="name" idField="id" parentField="p_id" resultAsTree="false" 
       allowSelect="false" enableHotTrack="false" expandOnLoad="true"
       showCheckBox="true" checkRecursive="false" autoCheckParent="true"
       >
   </ul>