搭建大型项目SSM框架

搭建大型项目SSM框架


  • 涉及技术:

    1. maven的继承、聚合等
    2. spring、springMVC、mybatis
    3. log4j
    4. Tomcat7、Web
    5. Mysql、BoneCP连接池等

================================================

  1. 案例代码采用3层结构表示
    • Controller
    • Service
    • Mapper(一般也成为DAO,这里不做详细解释)
    • 数据准备(user表,支持mariadb或mysql等关系型数据库,字段属性依次为id,name,age,sex,数据可自行添加)
1.定义pojo类,作为持久型对象,封装的数据与数据库中的表结构对应
public class User { 
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
    getter、setter...
}
2.定义mapper层接口及其Mapper.xml文件(sql)
public interface UserMapper {
    //查询全部的用户信息
    List<User> findAll();
}
3.定义Service接口及其实现类(业务层)
public interface UserService { List<User> findAll();}
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> findAll() {
        return userMapper.findAll();
    }
}
4.定义controller层
/**
 * 核心:WebApplicationContext,springMVC中锁特有的容器
 * @author bboyHan
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 定义浏览器的访问路径接收
     * 1.通过前端控制器将用户的url转发到具体的controller中
     * 2.通过转发查找最匹配的RequestMapping
     * 3.进行业务操作 controller - service - mybatis 依次调用
     * 4.准备返回数据(JSON串/页面名称)
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){
        //经过业务层获取数据
        List<User> userList = userService.findAll();
        //通过model对象返回数据,存入request域中
        model.addAttribute("userList", userList);   
        //页面跳转
        return "userList";
        /**
         * springMVC将返回的字符串经过视图解析器进行数据的拼接
         * -->prefix + userList + suffix
         * -->路径: /WEB-INF/views/userList.jsp
         */     
    }   
}

