使用ssm框架编写简单图书管理系统(一)

1.创建项目 编写配置文件 

可参考上一篇文章 https://blog.csdn.net/xyz1995920/article/details/79901324

贴上配置文件:

1、pom.xml:

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

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">
  4.0.0

  cn.vp
  BookManger
  1.0-SNAPSHOT
  war

  BookManger Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  

    
      junit
      junit
      4.11
      test
    


    
    
      javax.servlet
      jstl
      1.2
    
    
    
    
      com.alibaba
      druid
      1.1.6
    

    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
    
      org.springframework
      spring-tx
      4.2.4.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      4.2.4.RELEASE
    


    
    
      asm
      asm
      3.3.1
    

    
      cglib
      cglib
      2.2.2
    

    
      org.javassist
      javassist
      3.17.1-GA
    
    
      mysql
      mysql-connector-java
      5.1.6
    
    
    
      log4j
      log4j
      1.2.17
    
    
    
      commons-logging
      commons-logging
      1.2
    

    
      org.slf4j
      slf4j-api
      1.5.8
    

    
      org.slf4j
      slf4j-log4j12
      1.5.8
      test
    

    
      org.mybatis
      mybatis
      3.3.0
    

    


    
    
      org.springframework
      spring-context
      4.2.4.RELEASE
    
    
    
      org.springframework
      spring-core
      4.2.4.RELEASE
    

    
    
      org.springframework
      spring-beans
      4.2.4.RELEASE
    

    
    
      org.springframework
      spring-expression
      4.2.4.RELEASE
    
    
    
      org.springframework
      spring-test
      4.2.4.RELEASE
      test
    

    
    
      aopalliance
      aopalliance
      1.0
    
    
    
      org.ow2.asm
      asm
      4.2
    
    
    
      org.aspectj
      aspectjrt
      1.8.13
    

    
    
      org.aspectj
      aspectjweaver
      1.8.13
    

    
    
    
      org.springframework
      spring-web
      4.2.4.RELEASE
    


    
    
      org.springframework
      spring-webmvc
      4.2.4.RELEASE
    

    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    

    
    
      commons-io
      commons-io
      1.3.1
    

    
    
      com.alibaba
      fastjson
      1.2.46
    


  

  

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

    BookManger
    
      
        
          org.mybatis.generator
          mybatis-generator-maven-plugin
          1.3.5
          
            
            true
            
            true
          
        


        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

2、web.xml:

xml version="1.0" encoding="UTF-8"?>
version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  Archetype Created Web Application


  
  
    Spring
    org.springframework.web.context.ContextLoaderListener
  
  
    contextConfigLocation
    classpath:applicationContext.xml
  



  
    springMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc-servlet.xml
    
    1
  
  
    springMVC
    /
  




3、applicationContext.xml (spring配置文件)

xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <context:annotation-config/>
    <context:component-scan base-package="cn.vp">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    
    id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        name="url" value="${url}"/>
        name="username" value="${username}"/>
        name="password" value="${password}"/>
        name="initialSize" value="5"/>
        name="maxActive" value="20"/>
        name="minIdle" value="2"/>
        name="maxWait" value="5000"/>
    

    
    id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        name="dataSource" ref="druidDataSource"/>
        
        name="mapperLocations" value="classpath:cn/vp/dao/*.xml"/>
        
        name="typeAliasesPackage" value="cn.vp"/>
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        name="basePackage" value="cn.vp.dao"/>
        name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    
    
    id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        name="dataSource" ref="druidDataSource"/>
    
    
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>



4、springmvc-servlet.xml(springmvc配置文件)

xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="cn.vp.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"/>

     
    <mvc:resources location="/images/" mapping="/images/**"/>

     
    id="formattingConversionServiceFactoryBean"
          class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        name="converters">
            
                id="dateConverter" class="cn.vp.controller.DateConverter"/>
            
        
    


    
    id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        
        name="prefix" value="/WEB-INF/jsp/"/>
        
        name="suffix" value=".jsp"/>
        
        name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
    
    
    id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
        name="defaultEncoding" value="utf-8"/> 
        name="maxInMemorySize" value="10240"/> 
        name="uploadTempDir" value="/images/"/> 
        name="maxUploadSize" value="-1"/> 
    
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**"/>

5、generatorconfig.xml(自动生成javabean和dao)

xml version="1.0" encoding="UTF-8"?>
generatorConfiguration
      PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
      "http://www.mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

   resource="jdbc.properties"/>
   
   location="H:\Maven\LocalWarehouse\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" />
   id="DB2Tables" targetRuntime="MyBatis3">
      
      type="org.mybatis.generator.plugins.SerializablePlugin"/>
      
         name="suppressDate" value="true" />
         
         name="suppressAllComments" value="true" />
      
      
      driverClass="${driver}"
         connectionURL="${url}" userId="${username}" password="${password}">
      
      
      
         name="forceBigDecimals" value="false" />
      
      
      targetPackage="cn.vp.bean"
         targetProject="src/main/java">
         
         name="enableSubPackages" value="true" />
         
         name="trimStrings" value="true" />
      
      
      targetPackage="cn.vp.dao"
         targetProject="src/main/java">
         name="enableSubPackages" value="true" />
      
      
      type="XMLMAPPER"
         targetPackage="cn.vp.dao" targetProject="src/main/java">
         name="enableSubPackages" value="true" />
      
      
      
      tableName="book_info"
             domainObjectName="BookInfo"
            enableCountByExample="false" 
            enableUpdateByExample="false"
            enableDeleteByExample="false"
            enableSelectByExample="false"
            selectByExampleQueryId="false"/>

      
