图书管理系统curd配置环境及controller层编写

1.编写pom.xml
<dependencies>
  <!-- mybatis分页插件-->
  <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
  <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
  </dependency>

  
  <!-- **********************  json********************** -->
  <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.41</version>
  </dependency>


  <!-- **********************  编写Controller IO和fileupload********************** -->
  <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
  <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
  <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
  </dependency>


  <!-- **********************工具包[日志、junit4、依赖包]********************** -->
  <!-- Java依赖包 -->
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
  </dependency>

  <!-- 日志 -->
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>

  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
  </dependency>

  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
    <scope>test</scope>
  </dependency>

  <!-- junit -->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>


  <!-- **********************数据库[数据库连接池+数据库连接]********************** -->
  <!-- 德鲁伊数据库连接池 -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.12</version>
  </dependency>

  <!-- 数据库连接池 -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.26</version>
  </dependency>


  <!-- **********************MyBatis********************** -->
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.3.0</version>
  </dependency>

  <dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.2</version>
  </dependency>

  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.2</version>
  </dependency>


  <!-- **********************Spring 工具包********************** -->
  <!-- 封装了spring以JDBC方式对数据库访问的各种操作类 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>

  <!-- 支持Spring组建JUnit和TestNG的单元测试和集成测试 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.2.4.RELEASE</version>
    <scope>test</scope>
  </dependency>

  <!-- 封装了spring对于事物的控制 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>


  <!-- **********************Spring IOC********************** -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>


  <!-- **********************Spring AOP********************** -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
  </dependency>

  <dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm</artifactId>
    <version>4.2</version>
  </dependency>

  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.7.1</version>
  </dependency>

  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.7.1</version>
  </dependency>


  <!-- **********************Spring MVC********************** -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>


  <!-- **********************JSTL表达式********************** -->
  <!-- 防止jstl jar包与tomcat本身产生冲突,需要对jstl jar包解依赖-->
  <dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
    <exclusions>
      <exclusion>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
      </exclusion>
      <exclusion>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
      </exclusion>
    </exclusions>
  </dependency>

  <dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jstl-impl</artifactId>
    <version>1.2</version>
    <exclusions>
      <exclusion>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
      </exclusion>
      <exclusion>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
      </exclusion>
      <exclusion>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl-api</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
</dependencies>

<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>
  <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    <plugins>
      <!-- 使用MyBatis Generator逆向工程生成代码-->
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.5</version>
        <configuration>
          <!--允许移动生成的文件-->
          <verbose>true</verbose>
          <!--允许覆盖生成的文件-->
          <overwrite>true</overwrite>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
      <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>
2.web.xml编写
<!--  启动spring监听器 -->
<listener>
    <description>Spring 监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationConext.xml</param-value>
</context-param>

<!--  加载springmvc的配置文件 -->
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
3.springMVC.xml编写
<!-- 添加springmvc扫描器 只扫描扫描 @Controller注解 -->
<context:component-scan base-package="cn.vp.controller">
    <context:include-filter type="annotation"
                            expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!--开启注解-->
<context:annotation-config  />
<!-- 注册全局日期转换器-->
<mvc:annotation-driven conversion-service="conversionService2" />

<context:component-scan base-package="cn.vp.controller">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!--  配置视图解析器-->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <!-- 配置前缀-->
    <property name="prefix" value="/WEB-INF/front/" />
    <!-- 配置后缀-->
    <property name="suffix" value=".jsp" />
    <!--  如果页面中需要需要jstl-->
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

<!-- 静态资源处理器 -->
<mvc:resources location="/upload/" mapping="/upload/**" />
<mvc:resources location="/js/" mapping="/js/**" />


<!--   文件上传 -->
<bean id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
    <property name="defaultEncoding" value="utf-8"/> <!-- 默认编码 (ISO-8859-1) -->
    <property name="maxInMemorySize" value="10240"/> <!-- 最大内存大小 (10240)-->
    <property name="uploadTempDir" value="/upload/"/> <!-- 上传后的目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
    <property name="maxUploadSize" value="-1"/> <!-- 最大文件大小,-1为无限止(-1) -->
</bean>

<!--配置全局日期转换器-->
<bean  id="conversionService2" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="cn.vp.converter.DateConvertUtil" />
        </list>
    </property>
</bean>

4.controller层编写

@Controller
public class BookInfoController implements ServletContextAware {

