SSM之简单的CRUD

2023-06-25,,

文章目录

前言
项目介绍
项目代码介绍
数据库文件
源码介绍
代码展示
配置文件
业务逻辑代码
总结

前言

大家好呀,前面不是说最近在学习SSM么,可能学的不是那么深,不过刚刚开始,学完肯定需要先动手做点东西才行呀,所以就在前面看的一本书中的最后一章,做了一个小实践,做一个简单CRUD

项目介绍

这个项目主要是包括了下面图的功能,就是对两个功能模块进行CRUD,包括对一些业务进行修改,以及删除,包括一些对用户的信息录入和新的文章录入和发布

项目代码介绍

数据库文件

主要包括四个部分,分别是用户表、系统角色、新闻、新闻类别四个表,下面是这四个表的建表语句。

如果运行出现问题的话,那就更换下顺序,应该问题不大的。

源码介绍

我是直接拿了一本书的代码直接拿来写的,所以前端采用的是jsp和ajax来进行数据的交互,前端我也怎么仔细看,只是关注了传参的类型和传参的方式,下面这张图,应该可以概况所有的代码了

代码展示

代码挺简单的,我觉得,就按顺序来吧,我不会写前端页面,所以就抄了一本书的一个实例,里面也有静态页面的代码。

配置文件

主要包括applicationContext.xml, database.properties, mybatis.xml, spring-mvc.xml

applicationContext.xml,这些项目的配置文件就逐一展示,因为毕竟这个是整合的关键

<?xml version="1.0" encoding="UTF8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
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"> <context:property-placeholder location="classpath:database.properties"/>
<bean id = "dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name ="driverClass" value ="${jdbc.driver}"/>
<property name ="jdbcUrl" value ="${jdbc.url}"/>
<property name ="user" value ="${jdbc.user}"/>
<property name ="password" value ="${jdbc.password}"/>
<property name ="initialPoolSize" value ="${jdbc.initialSize}"/>
<property name ="maxPoolSize" value ="${jdbc.maxTotal}"/>
<property name ="minPoolSize" value ="${jdbc.maxIdle}"/>
</bean> <!--事务管理器,依赖于数据源 -->
<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="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="querry*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.DY.service.*.*(..))" />
</aop:config> <bean id = "sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean">
<property name ="dataSource" ref ="dataSource"/>
<property name ="configLocation" value ="classpath:mybatis-config.xml"/> </bean>
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value ="sqlSessionFactory" />
<property name="basePackage" value="com.DY.mapper"/>
</bean> <context:component-scan base-package="com.DY.service.impl"/> <import resource="classpath:spring-mvc.xml"/> </beans>

数据库的配置文件database.properties,主要数据库的密码方面的配置

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/news?serverTimezone=UTC
jdbc.user=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

mybatis的配置文件,主要主要是配置mapper和一些基础配置,这里我们还是设置mapperj接口的设置。

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 日志输出-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.DY.entity"/>
</typeAliases>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper resource="mapper/RoleMapper.xml"/>
<mapper resource="mapper/CategoryMapper.xml"/>
<mapper resource="mapper/NewsMapper.xml"/>
</mappers> </configuration>

mvc的配置文件,需要配置路径的拦截,编码的设置,静态资源的路径等等。

<?xml version="1.0" encoding="UTF8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <context:component-scan base-package="com.DY.controller"/> <!-- 配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 设置后缀 -->
<property name="suffix" value=".jsp" />
</bean> </beans>

再配置web.xml就可以开始写业务逻辑了。

业务逻辑代码

然后就可以写我们的业务逻辑代码,需要写mapper层,service层,controller层,分别需要一点点去写,代码:https://github.com/lyzhang30/news

代码地址

总结

代码我就不详细展示了,整个逻辑结果挺简单的,我会把代码文章的末尾处,需要的可以自取,有问题可以在下面一起分享,一起进步,加油加油加油!!!

SSM之简单的CRUD的相关教程结束。

《SSM之简单的CRUD.doc》

下载本文的Word格式文档,以方便收藏与打印。