110_SSM框架
需求分析->功能设计->数据库设计
环境要求
环境
IDEA
MySQL 5.7.19
Tomcat 9
Maven 3.6
要求
熟练掌握MySQL数据库
熟练掌握Spring,JavaWeb及MyBatis知识
简单的前端知识
数据库环境
CREATE DATABASE `ssm`CHARACTER SET utf8 COLLATE utf8_general_ci; USE `ssm`; CREATE TABLE `ssm`.`books` ( `book_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id', `book_name` VARCHAR(100) NOT NULL COMMENT '书名', `book_count` INT(10) NOT NULL COMMENT '数量', `book_detail` VARCHAR(1000) NOT NULL COMMENT '描述', PRIMARY KEY (`book_id`) ) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci; INSERT INTO `ssm`.`books` (`book_name`, `book_count`, `book_detail`) VALUES ('Java', '1', '从入门到放弃'); INSERT INTO `ssm`.`books` (`book_name`, `book_count`, `book_detail`) VALUES ('MySQL', '11', '从删库到跑路'); INSERT INTO `ssm`.`books` (`book_name`, `book_count`, `book_detail`) VALUES ('Linux', '5', 'j'); SELECT * FROM books LIMIT 0, 1000;
基本环境搭建
创建maven项目
pom.xml添加依赖,添加资源导出
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.qingssm1.0-SNAPSHOTjunitjunit4.13.2org.projectlomboklombok1.18.20mysqlmysql-connector-java5.1.49com.mchangec3p00.9.5.5javax.servletservlet-api2.5javax.servlet.jspjsp-api2.2javax.servletjstl1.2org.mybatismybatis3.5.7org.mybatismybatis-spring2.0.6org.springframeworkspring-webmvc5.3.9org.springframeworkspring-jdbc5.3.9org.aspectjaspectjweaver1.9.7runtimesrc/main/resources**/*.properties**/*.xmltruesrc/main/java**/*.properties**/*.xmltrue
idea连接数据库
提交项目到Git
git@gitee.com:wl3pbzhyq/ssm.git
创建基础包
创建配置文件
db.properties
driver=com.mysql.jdbc.Driver # 如果使用的是MySQL8.0+,增加一个时区的配置:&serverTimezone=Asia/Shanghai url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8 username=root password=123456
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
创建实体类
package com.qing.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Books { private int bookId; private String bookName; private int bookCount; private String bookDetail; }
创建Mapper接口和Mapper.xml
package com.qing.dao; import com.qing.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BooksMapper { /** * 新增书 * @param books * @return */ int add(Books books); /** * 根据Id删除书 * @param bookId * @return */ int deleteById(@Param("bookId") int bookId); /** * 修改书 * @param books * @return */ int update(Books books); /** * 根据Id获取书 * @param bookId * @return */ Books getBooksById(@Param("bookId") int bookId); /** * 获取全部书 * @return */ ListlistBooks(); }
<?xml version="1.0" encoding="UTF-8" ?>insert into ssm.books(book_name, book_count, book_detail) values (#{bookName},#{bookCount},#{bookDetail})delete from ssm.books where book_id = #{bookId}update ssm.books set book_name=#{bookName},book_count=#{bookCount},book_detail=#{bookDetail} where book_id=#{bookId} select * from ssm.books where book_id=#{bookId} select * from ssm.books
注册Mapper到mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
创建Service接口和实现类
package com.qing.service; import com.qing.pojo.Books; import java.util.List; public interface BooksService { /** * 新增书 * @param books * @return */ int add(Books books); /** * 根据Id删除书 * @param bookId * @return */ int deleteById(int bookId); /** * 修改书 * @param books * @return */ int update(Books books); /** * 根据Id获取书 * @param bookId * @return */ Books getBooksById(int bookId); /** * 获取全部书 * @return */ ListlistBooks(); }
package com.qing.service; import com.qing.dao.BooksMapper; import com.qing.pojo.Books; import java.util.List; public class BooksServiceImpl implements BooksService { // service调dao层,组合Dao private BooksMapper booksMapper; public void setBooksMapper(BooksMapper booksMapper) { this.booksMapper = booksMapper; } public int add(Books books) { return booksMapper.add(books); } public int deleteById(int bookId) { return booksMapper.deleteById(bookId); } public int update(Books books) { return booksMapper.update(books); } public Books getBooksById(int bookId) { return booksMapper.getBooksById(bookId); } public ListlistBooks() { return booksMapper.listBooks(); } }
Spring配置文件
创建配置文件 spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
创建配置文件 spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
创建配置文件 spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
applicationContext.xml导入spring-dao.xml spring-service.xml spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
项目添加web框架支持
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>spingmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:applicationContext.xml1spingmvc/encodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingutf-8encodingFilter/*15
创建Controller
package com.qing.controller; import com.qing.pojo.Books; import com.qing.service.BooksService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/books") public class BooksController { @Autowired @Qualifier("BookServiceImpl") private BooksService booksService; @RequestMapping("/listBooks") public String listBooks(Model model) { Listlist = booksService.listBooks(); model.addAttribute("list",list); return "listBooks"; } }
创建首页和书库页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>进入书库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %>书库书籍编号书籍名称书籍数量书籍描述${book.bookId}${book.bookName}${book.bookCount}${book.bookDetail}
Artifacts中WEB-INF下创建lib,并选择jar
配置Tomcat,并测试
赞 (0)