SSM书籍管理(环境搭建)

整合SSM:Spring+SpringMVC+Mybatis

环境要求:IDEA、MySQL5+、Tomcat9+、Maven3+

数据库搭建

数据库准备以下数据用于后续实验:创建一个ssmbuild数据库,表books,该表有4个字段,并且插入3条数据用于后续。

CREATE DATABASE `ssmbuild`use `ssmbuild`drop TABLE IF exists `books`CREATE TABLE `books`(`bookID` int(10) not null auto_increment comment'书id',`bookName` varchar(100) not null comment'书名',`bookCounts` int(11) not null comment'数量',`detail` varchar(200) not null comment'描述',key `bookID`(`bookID`)
)engine=innodb default charset=utf8insert into `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'java',1,'Java介绍'),
(2,'C',10,'C语言介绍'),
(3,'数据库',8,'数据库介绍')

项目准备

pom.xml引入依赖:junit、数据库驱动、连接池、servlet、jsp、mybatis、mybatis-spring、spring、lombok、

<dependencies><!-- Mysql连接依赖 数据库驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency><!--数据库连接池 可以用c3p0或dbcp--><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!--servlet-jsp 这三个一般一起使用--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--mybatis 以下两个一般一起使用--><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!--Spring--><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.20</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.20</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency>
</dependencies><!--在build中配置resources, 来防止我们资源导出失败问题-->
<build>
<resources><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource>
</resources>
</build>
</project>

连接数据库

建立包:

建立mybatis-config.xml文件,导入头文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration></configuration>

建立applicationContext.xml文件,导入Spring的头文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

建立数据库配置文件database.properties,注意ssmbuild是数据库名

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username=root
password=123456

配置Mybatis

1、编写实体类Books,该类中的属性对应数据库表book中的字段:

/*Date注解可以自动生成set与get方法与无参,AllArgsConstructor注解注入有参,NoArgsConstructor无参*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {private int bookID;private String bookName;private int bookCounts;private String detail;
}

2、编写接口BookMapper。

public interface BookMapper {//增加一本书int addBook(Books books);//删除一本书int deteleBookById(@Param("bookID") int id);//更新一本书int updateBook(Books books);//根据id查询一本书Books queryBookById(@Param("bookID") int id);//查询全部的书List<Books> queryAllBook();
}

3、编写接口实现文件,BookMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.serenity.dao.BookMapper"><!--每个mapper都需要绑定一个接口--><insert id="addBook" parameterType="Books">insert into ssmbuild.books(bookName,bookCounts,detail)values (#{bookName},#{bookCounts},#{detail})</insert><delete id="deleteBookById" parameterType="_int">delete from ssmbuild.books where bookID=#{bookID}</delete><update id="updateBook" parameterType="Books">update ssmbuild.booksset bookName=#{bookName},bookCounts=#{bookCouts},detail=#{detail}where bookID=#{bookID}</update><select id="queryBookById" resultType="Books">select * from ssmbuild.books where bookID=#{bookID}</select><select id="queryAllBook" resultType="Books">select * from ssmbuild.books</select>
</mapper>

4、将mapper文件注册到mybatis-config文件中

<!--设置标准日志输出--><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings>
<!--配置数据源,交给Spring了--><typeAliases><package name="com.serenity.pojo"/></typeAliases><mappers><mapper class="com.serenity.dao.BookMapper"/></mappers>

5、编写业务层中的Bookservice接口

public interface BookService {//增加一本书int addBook(Books books);//删除一本书int deteleBookById(int id);//更新一本书int updateBook(Books books);//根据id查询一本书Books queryBookById(int id);//查询全部的书List<Books> queryAllBook();
}

6、编写业务层的实现类BookServiceImpl

public class BookServiceImpl implements BookService {//service调dao层,所以该地需要组合dao层private BookMapper bookMapper;public void setBookMapper(BookMapper bookMapper) {this.bookMapper = bookMapper;}public int addBook(Books books) {return bookMapper.addBook(books);}public int deteleBookById(int id) {return bookMapper.deteleBookById(id);}public int updateBook(Books books) {return bookMapper.updateBook(books);}public Books queryBookById(int id) {return bookMapper.queryBookById(id);}public List<Books> queryAllBook() {return bookMapper.queryAllBook();}
}

Spring层

1、编写Spring-dao.xml文件,用于连接数据库

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--1、关联数据库文件--><context:property-placeholder location="classpath:database.properties"/><!--2、连接池datasource--><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.username}"/><property name="password" value="${jdbc.password}"/><!--c3p0连接池的私有属性--><property name="maxPoolSize" value="30"/><property name="minPoolSize" value="10"/><!--关闭连接后不自动commit--><property name="autoCommitOnClose" value="false"/><!--获取连接超时时间--><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"/><!--绑定mybatis配置文件--><property name="configLocation" value="classpath:mybatis-config.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.serenity.dao"/></bean></beans>

2、整合service层,编写spring-service.xml文件

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--1、扫描service下的包--><context:component-scan base-package="com.serenity.service"/><!--2、将所有业务内注入到Spring,可以通过配置或注解实现,此处所有配置--><bean id="BookServiceImpl" class="com.serenity.service.BookServiceImpl"><property name="bookMapper" ref="bookMapper"/></bean>
<!--3、声明式事务--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入数据源--><property name="dataSource" ref="dataSource"/></bean>
</beans>

注意各配置文件关联起来。

SpringMVC层

1、增加web的支持

2、编写web.xml文件内容

<!--DispatcherServlet--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--乱码过滤--><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-->

3、编写spring-mvc.xml配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--1、注解驱动--><mvc:annotation-driven/><!--2、静态资源过滤--><mvc:default-servlet-handler/><!--3、扫描包:controller--><context:component-scan base-package="com.serenity.controller"/><!--4、视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp"/><property name="suffix" value=".jsp"/></bean>
</beans>

配置结束:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/77709.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

API文档生成与测试工具推荐

在API开发过程中&#xff0c;文档的编写和维护是一项重要但繁琐的工作。为了提高效率&#xff0c;许多开发者会选择使用API文档自动生成工具或具备API文档生成功能的API门户产品。选择能导入API文档的工具生成测试脚本, 本文将全面梳理市面上符合OpenAPI 3.0规范的文档生成工具…

linux修改环境变量

添加环境变量注意事项。 vim ~/.bashrc 添加环境变量时&#xff0c;需要source ~/.bashrc后才能有效。同时只对当前shell窗口有效&#xff0c;当打开另外的shell窗口时&#xff0c;需要重新source才能起效。 1.修改bashrc文件后 2.source后打开另一个shell窗口则无效&#xff…

springboot项目中,MySQL数据库转达梦数据库

前言 前段时间&#xff0c;公司要求要把某几个项目的数据库换成达梦数据库&#xff0c;说是为了国产化。我就挺无语的&#xff0c;三四年的项目了&#xff0c;现在说要换数据库。我一开始以为这个达梦数据库应该是和TIDB差不多的。 我之前做的好几个项目部署到测试服、正式服…

【Quest开发】透视环境下抠出身体并能遮挡身体上的服装

软件&#xff1a;Unity 2022.3.51f1c1、vscode、Meta XR All in One SDK V72 硬件&#xff1a;Meta Quest3 仅针对urp管线 博主搞这个主要是想做现实里的人的变身功能&#xff0c;最后效果如下 可以看到虽然身体是半透明的&#xff0c;但是裙子依旧被完全遮挡了 原理是参考…

前端安全中的XSS(跨站脚本攻击)

XSS 类型 存储型 XSS 特征&#xff1a;恶意脚本存储在服务器&#xff08;如数据库&#xff09;&#xff0c;用户访问受感染页面时触发。场景&#xff1a;用户评论、论坛帖子等持久化内容。影响范围&#xff1a;所有访问该页面的用户。 反射型 XSS 特征&#xff1a;恶意脚本通过…

(第三篇)Springcloud之Ribbon负载均衡

一、简介 1、介绍 Spring Cloud Ribbon是Netflix发布的开源项目&#xff0c;是基于Netflix Ribbon实现的一套客户端负载均衡的工具。主要功能是提供客户端的软件负载均衡算法&#xff0c;将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时&…

大模型——使用coze搭建基于DeepSeek大模型的智能体实现智能客服问答

大模型——使用coze搭建基于DeepSeek大模型的智能体实现智能客服问答 本章实验完全依托于coze在线平台,不需要本地部署任何应用。 实验介绍 1.coze介绍 扣子(coze)是新一代 AI 应用开发平台。无论你是否有编程基础,都可以在扣子上快速搭建基于大模型的各类 AI 应用,并…

【计算机视觉】目标检测:深度解析YOLOv9:下一代实时目标检测架构的创新与实战

深度解析YOLOv9&#xff1a;下一代实时目标检测架构的创新与实战 架构演进与技术创新YOLOv9的设计哲学核心创新解析1. 可编程梯度信息&#xff08;PGI&#xff09;2. 广义高效层聚合网络&#xff08;GELAN&#xff09;3. 轻量级设计 环境配置与快速开始硬件需求建议详细安装步骤…

【SpringBoot】基于MybatisPlus的博客管理系统(1)

1.准备工作 1.1数据库 -- 建表SQL create database if not exists java_blog_spring charset utf8mb4;use java_blog_spring; -- 用户表 DROP TABLE IF EXISTS java_blog_spring.user_info; CREATE TABLE java_blog_spring.user_info(id INT NOT NULL AUTO_INCREMENT,user_na…

贵族运动项目有哪些·棒球1号位

10个具有代表性的贵族运动&#xff1a; 高尔夫 马术 网球 帆船 击剑 斯诺克 冰球 私人飞机驾驶 深海潜水 马球 贵族运动通常指具有较高参与成本、历史底蕴或社交属性的运动&#xff0c;而棒球作为一项大众化团队运动&#xff0c;与典型贵族运动的结合较为罕见。从以下几个角度探…

【Tauri2】035——sql和sqlx

前言 这篇就来看看插件sql SQL | Taurihttps://tauri.app/plugin/sql/ 正文 准备 添加依赖 tauri-plugin-sql {version "2.2.0",features ["sqlite"]} features可以是mysql、sqlite、postsql 进去features看看 sqlite ["sqlx/sqlite&quo…

全链路自动化AIGC内容工厂:构建企业级智能内容生产系统

一、工业化AIGC系统架构 1.1 生产流程设计 [需求输入] → [创意生成] → [多模态生产] → [质量审核] → [多平台分发] ↑ ↓ ↑ [用户反馈] ← [效果分析] ← [数据埋点] ← [内容投放] 1.2 技术指标要求 指标 标准值 实现方案 单日产能 1,000,000 分布式推理集群 内容合规率…

是否想要一个桌面哆啦A梦的宠物

是否想拥有一个在指定时间喊你的桌面宠物呢&#xff08;手动狗头&#xff09; 如果你有更好的想法&#xff0c;欢迎提出你的想法。 是否考虑过跟开发者一对一&#xff0c;提出你的建议&#xff08;狗头&#xff09;。 https://wwxc.lanzouo.com/idKnJ2uvq11c 密码:bbkm

Unity AI-使用Ollama本地大语言模型运行框架运行本地Deepseek等模型实现聊天对话(二)

一、使用介绍 官方网页&#xff1a;Ollama官方网址 中文文档参考&#xff1a;Ollama中文文档 相关教程&#xff1a;Ollama教程 使用版本&#xff1a;Unity 2022.3.53f1c1、Ollama 0.6.2 示例模型&#xff1a;llama3.2 二、运行示例 三、使用步骤 1、创建Canvas面板 具体…

从 BERT 到 GPT:Encoder 的 “全局视野” 如何喂饱 Decoder 的 “逐词纠结”

当 Encoder 学会 “左顾右盼”&#xff1a;Decoder 如何凭 “单向记忆” 生成丝滑文本&#xff1f; 目录 当 Encoder 学会 “左顾右盼”&#xff1a;Decoder 如何凭 “单向记忆” 生成丝滑文本&#xff1f;引言一、Encoder vs Decoder&#xff1a;核心功能与基础架构对比1.1 本…

数据结构入门:详解顺序表的实现与操作

目录 1.线性表 2.顺序表 2.1概念与结构 2.2分类 2.2.1静态顺序表 2.2.2动态顺序表 3.动态顺序表的实现 3.1.SeqList.h 3.2.SeqList.c 3.2.1初始化 3.2.2销毁 3.2.3打印 3.2.4顺序表扩容 3.2.5尾部插入及尾部删除 3.2.6头部插入及头部删除 3.2.7特定位置插入…

LeetCode热题100--53.最大子数组和--中等

1. 题目 给你一个整数数组 nums &#xff0c;请你找出一个具有最大和的连续子数组&#xff08;子数组最少包含一个元素&#xff09;&#xff0c;返回其最大和。 子数组是数组中的一个连续部分。 示例 1&#xff1a; 输入&#xff1a;nums [-2,1,-3,4,-1,2,1,-5,4] 输出&…

python:练习:2

1.题目&#xff1a;统计一篇英文文章中每个单词出现的次数&#xff0c;并按照出现次数排序输出。 示例输入&#xff1a; text "Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991…

AI Agent 孵化器?开源框架CAMEL

简介 CAMEL&#xff08;Communicative Agents for Mind Exploration of Large Scale Language Model Society&#xff09;是一个开源框架&#xff0c;大语言模型多智能体框架的先驱者。旨在通过角色扮演和自主协作&#xff0c;探索大语言模型&#xff08;LLM&#xff09;在多智…

关于插值和拟合(数学建模实验课)

文章目录 1.总体评价2.具体的课堂题目 1.总体评价 学校可以开设这个数学建模实验课程&#xff0c;我本来是非常的激动地&#xff0c;但是这个最后的上课方式却让我高兴不起哦来&#xff0c;因为老师讲的这个内容非常的简单&#xff0c;而且一个上午的数学实验&#xff0c;基本…