=================================

  1. 编写SSM框架配置文件(由sping容器进行管理)

    • mybatis/mappers/*.xml —mapper.xml的存储路径
    • mybatis/mybatis-config.xml
    • property/jdbc.properties
    • spring/springmvc.xml
    • spring/applicationContext.xml
    • spring/applicationContext-mybatis.xml
    • WEB-INF/web.xml

    上述文件路径相对于resource的根目录而言,这是我的配置文件管理路径,可根据项目实际需求进行合理布置,以上仅供参考

----------------web.xml---------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="jt-manage" version="2.5">
    <display-name>xxx-manage</display-name>
    <!-- 
        spring、springMVC的总结 
        配置方式:
            1.通过前端控制器直接扫描全部容器
            2.通过监听器的方式启动spring容器
        两种配置方式的差别:
            1.如果springMVC和spring的配置文件都是通过前端控制器扫描,则容器的启动是懒加载的(即浏览器发送请求后,前端控制器才会启动spring容器和springMVC容器)
            存在的弊端:如果此项目是大型项目,用户进行访问时,后台服务才将其启动,使得用户的等待时间较长(等待容器的启动)
            2.通过监听器的方式启动spring容器时,当tomcat启动时,监听器便会开始监听是否需要spring容器,如果检测到需要spring容器,则
            监听器会在后台启动spring容器
            当用户通过浏览器访问时,首先将请求发给springMVC,springMVC通过spring容器获取具体的对象直接完成业务调用,用户不需要等待容器启动,等待时间较短
    -->
    <!-- 配置监听器启动spring容器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/applicationContext*.xml</param-value>
    </context-param>
    <!-- 1.配置前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置加载springMVC -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--  /  表示拦截全部请求,拦截所有静态资源(默认的一种机制),放行.jsp资源 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 配置全站乱码,解决post乱码 -->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <!-- /* 表示过滤全部 -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
------------springmvc---------------
<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 1.开启springMVC的注解 -->
    <mvc:annotation-driven/>    
    <!-- 2.开启包扫描controller -->
    <context:component-scan base-package="com.xx
                                          .manage"/>    
    <!-- 3.配置视图解析器 : 内部资源视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置前缀和后缀 -->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
---------------applicationContext.xml---------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 1.开启包扫描  让spring扫描所有的项目-->
    <context:component-scan base-package="com.xx"/>

    <!-- 2.0导入外部配置文件 -->
    <!-- 方法一:jdbc.properties -->
    <!-- <context:property-placeholder location="classpath:/property/jdbc*.properties"/> -->
    <!-- 方法二:通过标签引入property时,location属性中引入文件路径,只能引入文件名开头相同的配置文件 -->
    <!-- 方法三: PropertyPlaceholderConfigurer-->
    <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/property/jdbc.properties</value>
            </list>
        </property>
    </bean>

    <!-- 3.配置数据源 通常使用dataSource作为id使用-->
    <!-- 3.1配置连接池 -->
    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <!-- 数据库驱动 -->
        <property name="driverClass" value="${jdbc.driver}" />
        <!-- 相应驱动的jdbcUrl -->
        <property name="jdbcUrl" value="${jdbc.url}" />
        <!-- 数据库的用户名 -->
        <property name="username" value="${jdbc.username}" />
        <!-- 数据库的密码 -->
        <property name="password" value="${jdbc.password}" />
        <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
        <property name="idleConnectionTestPeriod" value="60" />
        <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
        <property name="idleMaxAge" value="30" />
        <!-- 每个分区最大的连接数 -->
        <property name="maxConnectionsPerPartition" value="150" />
        <!-- 每个分区最小的连接数 -->
        <property name="minConnectionsPerPartition" value="5" />
    </bean>

    <!-- 4.配置声明式事务处理 -->
    <!-- 
        作用:通过自定义的事务策略,描述某些方法需要事务,某些方法不需要事务设置为只读状态 
        步骤:
            1.声明事务管理器
            2.自定义事务策略&配置事务通知
            3.生成事务切面
    -->
    <!-- 4.1 定义事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 添加数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 4.2 定义事务通知及策略  -->
    <!-- 
        id:唯一的标识通知,
        transaction-manager:默认的名称为transactionManager,如果事务管理的名称一致则可省略
        事务策略:
            新增:insert,save,add
            修改:update
            删除:delete
            查询:find,select,query
        自定义事务策略:
            <tx:method name="save*" propagation="REQUIRED"/>
            name:需要控制的方法,一般与*号结尾,表示通配
            propagation:事务的传播属性 - required:必须添加事务; supports:表示事务支持,一般按这配:
    -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>      
            <tx:method name="find*" propagation="SUPPORTS"/>
            <tx:method name="select*" propagation="SUPPORTS"/>      
            <!-- 其他方法使用只读  ,不允许更新操作-->
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 4.3 配置事务切面  通知+切入点=切面-->
    <!-- 
        拦截:execution(返回值类型  包名.类名.方法名(参数列表)) 
    -->
    <aop:config>
        <aop:pointcut expression="execution(* com.jt.manage.service..*.*(..))" id="pc"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>
</beans>
----------applicationContext-mybatis.xml-----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 1.定义sqlSessionfactoryBean -->
    <!-- 
        步骤:
            1.添加数据源
            2.加载核心配置文件
            3.添加映射文件
            4.配置别名包
     -->
    <bean id="sqlSessionfactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:/mybatis/mappers/*.xml"></property>
        <property name="typeAliasesPackage" value="com.xx.manage.pojo"></property>
    </bean> 
    <!-- 2.为Mapper接口生成代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 定义接口的包路径  basePackage: 根据配置的包路径去扫描所有的Mapper接口文件并且为其创建代理(JDK代理),交给spring管理-->
        <property name="basePackage" value="com.xx.manage.mapper"></property>
    </bean>
</beans>
-------jdbc.properties----------
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xx库?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=用户名
jdbc.password=密码
---------mybatis-config.xml-------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 开启驼峰自动映射 -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
        <!-- 二级缓存的总开关,被redis替代 -->
        <setting name="cacheEnabled" value="false" />
    </settings>
</configuration>
----------userMapper.xml---------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.manage.mapper.UserMapper">
    <!-- namespace:唯一标识映射文件 -->
    <!-- 别名包在spring中定义 -->
    <select id="findAll" resultType="User">
        select * from user
    </select>
</mapper>

================================

  1. 前端页面显示

    ​ 本示例主要供后台开发管理人员的技术讨论,前端页面大家可以自由发挥。此处省略…

===============================

  1. pom.xml配置
<!-- 集中定义依赖版本号 -->
    <properties>
        <junit.version>4.10</junit.version>
        <spring.version>4.1.3.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <bonecp-spring.version>0.8.0.RELEASE</bonecp-spring.version>
        <druid.version>1.0.9</druid.version>

        <mapper.version>2.3.2</mapper.version>
        <pagehelper.version>3.4.2</pagehelper.version>
        <jsqlparser.version>0.9.1</jsqlparser.version>

        <slf4j.version>1.6.4</slf4j.version>
        <jstl.version>1.2</jstl.version>
        <servlet-api.version>2.5</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
        <joda-time.version>2.5</joda-time.version>

        <commons-lang3.version>3.3.2</commons-lang3.version>
        <commons-io.version>2.4</commons-io.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>

        <jackson.version>2.4.2</jackson.version>
        <httpclient.version>4.3.5</httpclient.version>
        <jedis.version>2.6.2</jedis.version>
    </properties>
    <dependencies>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>${mybatis.paginator.version}</version>
        </dependency>


        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- 通用Mapper -->
        <dependency>
            <groupId>com.github.abel533</groupId>
            <artifactId>mapper</artifactId>
            <version>${mapper.version}</version>
        </dependency>

        <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>${jsqlparser.version}</version>
        </dependency>

        <!-- 连接池 -->
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>${bonecp-spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <!-- Jackson Json处理工具包 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.1</version>
        </dependency>

        <!-- 消息队列 -->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
               <groupId>org.springframework.amqp</groupId>
               <artifactId>spring-rabbit</artifactId>
               <version>1.4.0.RELEASE</version>
        </dependency>

        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp-api.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- 时间操作组件 -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda-time.version}</version>
        </dependency>

        <!-- Apache工具组件 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>

        <!-- 文件上传组件 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons-fileupload.version}</version>
        </dependency>

        <!-- jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>

        <!--添加spring-datajar包  -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>

        <!-- 字符加密、解密 -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>

        <!-- 数据校验 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>
    </dependencies>

​ 后续对大型项目所应用的技术一一深入,欢迎讨论,不足之处还请提出,谢谢。

发布了76 篇原创文章 · 获赞 122 · 访问量 29万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览