tableName="book_type" domainObjectName="BookType" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>

6、jdbc.properties和log4j.properties


2. 生成代码并修改

1.配置generator

使用ssm框架编写简单图书管理系统(一)_第1张图片

2.配置tomcat

3.生成代码


4.根据需求修改实体类代码  

使用ssm框架编写简单图书管理系统(一)_第2张图片

在BookInfoMapper类加上查询所有图书的方法

使用ssm框架编写简单图书管理系统(一)_第3张图片

在对应的xml里面加上sql语句 并修改上面因为实体类变化要改的代码

xml version="1.0" encoding="UTF-8"?>
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace="cn.vp.dao.BookInfoMapper">
  id="BaseResultMap" type="cn.vp.bean.BookInfo">
    column="book_id" jdbcType="INTEGER" property="bookId" />
    column="book_code" jdbcType="VARCHAR" property="bookCode" />
    column="book_name" jdbcType="VARCHAR" property="bookName" />
    column="book_author" jdbcType="VARCHAR" property="bookAuthor" />
    column="publish_press" jdbcType="VARCHAR" property="publishPress" />
    column="publish_date" jdbcType="DATE" property="publishDate" />
    column="is_borrow" jdbcType="INTEGER" property="isBorrow" />
    column="path" jdbcType="VARCHAR" property="path" />
    
      
      
    
  
  id="Base_Column_List">
    book_id, book_code, book_name, book_type, book_author, publish_press, publish_date, 
    is_borrow, path
  
  


  

  id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from book_info
    where book_id = #{bookId,jdbcType=INTEGER}
  
  id="insert" parameterType="cn.vp.bean.BookInfo">
    insert into book_info (book_id, book_code, book_name, 
      book_type, book_author, publish_press, 
      publish_date, is_borrow, path
      )
    values (#{bookId,jdbcType=INTEGER}, #{bookCode,jdbcType=VARCHAR}, #{bookName,jdbcType=VARCHAR}, 
      #{bookType.id,jdbcType=INTEGER}, #{bookAuthor,jdbcType=VARCHAR}, #{publishPress,jdbcType=VARCHAR},
      #{publishDate,jdbcType=DATE}, #{isBorrow,jdbcType=INTEGER}, #{path,jdbcType=VARCHAR}
      )
  
  id="insertSelective" parameterType="cn.vp.bean.BookInfo">
    insert into book_info
    prefix="(" suffix=")" suffixOverrides=",">
      test="bookId != null">
        book_id,
      
      test="bookCode != null">
        book_code,
      
      test="bookName != null">
        book_name,
      
      test="bookType != null">
        book_type,
      
      test="bookAuthor != null">
        book_author,
      
      test="publishPress != null">
        publish_press,
      
      test="publishDate != null">
        publish_date,
      
      test="isBorrow != null">
        is_borrow,
      
      test="path != null">
        path,
      
    
    prefix="values (" suffix=")" suffixOverrides=",">
      test="bookId != null">
        #{bookId,jdbcType=INTEGER},
      
      test="bookCode != null">
        #{bookCode,jdbcType=VARCHAR},
      
      test="bookName != null">
        #{bookName,jdbcType=VARCHAR},
      
      test="bookType != null">
        #{bookType.id,jdbcType=INTEGER},
      
      test="bookAuthor != null">
        #{bookAuthor,jdbcType=VARCHAR},
      
      test="publishPress != null">
        #{publishPress,jdbcType=VARCHAR},
      
      test="publishDate != null">
        #{publishDate,jdbcType=DATE},
      
      test="isBorrow != null">
        #{isBorrow,jdbcType=INTEGER},
      
      test="path != null">
        #{path,jdbcType=VARCHAR},
      
    
  
  id="updateByPrimaryKeySelective" parameterType="cn.vp.bean.BookInfo">
    update book_info
    
      test="bookCode != null">
        book_code = #{bookCode,jdbcType=VARCHAR},
      
      test="bookName != null">
        book_name = #{bookName,jdbcType=VARCHAR},
      
      test="bookType != null">
        book_type = #{bookType.id,jdbcType=INTEGER},
      
      test="bookAuthor != null">
        book_author = #{bookAuthor,jdbcType=VARCHAR},
      
      test="publishPress != null">
        publish_press = #{publishPress,jdbcType=VARCHAR},
      
      test="publishDate != null">
        publish_date = #{publishDate,jdbcType=DATE},
      
      test="isBorrow != null">
        is_borrow = #{isBorrow,jdbcType=INTEGER},
      
      test="path != null">
        path = #{path,jdbcType=VARCHAR},
      
    
    where book_id = #{bookId,jdbcType=INTEGER}
  
  id="updateByPrimaryKey" parameterType="cn.vp.bean.BookInfo">
    update book_info
    set book_code = #{bookCode,jdbcType=VARCHAR},
      book_name = #{bookName,jdbcType=VARCHAR},
      book_type = #{bookType.id,jdbcType=INTEGER},
      book_author = #{bookAuthor,jdbcType=VARCHAR},
      publish_press = #{publishPress,jdbcType=VARCHAR},
      publish_date = #{publishDate,jdbcType=DATE},
      is_borrow = #{isBorrow,jdbcType=INTEGER},
      path = #{path,jdbcType=VARCHAR}
    where book_id = #{bookId,jdbcType=INTEGER}
  

到目前为止框架大致搭完

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