关于搭建ssm项目的一些总结

搭建ssm项目的时候我们需要分层设计:service,mapper,pojo,controller;

上面几个层的作用是:

1.pojo是一个数据库的实体类对象,能数据库中的数据实例化为一个pojo类的对象;

2.mapper层是一个编写操作增删改查interface和数据库语句的xml文件的地方:实质上的作用就是操作数据库。

3.service里面实际编写的业务和mapper层一致只是service层,因为service层拥有了一个mapper层的一个对象实际上调用的也是mapper层的方法,本质上mapper和service层的方法名称一样,只是调用service的某一个方法,实际上是在调用mapper罢了。

但是service层的主要作用是和controller层进行交互做一个传递的作用。

4.controller层的主要作用就是和前端的代码进行交互,俗称写接口,实际上就是在controller层增加一个功能然后逐级传递下去给后台再把数据拿到交给前台显示。

聊到配置文件:

一般需要配置的就可分为几层:数据库层,一般用交付给spring进行数据库的管理;

数据库层(dao层/service层)

在学习mybatis的时候数据库的配置本身是由mybatis-config这一个文件进行配置的,但是整合了ssm框架之后配置数据库的任务就可以交给spring来处理,mybatis-config就可以来做一些简单的

配置,比如

<configuration>
    <!--绑定文件信息db.properties-->
    <properties>
        <property name="database" value="database.properties"/>
    </properties>
    <!--启动日志信息,能显示数据库的连接,等等其他信息-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <!--注解起别名,默认pojo下面的类都是以类名的小写作为别名-->
    <typeAliases>
        <package name="com.hzk.pojo"/>
    </typeAliases>
    <!--绑定mapper的实现类(映射xml配置文件)-->
    <mappers>
        <mapper resource="com/hzk/Dao/mapper.xml"/>
    </mappers>
</configuration>

***值得一提的时绑定mapper.xml配置文件的作用,应该是mybatis绑定了这样一个配置文件就能实现增删改查的操作*****

但是真正实现mapper注入容器的还是后面动态扫描的代码。

然而剩下的一些本该由mybatis去配置的一些东西就全部交给spring去完成。因此编写一个

applicationContext-mybatis文件去写剩下的数据库配置文件:

1.绑定database.properties文件获得jdbc-diver和url username password;

2.配置数据库连接池:c3p0,druid(上面那些参数就能够直接用来配置)(c3p0还有一些私有的属性需要配置)

3.配置操作数据库的对象sqlsessionfactory。将这些东西都交给spring管理。(配置接口扫描包扫描mapper)

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--关联properties

    1.先跟properties文件关联获得数据库的driver和用户名,密码
    2.配置数据库连接池druid,c3p0
    3.配置操作数据库的对象sqlsessionfactorybean
    数据库配置文件-->
    <context:property-placeholder location="classpath:properties/*.properties"/>
    <!--配置数据库连接池-->
    <!--数据库连接池有很多,c3p0自动化操作
    ,dbcp半自动化操作不能自动连接
    ,druid:Springboot专用-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--德鲁伊连接池无需配置driver会自动的根据url配置driver   -->
       <!-- <property name="driverClass" value="${jdbc.driver}"/>-->
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--sqlsessionFactory-->
    <!-- 3.配置SqlSessionFactory对象 --><!--这种方法就不用像temple一样需要手写set方法-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    </bean>
    
    <!--配置mapper接口扫描包,动态实现dao接口注入容器,其实只是把他们的实现类交给spring管理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.hzk.mapper"/>
    </bean>
</beans>

注:值得一提的是配置接口扫描包的那一部分:

最后一个操作其实是分成两部:

1.配置操作数据库的对象,2.把这个对象和需要操作的接口交给spring去扫描和管理只是个人理解。不能保证正确。

配置sqlsessionfactory的时候分成两部分:1.配置数据源 2.关联mybatis-config的配置文件;

配置mapper接口扫描包的时候把这一个sqlsessionfactory对象和基本包传递给spring

****猜测让他通过sqlsessionfactory去拿到一个sqlsession对象调用getmapper方法(拿到mapper对象调用接口中的方法,默认调用mapper.xml这个文件(因为它是一个实现类))有待考证******

service层:

每一层都需要关联到该层所在的包,这一层的是配置事务和事务通知的,

为了保证数据的一致性原子性隔离性持久性 ,我们必须配置事务管理器

我们可以选择不同的事务管理器并在xml文件中配置你所需要的一个。

我们需要在该层配置文件中做的是:

1.扫描service包/使用aop织入的方法对事务进行绑定service(相当于扫了一遍service包)

2.配置你的事务管理器

3.可以实现aop织入service层             

<?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 https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描包-->
    
    <!--为了保证数据的一致性原子性隔离性持久性 ,我们必须配置事务管理器
    但是spring中的事务管理有很多选择jdbc就是其中一个
    -->
    <!--配置数据管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--使用了aop对事务通知进行织入service包下的所有类所有方法-->
    <aop:config>
        <aop:pointcut expression="execution(* com.hzk.service.service.*.*(..))" id="pointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

mvc层(controller层):

这一层是显示的作用和需要配置两个东西,一个是web.xml文件。第二个是springmv的配置文件。

webxml我们就配置一个请求分发,dispatcherservlet 还有乱码过滤和session。

mvc我们就配置静态资源过滤 ,注解驱动,还有视图解析器,扫描controller的包

1.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--DispatcherServlet-->
    <!--这个东西是spring用来做请求分发的-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <!--和tomcat一起启动-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--encodingFilter--><!--编码格式和乱码过滤-->
    <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>

    <!--Session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>

2.

<?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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解驱动 -->
    <mvc:annotation-driven />
    <!-- 2.静态资源默认servlet配置-->
    <mvc:default-servlet-handler/>

    <!-- 3.配置jsp 显示ViewResolver视图解析器扫描到当前的web-INF页面 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <!--默认访问webinf下面的jsp包的jsp页面-->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.hzk.controller"/>

</beans>

最后这个开启注解 ,配置静态资源过滤,还有视图解析器。最后绑定controller层。

controller层其实是和前端交互的一个页面。也是部署完后/url去访问和寻找的媒介。

配合的mybatis,spring,springmvc

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值