【SSMコンプリートスモールプロジェクト】イベントレコーディングシステム

最近は大学院のレビュー企画で忙しくて、長い間ブログを書いていませんでしたが、本日は、行った2つの小さなSSMプロジェクト(基本的な追加、削除、改訂、プロジェクト構造展開)を公開します。学期の終わりに、全員が交換して共有できるようにします。

プロジェクトアドレス:https://github.com/gaoliwei1102/Event-Notes

プロジェクト構造ディレクトリ

ここに画像の説明を挿入

ここに画像の説明を挿入

プロジェクトのメインランニングのスクリーンショット

プロジェクトのメインページ

ここに画像の説明を挿入

情報一覧ページ(イベント一覧)

ここに画像の説明を挿入

大きなイベントページを追加

ここに画像の説明を挿入

大きなイベントページを変更する

ここに画像の説明を挿入

イベントを削除するには、削除ボタンをクリックするだけです。

プロジェクトのメイン構成ファイル

spring-daoデータベース永続層構成ファイル

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--1、关联数据库文件-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--2、连接池-->
    <!--
    C3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中)
    jdbc:半自动化操作,不能自动连接
    druid:
    hikari:
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${user}"/>
        <property name="password" value="${password}"/>

        <!--c3p0的私有属性-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!--关闭连接后不能自动连接commit-->
        <property name="autoCommitOnClose" value="false"/>
        <!--获取连接超时时间,10秒-->
        <property name="checkoutTimeout" value="10000"/>
        <!--当前获取连接失败的次数-->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 扫描pojo包 使用别名 -->
        <property name="typeAliasesPackage" value="com.gaoliwei.pojo"/>
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.gaoliwei.dao"/>
    </bean>

</beans>

spring-mvcレイヤー構成ファイル

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--1、注解驱动-->
    <mvc:annotation-driven/>
    <!--2、静态资源过滤器-->
    <mvc:default-servlet-handler/>


    <!-- 3、扫描包:controller -->
    <context:component-scan base-package="com.gaoliwei.controller"/>
    <!-- 4、视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

スプリングサービスビジネスレイヤー構成

<?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:tx="http://www.springframework.org/schema/tx"
       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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 扫描service包下所有使用注解的类型 -->
    <context:component-scan base-package="com.gaoliwei.service" />

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置基于注解的声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>
プロジェクトコアクラス

NotesControllerコントロールジャンプクラス

package com.gaoliwei.controller;

import com.gaoliwei.pojo.Notes;
import com.gaoliwei.service.NotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/note")
public class NotesController {
    
    
    @Autowired
    private NotesService notesService;

    @RequestMapping("/allNotes")
    public String list(Model model) {
    
    
        List<Notes> list = notesService.queryAllNotes();
        model.addAttribute("list", list);
        return "allNotes";
    }

    @RequestMapping("/toAddNotes")
    public String toAddNotes(){
    
    
        return "addNotes";
    }

    @RequestMapping("/addNotes")
    public String addNotes(Notes notes) {
    
    

        notesService.addNotes(notes);
        return "redirect:/note/allNotes";
    }

    @RequestMapping("/del/{noteId}")
    public String deleteNotes(@PathVariable("noteId") Long id) {
    
    
        notesService.deleteNotesById(id);
        return "redirect:/note/allNotes";
    }

    @RequestMapping("toUpdateNotes")
    public String toUpdateNotes(Model model, Long id) {
    
    
        model.addAttribute("notes", notesService.queryById(id));
        return "update";
    }

    @RequestMapping("/updateNotes")
    public String updatePaper(Model model, Notes notes) {
    
    
        notesService.updateNotes(notes);
        notes = notesService.queryById(notes.getNoteId());
        model.addAttribute("notes", notes);
        return "redirect:/note/allNotes";
    }
}

NotesDaoクラスマッピングファイルNotesMapper.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.gaoliwei.dao.NotesDao">
    <resultMap type="Notes" id="notesResultMap" >
        <id property="noteId" column="noteId"/>
        <result property="noteContent" column="noteContent"/>
        <result property="author" column="author"/>
        <result property="date" column="date"/>
    </resultMap>
    <insert id="addNotes" parameterType="Notes">
        INSERT INTO notes(noteContent,author,date) VALUE (#{noteContent}, #{author}, #{date})
    </insert>

    <delete id="deleteNotesById" parameterType="long">
        DELETE FROM notes WHERE noteId=#{noteId}
    </delete>

    <update id="updateNotes" parameterType="Notes">
        UPDATE notes
        SET noteContent = #{noteContent},author = #{author},date = #{date}
        WHERE  noteId = #{noteId}
    </update>

    <select id="queryById" resultType="Notes" parameterType="long">
        SELECT noteId,noteContent,author,date
        FROM notes
        WHERE noteId=#{noteId}
    </select>
    <select id="queryAllNotes" resultMap="notesResultMap">
        SELECT noteId,noteContent,author,date
        FROM notes
    </select>

</mapper>

その他のソースコードについては、私のgithubにアクセスしてソースプロジェクトをダウンロードしてください。

デモンストレーション結果

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_43479947/article/details/112132385