    @Autowired
    private BookInfoService bookInfoService;

    private ServletContext servletContext;

    @Override
    public void setServletContext(javax.servlet.ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    /**
     * 图片的回传
     *
     * @param fileImage 图片的名称
     * @return 图片信息
     */
    @RequestMapping("/imageupload.htm")
    @ResponseBody
    public String imageUpload(@RequestParam("fileImage") CommonsMultipartFile fileImage) {
        // 获取上传图片的位置
        String path = servletContext.getRealPath("/upload/");
        // 获取文件名称
        String name = fileImage.getOriginalFilename();
        // 创建file对象 写入
        File file = new File(path, name);
        try {
            fileImage.getFileItem().write(file);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 将上传的图片路径以json的方式返回客户端
        String imagePath = "upload/" + name;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("imagePath", imagePath);
        // 将对象转为json格式
        String json = jsonObject.toJSONString();
        System.out.println("json:" + json);
        return json;
    }

    /**
     * 新增图书
     *
     * @param bookInfo 图书对象
     * @return main.jsp
     */

    @RequestMapping("/addBook.htm")
    public String addBook(BookInfo bookInfo) {
        System.err.println(bookInfo);

        int i = bookInfoService.addBook(bookInfo);
        if (i > 0) {
            System.out.println("新增成功");
        } else {
            System.out.println("失败...");
        }
        return "forward:main.jsp";
    }

    /**
     * 批量删除 通过图书id集合
     *
     * @param bookid//删除的图书id数组集合
     * @return ajaxQuery.htm
     */
    @RequestMapping(value = "/ajaxDelete.htm")
    public String deleteByBookIds(String[] bookid) {
        int i = bookInfoService.deleteByBookIds(bookid);
        return "forward:/ajaxQuery.htm";
    }

    /**
     * 删除图书信息  根据图书id
     *
     * @param bookid 删除的图书id
     * @return ajaxQuery.htm
     */
    @RequestMapping(value = "/delete.htm")
    public String deleteBook(String bookid) {
        int i = bookInfoService.deleteByPrimaryKey(Integer.valueOf(bookid));
        return "forward:main.jsp";
    }

    /**
     * 根据图书id查询 图书信息
     *
     * @param bookInfoId 图书id
     * @return 图书对象
     */
    @RequestMapping(value = "/queryByBookID.htm")
    public String queryById(@RequestParam("bookid") Integer bookInfoId, ModelMap map) {
        BookInfo bookInfo = bookInfoService.queryByID(bookInfoId);
        map.put("bookInfo", bookInfo);
        return "forward:editBookInfo.jsp";
    }

    /**
     * 更改图书信息
     * @param bookInfo 图书对象
     * @return main.jsp
     */
    @RequestMapping(value = "/editBook.htm")
    public String editBoonInfo(BookInfo bookInfo) {
        int i = bookInfoService.updateBookInfo(bookInfo);
        System.err.println("受影响行数:" + i);
        return "forward:main.jsp";

    }

    /**
     * 详情查找
     * @param bookid 详情的图书id
     * @return show.jsp
     */
    @RequestMapping(value = "/show.htm")
    public String findBook(String bookid, ModelMap map) {
        System.out.println(bookid);
        BookInfo bookInfo = bookInfoService.selectByPrimaryKey(Integer.valueOf(bookid));
        System.out.println(bookInfo);
        map.put("bookInfo", bookInfo);
        return "forward:show.jsp";
    }

    /**
     * 条件查找
     * @param booktype 图书类型
     * @param bookname 图书名字
     * @param borrow   借阅是否
     * @param now      显示查询页数
     * @return 查找的得到图书集合信息
     */
    @RequestMapping(value = "/ajaxQuery.htm", produces = {"application/json;", "text/html;charset=UTF-8;"})
    @ResponseBody //指明返回json格式的编码格式
    public String ajaxQuery(String booktype, String bookname, String borrow, @RequestParam(name = "now", required = false, defaultValue = "1") Integer now) {
        //设置
        PageHelper.startPage(now, 3);
        //1.调用业务逻辑层
        List<BookInfo> query = bookInfoService.query(booktype, bookname, borrow);
        //1.1将查询的集合封装到PageInfo
        PageInfo<BookInfo> bookInfoPageInfo = new PageInfo<>(query);
        //2.将获取的数据封装为Json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bookinfos", bookInfoPageInfo);
        String s = JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd");
        return s;
    }

}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