java 单机接口限流处理方案
283
2022-09-20
Java 整合模板彻底解决ssm配置难题
Spring+SpringMVC+Mybatis
环境配置
IDEA
mysql 5.7
Tomcat 8.5
Maven 3.6
创建数据库
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`bookID` INT(10) NOT NULSdCTTTcL 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=utf8
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'linux',5,'从进门到进牢');
编写数据库对应的实体类com.longdi.pojo.Books
导入lombok在pom.xml中
package com.longdi.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author: 龍弟
* @description
* @date: 2021/9/27 20:22
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
导入相关的pom依赖
资源过滤设置
Mybatis层编写
创建mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
编写Dao层的Mapper接口
package com.longdi.dao;
import com.longdi.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author: 龍弟
* @description
* @date: 2021/9/27 20:32
*/
public interface BookMapper {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(@Param("bookId") int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(@Param("bookId")int id);
//查询全部的书
List
Books queryBookByName(@Param("bookName")String bookName);
}
编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
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
where bookID=#{bookId}
select * from ssmbuild.books
select * from ssmbuild.books where bookName=#{bookName}
编写Service层的接口
package com.longdi.service;
import com.longdi.pojo.Books;
import java.util.List;
/**
* @author: 龍弟
* @description
* @date: 2021/9/27 21:03
*/
public interface BookService {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById( int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(int id);
//查询全部的书
List
Books queryBookByName(String bookName);
}
Service层的实现类
package com.longdi.service;
import com.longdi.dao.BookMapper;
import com.longdi.pojo.Books;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author: 龍弟
* @description
* @date: 2021/9/27 21:05
*/
public class BookServiceImpl implements BookService{
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
public int updateBook(Books books) {
System.out.println("BookServiceImpl:updateBook=>"+books);
return bookMapper.updateBook(books);
}
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
public List
return bookMapper.queryAllBook();
}
public Books queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
}
Spring整合Mybatis spring-dao.xml
以下为spring层
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
https://springframework.org/schema/context/spring-context.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
https://springframework.org/schema/context/spring-context.xsd">
spring-service.xml文件
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd">
以下为SpringMVC层
配置web.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
org.springframework.web.filter.CharacterEncodingFilter
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
org.springframework.web.filter.CharacterEncodingFilter
spring-mvc.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
https://springframework.org/schema/mvc/spring-mvc.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
https://springframework.org/schema/mvc/spring-mvc.xsd">
Spring配置整合文件,创建applicationContext.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd">
以上为SSM的环境配置,下面编写Controller层和视图层,实现书籍的CRUD功能
controller层 书籍的增删改查功能
import java.util.ArrayList;
import java.util.List;
/**
* @author: 龍弟
* @description
* @date: 2021/9/27 22:47
*/
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
@RequestMapping("/allBook")
public String list(Model model) {
List
model.addAttribute("list", list);
return "allBook";
}
//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPaper(){
return "addBook";
}
//添加书籍的请求
@RequestMapping("/addBook")
public String addBook(Books books){
System.out.println("addBook=>"+books);
bookService.addBook(books);
return "redirect:/book/allBook";
}
//跳转到修改页面
@RequestMapping("toUpdateBook")
public String toUpdatePaper(int id,Model model){
Books books = bookService.queryBookById(id);
model.addAttribute("book",books);
return "updateBook";
}
//修改书籍
@RequestMapping("updateBook")
public String updateBook(Books books){
System.out.println("updateBook=>"+books);
int i=bookService.updateBook(books);
if(i>0){
System.out.println("添加books成功"+books);
}
return "redirect:/book/allBook";
}
//删除书籍
@RequestMapping("/deleteBook/{bookId}")
public String deleBook(@PathVariable("bookId") int id){
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
//查询书籍
@RequestMapping("/queryBook")
public String queryBook(String queryBookName,Model model){
Books books=bookService.queryBookByName(queryBookName);
System.err.println("queryBook=>"+books);
List
list.add(books);
if(books==null){
list=bookService.queryAllBook();
model.addAttribute("error","未查到");
}
model.addAttribute("list",list);
return "allBook";
}
}
编写首页index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
a{
text-decoration: none;
color:black;
font-size:18px;
}
h3{
width:180px;
height:38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~