自写demo配(ssm)

由于楼主前段时间有些事情 没有能在java事业中继续前进冲锋 所以错失了很多的机会 这是楼主自学 的demo ssm的

建项目  首先要做到的就是有一个基本的构思 分层 dao层 控制层 view层 要有用什么表示 要构建接口  分几层 这些都要清楚

我这次是写一个小demo所以 分层不是太明显

分层结构是

controller  service(biz)dao(mapper) 其次就是实体类entity

然后我们说一下项目接下来要做什么

项目接下来是要引入jar包 jar引入现在一般都是配置的maven 配置pom.xml 我是下载的  

项目初始大概是需要这些jar包的

这是前期的jar包 下载地址如下

https://download.csdn.net/download/fuyonghui123/10388670

项目的下一步就是配置web.xml 

首先我们要知道 项目启动的时候 是会首先加载web.xml的

所以 我们的spring配置文件就要在web.xml里面引入

一般的引入方式 是

<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext.xml</param-value>  

    </context-param> 

 <!--配置springMVC自带的乱码过滤器  -->  
    <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <!--初始化字符编码为utf-8  -->  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>utf-8</param-value>  
        </init-param>  
        <!-- 强制将请求的编码格式设置为utf-8 -->  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <!--过滤器映射  -->  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  
    <!-- 加载监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
  
    <!--加载springMVC的核心控制器  -->  
    <servlet>  
        <servlet-name>springMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <!-- 初始化servlet配置文件 -->  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:springMVC-servlet.xml</param-value>  
        </init-param>  
        <!-- 设置同tomcat一起加载 -->  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <!--配置servlet映射  -->  
    <servlet-mapping>  
        <servlet-name>springMVC</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  

这些就是一般写项目所需要配置的东西 

web.xml这个时候会通过tomcat把Spring的配置文件加载进来

这个时候 你就要加载Spring配置文件了

我们也知道Spring是容器管理 所以数据的配置是要写在Spring配置中的

Spring的功能是IOc和Aop这些也是要配置的 所以 他的配置文件就是

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 扫描业务层 -->
    <context:component-scan base-package="com.mengfei.biz"/>


    <!--配置c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--基本配置-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo"/>
        <property name="user" value="root"/>
        <property name="password" value="1234"/>
        <!--高级配置-->
        <property name="maxPoolSize" value="30"/> <!--连接池的最大连接数,默认: 15,建议:对于有较大DB规模,最大连接不要设置过大,避免本地维护的db太大-->
        <property name="minPoolSize" value="3"/><!--c3p0连接池中保留的最小连接数,默认:3,建议:可考虑该值的设置和初始化连接保持一致-->
        <property name="initialPoolSize" value="3"/><!--初始化时获取3个连接,取值应在minPoolSize与maxPoolSize之间。默认: 3,建议:3,对于db规模特别大的情况下可考虑设置为1个,避免启动时间过长-->
        <property name="maxIdleTime" value="1800"/><!--连接的最大空闲时间,3分钟内未使用则会断开连接,若为0则永远不会断开。默认: 0 毫秒,建议:该值不要太小,避免频繁的建立连接关闭连接。也不要太大,导致一直无法关闭-->
        <property name="maxStatements" value="900"/> <!--连接池为数据源缓存的PreparedStatement的总数,就是可以执行SQL语句的对象的个数,默认:0,建议:这个数量应该根据应用中平均连接数乘以每个连接的平均PreparedStatement来计算-->
        <property name="maxStatementsPerConnection" value="30"/><!--连接池为数据源单个连接缓存的PreparedStatement数,默认:0,建议:由于PreparedStatement属于单个Connection,这个配置比maxStatements更有意义-->
        <property name="idleConnectionTestPeriod" value="90"/><!--在进行心跳检测的时候,会对所有的空闲连接进行心跳检测。如果发现总连接小于最小连接数,则会创建连接,保持最小的连接数,该值需要结合数据库的wait_timeout,interactive_timeout值进行设置
        假如数据库为120s,则心跳检测时间在120s以内越大越好。如果太小,心跳检测时间会比较频繁。建议:设置为90s-->
        <property name="checkoutTimeout" value="5000"/><!--如果没有连接可用或者连接全部占用时,需要等待的时间(单位:ms),默认为:一直等待-->
    </bean>


    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引用数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 扫描类的映射文件 -->
        <property name="mapperLocations" value="classpath:com/mengfei/mapper/*.xml"/>
    </bean>


    <!-- 配置映射接口,spring会自动查找下面的接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mengfei.mapper"/>
    </bean>


    <!-- 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>


    <!-- 配置事务通知 -->
    <tx:advice transaction-manager="txManager" id="txAdvice">
        <tx:attributes>
            <tx:method name="register*"/>
            <tx:method name="save*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="login*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>


    <!-- 配置aop增强处理 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.mengfei.biz.*.*(..))"/>

    </aop:config>

</beans>

下面就是Mvc的配置了 这个配置 有的项目会把它加到Spring中启动 有的是在web里面启动 我是在web 里面

SpringMVc就是以前的action  所以 他们的配置就是跳转的配置

<?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/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 扫描控制器 -->
    <context:component-scan base-package="com.mengfei.*">  
    </context:component-scan>  


    <!-- 使用默认处理器处理静态资源 -->
    <mvc:default-servlet-handler/>


    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>


    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

配置好文件 那下面就是写代码了

我们先理清思路 

我们前端需要什么 这个时候我们就去配置什么

比如一个简单 的登录 

那么我们就先写login.jsp

前端请求找适配器 MVc配置文件 这个时候会把它分配到controller   不同于sturts2 SpringMVc是直接交给一个一个方法 

然后controller  会交给service  service 找实体 找dao层 

dao层mapper 找其配置文件****.xml

然后操作数据库

理清流程那代码就好写了

这是代码的链接


https://download.csdn.net/download/fuyonghui123/10388635



猜你喜欢

转载自blog.csdn.net/fuyonghui123/article/details/80177143