SSM Java project

Fri, Apr 16, 2021 2-minute read

key point

  • Pagination

  • Front-End jquery Verification

  • Back-End JSR303 Verification

  • Ajax

  • REST URI(GET, POST, PUT, DELETE)

  • SSM Framework(SpringMVC+Spring+MyBatis)

  • MySQL

  • Bootstrap

  • Maven

  • pagehelper

  • Reverse Engineering ByBatis Generator


环境配置

create maven project: war

import dependencies : spring-webmvc, spring-jdbc, spring-aspects, mybatis, mybatis-spring, c3p0, mysql-connector-java, jstl, servlet-api

tomcat 中已有 jsp 和 servlet-api,servlet-api 用的 scope 是 provided

import bootstrap

src/webapp/static/bootstrap

image

ssm

  • jquery.min.js
  • bootstrap.min.css
  • bootstrap.min.js

web.xml

src/webapp/WEB-INF/web.xml

1. 启动 Spring 容器

<!-- 1. 启动 eeded for contextloaderListener -->
<context-param>
    <param-name>comtextConfigLocation</param-name>
    <param-value>classpath:applicationContexr.xml</param-value>
</context-param>

<!-- Bootstraps the root web application context before -->
<listener>
    <listener-class> org.springframework.web.context.ContextLoaderListener<listener>
</listener>

<!-- 2. springmvc 前端控制器,拦截所有请求 -->
<listener>
    <listener-class> org.springframework.web.context.conte<listener>
</listener>

<servlet>
    <servlet-name> dispatcherServlet</servlet-name>
    <servlet-class>....DispatcherServlet </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name> dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器 -->
<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>....CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceRequestencoding</param-name>
        <param-value>true</param-value>
    </init-param>
     <init-param>
        <param-name>forceResponsecoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name> CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 使用 rest风格的url,将页面普通的psot请求转为指定的delete和put请求 -->
<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>....HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name> HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

2. 配置 springMVC

src/webapp/WEB-INF/dispatcherServlet-servlet.xml

use-default-filter=“true” 不写就会扫描所有的

<!-- springMVC配置文件, 包含网站跳转逻辑的控制,配置-->
<context:component-scan base-package="com.demo.crud" use-default-filter="true">
<!-- 只扫描控制器 -->
    <context:include-filter type="annotation" expression="..Controller">
</context:component-scan>

<!-- 配置视图解析器,方便页面返回 -->
<bean class="...view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

<!-- 两个标准配置 -->
<!-- 将springmvc不能处理的请求交给tomcat -->
<mvc:default-servlet-handler/>
<mvc:annotation-driver/>

3. Spring 配置文件

src/main/resouces/applicationContexr.xml


<context:component-scan base-package="com.demo">
    <!-- 除了controller什么都扫 -->
    <context:exclude-filter type="annotation" expression="..Controller">
</context:component-scan>
<!-- 主要配置和业务逻辑有关的 -->
<!-- 数据源,事务控制, -->
<context:property-placeholder location="classpath:dbconfig.properties"/>

<bean id="pooledDataSource" class="ComboPooledDataSource">
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    <property name="driverClass" value="${jdbc.driverClass}"></property>
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

<!-- 配置和MyBatis整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis全局配置文件的位置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <property name="dataSource" ref="pooledDataSource"></property>
     <!-- 指定mybatis mapper文件的位置 放进 resouces下面的mapper文件夹-->
    <property name="mapperLocation" value="classpath:mapper/*.xml"></property>
</bean>

<!-- 配置扫描器, 将MyBatis接口的实现加入到ioc容器中 -->
<bean  class="org.mybatis.spring..mapper.MapperScannerConfiguration">
<!-- 扫描所有dao接口的实现, 加入到ioc容器中 -->
    <property name="basePackage" value="com.demo.crud"></property>
</bean>

<!-- 事务控制的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DatasourceTransactionManager">
<!-- 控制数据源 -->
    <property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!-- 开启基于注解的事务,使用 xml 配置的事务,(必要主要的都是使用配置) -->
<aop:config>
    <!-- 切入点表达式 -->
    <aop:pointcut expression="execution(* com.demo.cr7ud.service..*(..))" id="txPoint"/>
    <!-- 配置事务增强 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config> 
<!-- 配置事务增强 , 事务如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 所有方法都是事务方法 -->
        <tx:method name="*"/>
         <!-- 以get开始的所有方法 -->
        <tx:method name="get*" read-only="true"/>
    </tx:attributes>
</tx:advice>

com.demo.cr7ud.service..*()下面的所有类方法,(..)代表参数任意多*

4. dbconfig.properties

src/main/resouces/dbconfig.properties

jdbc.jdbcUrl=

5. mybatis-config.xml

<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value=""true/>
        <typeAliases>
            <package name="com.demo.crud.bean">
        </typeAliases>
    </setting>
</configuration>

sql 表格

mybatis 逆向工程生成对应的 bean 以及 mapper

mybatis generator jar 包

创建一个 mbg.xml 文件