最近忙于考研复习规划,很久没有写过博客了,今天把期末做的两个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>
spring-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: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下载源项目