我的第一个ssm项目

第一步(建一个数据库表)

数据库文件

/*
Navicat MySQL Data Transfer

Source Server         : MySQL
Source Server Version : 80019
Source Host           : localhost:3306
Source Database       : ssmbuild

Target Server Type    : MYSQL
Target Server Version : 80019
File Encoding         : 65001

Date: 2020-05-18 00:17:41
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `books`
-- ----------------------------
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
  `bookID` int NOT NULL AUTO_INCREMENT COMMENT '娑旑洀d',
  `bookName` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '书名',
  `bookCounts` int NOT NULL COMMENT '涔︾殑鏁伴噺',
  `detail` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '书的表述',
  PRIMARY KEY (`bookID`),
  KEY `bookID` (`bookID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of books
-- ----------------------------
INSERT INTO `books` VALUES ('1', 'Java', '111', '从入门到放弃');
INSERT INTO `books` VALUES ('2', 'MySQL222', '10', '从删库到跑路');
INSERT INTO `books` VALUES ('3', 'Linux', '5', '从进门到进牢');
INSERT INTO `books` VALUES ('4', 'xsa', '21', 'xs');
INSERT INTO `books` VALUES ('9', 'aaa', '2', '2');

第二步(建一个Maven项目)

pom依赖


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>ssmartifactId>
    <version>1.0-SNAPSHOTversion>
    
    <build>
        <finalName>SSMDemo5finalName>
        
        <resources>
            <resource>
                <directory>${basedir}/src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
            resource>
        resources>
    build>

<dependencies>
      
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.13version>
        <scope>testscope>
    dependency>
    <dependency>
           
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.19version>
    dependency>
   
    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.2version>
    dependency>
    
     
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
    dependency>
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>servlet-apiartifactId>
        <version>2.5version>
    dependency>
    <dependency>
        <groupId>javax.servlet.jspgroupId>
        <artifactId>jsp-apiartifactId>
        <version>2.2version>
    dependency>
       
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.5.2version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>2.0.2version>
    dependency>
       
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>5.2.5.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>5.1.9.RELEASEversion>
    dependency>
       
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>1.16.10version>
    dependency>
     
    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjweaverartifactId>
        <version>1.9.4version>
    dependency>
     
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.11.0version>
    dependency>
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>fastjsonartifactId>
        <version>1.2.60version>
    dependency>
dependencies>
project>

第三步( 建立基本结构和配置框架 )

  • com.wxs.pojo

  • com.wxs.dao

  • com.wxs.service

  • com.wxs.controller

  • mybatis-config.xml

    
    
    <configuration>
        
    configuration>
    
  • applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 
    
beans>

第四步(编写持久层)

  • com.wxs.pojo.Books.java

    package com.wxs.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Books {
        private int bookID;
        private String bookName;
        private int bookCounts;
        private String detail;
    }
    
    
  • com.wxs.dao.BookMap.java

    package com.wxs.dao;
    
    import com.wxs.pojo.Books;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface BookMap {
        public int addBook(Books books);
        public int deleteBook(@Param("bookID") int id);
        public int updateBook(Books books);
        public Books findById(@Param("bookID") int id);
        public List<Books> findBooks();
        public List<Books> findByName(@Param("bookName") String name);
    }
    
    
  • com.wxs.dao.BookMap.xml



<mapper namespace="com.wxs.dao.BookMap">
    <insert id="addBook" parameterType="books">
        insert into ssmbuild.books(bookID, bookName, bookCounts, detail)
        values (null, #{bookName}, #{bookCounts}, #{detail});
    insert>
    <delete id="deleteBook">
        delete ssmbuild.books
        from ssmbuild.books
        where bookID = #{bookID}
    delete>
    <update id="updateBook" parameterType="books">
        update ssmbuild.books
        set bookCounts =#{bookCounts},bookName = #{bookName},detail = #{detail}
        where bookID = #{bookID} ;
    update>
    <select id="findById" resultType="books">
        select *
        from ssmbuild.books where bookID = #{bookID};
    select>
    <select id="findBooks" resultType="books">
        select *
        from ssmbuild.books;
    select>
    <select id="findByName" resultType="books" parameterType="String">
        select *
        from ssmbuild.books where bookName like '%${bookName}%';
    select>
mapper>database.properties
  • database.properties( 编写MyBatis的核心配置文件 )

    
    
    
       
          
      
    //配置别名
        
            
        
    
        
    
    
    
  • 数据库配置文件 (database.properties)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuilduseUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
jdbc.username=root
jdbc.password=root

第五步(编写业务层)

  • com.wxs.service.BookService(接口)

    package com.wxs.service;
    
    import com.wxs.pojo.Books;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface BookService {
        public int addBook(Books books);
        public int deleteBook(int id);
        public int updateBook(Books books);
        public Books findById(int id);
        public List<Books> findBooks();
        public List<Books> findBookName(String name);
    }
    
    
  • com.wxs.service.BookService(实现类)

    package com.wxs.service;
    
    import com.wxs.dao.BookMap;
    import com.wxs.pojo.Books;
    
    import java.util.List;
    
    public class BookServiceImpl implements BookService {
        BookMap bookMap;
    
        public void setBookMap(BookMap bookMap) {
            this.bookMap = bookMap;
        }
    
        public int addBook(Books books) {
            return bookMap.addBook(books);
        }
    
        public int deleteBook(int id) {
            return bookMap.deleteBook(id);
        }
    
        public int updateBook(Books books) {
            return bookMap.updateBook(books);
        }
    
        public Books findById(int id) {
            return bookMap.findById(id);
        }
    
        public List<Books> findBooks() {
            return bookMap.findBooks();
        }
    
        public List<Books> findBookName(String name) {
            return bookMap.findByName(name);
        }
    }
    
    

    第六步(spring整合dao和service)

  • spring-dao.xml

    
    <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/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd">
       
       
        <context:property-placeholder location="classpath:database.properties"/>
       
       
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
             
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="driverClass" value="${jdbc.driver}"/>
            
            <property name="maxPoolSize" value="30"/>
            <property name="minPoolSize" value="10"/>
             
            <property name="autoCommitOnClose" value="false"/>
             
            <property name="checkoutTimeout" value="10000"/>
            
            <property name="acquireRetryAttempts" value="2"/>
        bean>
      
        <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
             
            <property name="dataSource" ref="dataSource"/>
              
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        bean>
           
       
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
             
            <property name="basePackage" value="com.wxs.dao"/>
        bean>
    beans>
    
  • spring-service.xml

    
    <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/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd           http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
           
        <context:component-scan base-package="com.wxs.service"/>
           
        <bean class="com.wxs.service.BookServiceImpl" id="service">
            <property name="bookMap" ref="bookMap"/>
        bean>
           
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"/>
        bean>
    
    beans>
    

    第七步(编写controller层,并整合)

    spring-mvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <mvc:annotation-driven/>
    
        <mvc:default-servlet-handler/>
    
        <context:component-scan base-package="com.wxs.controller"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        bean>
    
    beans>
    

    web.xml

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.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">
        <servlet>
            <servlet-name>springMVCservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:applicationContext.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>springMVCservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
        <filter>
            <filter-name>encodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>utf-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>encodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    web-app>
    

applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="classpath:spring-dao"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="spring-mvc"/>
beans>

com.wxs.controller.bookController

package com.wxs.controller;


import com.wxs.pojo.Books;
import com.wxs.service.BookServiceImpl;
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("/book")
public class bookController {
    @Autowired
    @Qualifier("service")
    private BookServiceImpl bookService;
    @RequestMapping("/allBooks")
    public String allBooks(Model model){
        List<Books> books = bookService.findBooks();
        model.addAttribute("list",books);
        return "allBook";
    }
    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addBook";
    }
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/book/allBooks";
    }
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(int id,Model model){
        Books books = bookService.findById(id);
        model.addAttribute("QBook",books);
        return "upDateBooks";
    }
    @RequestMapping("/updateBook")
    public String toUpdateBook(Books books){
        bookService.updateBook(books);
        return "redirect:/book/allBooks";
    }
    @RequestMapping("/deleteBook")
    public String toUpdateBook(int id){
        bookService.deleteBook(id);
        return "redirect:/book/allBooks";
    }
    @RequestMapping("/queryBooks")
    public String queryBooks(String QBookName,Model model){
        List<Books> books = bookService.findBookName(QBookName);
        model.addAttribute("list",books);
        return "allBook";
    }
}

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: 武祥市
  Date: 2020/5/10
  Time: 18:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


  书籍



书籍

allBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 武祥市
  Date: 2020/5/17
  Time: 19:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>



    
    书籍展示


书籍编号 书籍名称 书籍数量 书籍详情 基本操作
${book.bookID} ${book.bookName} ${book.bookCounts} ${book.detail} 修改 |   删除

addBook.jsp

<%--
  Created by IntelliJ IDEA.
  User: 武祥市
  Date: 2020/5/17
  Time: 21:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    



upDateBooks.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: 武祥市
  Date: 2020/5/17
  Time: 21:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    



你可能感兴趣的:(ssm笔记)