In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "Java how to integrate templates to completely solve the ssm configuration problem", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java how to integrate templates to thoroughly solve the ssm configuration problem" this article.
Spring+SpringMVC+Mybatis
Environment configuration
IDEA
MySQL 5.7
Tomcat 8.5
Maven 3.6
Create a database
CREATE DATABASE `ssmbuild`; USE `ssmbuild`; DROP TABLE IF EXISTS `books` CREATE TABLE `books` (`bookID` INT (10) NOT NULL AUTO_INCREMENT COMMENT 'book id', `bookName` NOT NULL COMMENT' title', `bookCounts` INT (11) NOT NULL COMMENT 'quantity', `detail`VARCHAR 'NOT NULL COMMENT' description', KEY `bookID` (`bookID`)) ENGINE=INNODB DEFAULT CHARSET=utf8INSERT INTO `books` (`bookID`, `bookName`, `bookCounts`, `detail`) VALUES From entering the door to going to prison
Write the entity class com.longdi.pojo.Books corresponding to the database
Import lombok in pom.xml
Package com.longdi.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;/** * @ author: brother * @ description * @ date: 20:22 on 2021-9-27 * / @ Data@AllArgsConstructor@NoArgsConstructorpublic class Books {private int bookID; private String bookName; private int bookCounts; private String detail;}
Import related pom dependencies
Junit junit 4.12 mysql mysql-connector-java 5.1.47 com.mchange c3p0 0.9.5.2 javax.servlet servlet-api 2.5 javax.servlet.jsp jsp-api 2.2 javax.servlet jstl 1.2 org.mybatis mybatis 3.5.2 Org.mybatis mybatis-spring 2.0.2 org.aspectj aspectjweaver 1.8.4 org.springframework spring-webmvc 5.1.9.RELEASE org.springframework spring-jdbc 5.1.9.RELEASE org.projectlombok lombok 1.18.16 compile
Resource filtering Settings
Src/main/java * * / * .properties * * / * .xml false src/main/resources * * / * .properties * * / .xml false
Mybatis layer writing
Create mybatis-config.xml
Jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8jdbc.username=rootjdbc.password=123456
Write the Mapper interface of Dao layer
Package com.longdi.dao;import com.longdi.pojo.Books;import org.apache.ibatis.annotations.Param;import java.util.List;/** * @ author: brother * @ description * @ date: 20:32 on 2021-9-27 * / public interface BookMapper {/ / add a book int addBook (Books books); / / delete a book int deleteBookById (@ Param ("bookId") int id); / / update a book int updateBook (Books books) / / query a book Books queryBookById (@ Param ("bookId") int id); / / query all books List queryAllBook (); Books queryBookByName (@ Param ("bookName") String bookName);}
Write the Mapper.xml file corresponding to the interface. Packages that need to be imported into MyBatis
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}
Write the interface of the Service layer
Package com.longdi.service;import com.longdi.pojo.Books;import java.util.List;/** * @ author: brother * @ description * @ date: 21:03 on 2021-9-27 * / public interface BookService {/ / add a book int addBook (Books books); / / delete a book int deleteBookById (int id); / / update a book int updateBook (Books books); / / query a book Books queryBookById (int id) / / query all books List queryAllBook (); Books queryBookByName (String bookName);}
Implementation classes of the Service layer
Package com.longdi.service;import com.longdi.dao.BookMapper;import com.longdi.pojo.Books;import org.springframework.stereotype.Service;import java.util.List;/** * @ author: brother * @ description * @ date: 21:05 on 2021-9-27 * / 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 queryAllBook () {return bookMapper.queryAllBook () } public Books queryBookByName (String bookName) {return bookMapper.queryBookByName (bookName);}}
Spring integrates Mybatis spring-dao.xml
The following is the spring layer
Spring-service.xml file
The following is the SpringMVC layer
Configure web.xml
DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 DispatcherServlet / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 EncodingFilter / * 15
Spring-mvc.xml
Spring configuration consolidation file, creating applicationContext.xml
The above is the environment configuration of SSM. Let's write Controller layer and view layer to realize the CRUD function of books.
The function of adding, deleting, changing and querying books in controller layer
Import java.util.ArrayList;import java.util.List;/** * @ author: brother * @ description * @ date: 22:47 on 2021-9-27 * / @ Controller@RequestMapping ("/ book") public class BookController {@ Autowired @ Qualifier ("BookServiceImpl") private BookService bookService; @ RequestMapping ("/ allBook") public String list (Model model) {List list = bookService.queryAllBook (); model.addAttribute ("list", list) Return "allBook";} / / Jump to add a book page @ RequestMapping ("/ toAddBook") public String toAddPaper () {return "addBook";} / / request to add a book @ RequestMapping ("/ addBook") public String addBook (Books books) {System.out.println ("addBook= >" + books); bookService.addBook (books); return "redirect:/book/allBook" } / / Jump to the modification page @ RequestMapping ("toUpdateBook") public String toUpdatePaper (int id,Model model) {Books books = bookService.queryBookById (id); model.addAttribute ("book", books); return "updateBook";} / / modify the book @ RequestMapping ("updateBook") public String updateBook (Books books) {System.out.println ("updateBook= >" + books); int i=bookService.updateBook (books) If (I > 0) {System.out.println ("add books successfully" + books);} return "redirect:/book/allBook";} / delete books @ RequestMapping ("/ deleteBook/ {bookId}") public String deleBook (@ PathVariable ("bookId") int id) {bookService.deleteBookById (id); return "redirect:/book/allBook" } / / query books @ RequestMapping ("/ queryBook") public String queryBook (String queryBookName,Model model) {Books books=bookService.queryBookByName (queryBookName); System.err.println ("queryBook= >" + books); List list=new ArrayList (); list.add (books); if (books==null) {list=bookService.queryAllBook (); model.addAttribute ("error", "not found") } model.addAttribute ("list", list); return "allBook";}}
Write the home page index.jsp
Home page a {text-decoration: none; color:black; font-size:18px;} h4 {width:180px; height:38px; margin: 100px auto; text-align: center; line-height: 38px; background: deepskyblue; border-radius: 4px;} go to the book page
Write a book list page allBook.jsp
Book list-Show all books new books show all books ${error} Book number, book name, book quantity, book detail operation ${book.getBookID ()} ${book.getBookName ()} ${book.getBookCounts ()} ${book.getDetail ()} Change | Delete
Add a book page addBook.jsp
New Books New Book titles:
Number of books:
Book details:
Modify the book page updateBook.jsp
Modify information book name: number of books: book details:
The project is deployed to tomcat
Project structure diagram
These are all the contents of the article "how to integrate Java templates to completely solve the ssm configuration problem". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.