欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

简单编写基于SSM框架的数据库CRUDweb页面

时间:2023-06-12
开发工具

idea,navicate,tomcat

技术支持

Maven,SSM(spring,springMVC,Mybatis),bootstrap前端框架,Mysql数据库

mysql设置

使用navicat建立数据库并创建一个数据表,表结构如下:

基础环境搭建 在idea中创建一个Maven项目,使目录结构如下,后续目录将会用到
引入项目依赖的jar包

org.springframework spring-webmvc 5.3.15 org.springframework spring-jdbc 5.3.15 org.springframework spring-aspects 5.3.15 org.mybatis mybatis 3.5.7 org.mybatis mybatis-spring 2.0.6 c3p0 c3p0 0.9.1.2 mysql mysql-connector-java 8.0.27 junit junit 4.11 test javax.servlet javax.servlet-api 4.0.0-b01 provided javax.servlet jsp-api 2.0 provided javax.servlet jstl 1.2

maven资源过滤配置

src/main/java ***.xml false src/main/resources ***.xml false

引入bootstrap前端框架
因为所有的bootstrap框架都依赖于jQuery,所以引入bootstrap之前都必须引入jQuery 官网下载bootstrap和jQuery
bootstrap:https://www.bootcss.com/
jQuery:https://jquery.com/idea中webapp目录下新建一个static静态目录,新建js和bootstrap目录,分别引入jQuery和bootstrap,目录结构如下
创建一个jsp文件,引入jQuery和bootstrap,结果如图
配置文件的编写
在resource目录下建立applicationContext.xml和mybatis-config.xml配置文件

基本配置如下:
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

连接数据库–Mybatis层编写 resources目录下建立database.properties文件,内容如下:

jdbc.driver=com.mysql.jdbc.Driver#mysql 8.0 以上 添加时区配置 &serverTimezone=Asia/Shanghaijdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai jdbc.username=rootjdbc.password=root

idea关联数据库编写mybatis核心配置文件


4、编写实体类
pojo目录下创建一个Books实体类

public class Books { private int bookID; private String bookName; private String bookCounts; private String detail; public Books() { } public Books(int bookID, String bookName, String bookCounts, String detail) { this.bookID = bookID; this.bookName = bookName; this.bookCounts = bookCounts; this.detail = detail; } public int getBookID() { return bookID; } public void setBookID(int bookID) { this.bookID = bookID; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookCounts() { return bookCounts; } public void setBookCounts(String bookCounts) { this.bookCounts = bookCounts; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } @Override public String toString() { return "Books{" + "bookID=" + bookID + ", bookName='" + bookName + ''' + ", bookCounts='" + bookCounts + ''' + ", detail='" + detail + ''' + '}'; }}

编写Mapper文件夹

BookMapper:

public interface BookMapper { //插入书本 int addBook(Books books); //删除书本 int delbookByID( @Param("bookID") int id); //更新书本 int updateBook(Books books); //查询书本 Books queryBookByID( @Param("bookID") int id); //查询全部书本 List queryAllBooks(); }

BookMapper.xml

<?xml version="1.0" encoding="UTF-8" ?> insert into ssmbuild.books (bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail}); delete from ssmbuild.books where bookID = #{bookID}; update ssmbuild.books set bookName =#{bookName},bookCounts = #{bookCounts},detail=#{detail} where bookID=#{bookID}; select * from ssmbuild.books;

编写Service层接口和实现类

BookService:

public interface BookService { //插入书本 int addBook(Books books); //删除书本 int delbookByID( int id); //更新书本 int updateBook(Books books); //查询书本 Books queryBookByID( int id); //查询全部书本 List queryAllBooks();}

BookServiceImpl

public class BookServiceImpl implements BookService{ //service层调用mapper层 private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int addBook(Books books) { return bookMapper.addBook(books); } @Override public int delbookByID(int id) { return bookMapper.delbookByID(id); } @Override public int updateBook(Books books) { return bookMapper.updateBook(books); } @Override public Books queryBookByID(int id) { return bookMapper.queryBookByID(id); } @Override public List queryAllBooks() { return bookMapper.queryAllBooks(); }}

Spring层配置文件编写 配置Spring整合MyBatis,spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>

配置spring整合service层,spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>

springMVC层配置文件的编写 web.xml的配置

<?xml version="1.0" encoding="UTF-8"?> DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 DispatcherServlet / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter /* 15

配置spring整合springmvc,spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>

至此,idea中关于SSM的配置已经全部完成,接下来是业务处理控制以及前台代码的编写

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。