LibrarySystem图书管理系统(二)

 更新时间:2018年05月28日 08:33:34   作者:Remember_Ray  
这篇文章主要为大家详细介绍了LibrarySystem图书管理系统开发第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了LibrarySystem图书管理系统第二篇,供大家参考,具体内容如下

第一步:添加数据库配置文件

jdbc.properties

# 数据库驱动 
jdbc.driver=com.mysql.jdbc.Driver 
 
# 数据库地址 
jdbc.url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8 
 
# 用户名 
jdbc.username=root 
 
# 密码 
jdbc.password=root 
 
# 初始化连接 
initialSize=0 
 
# 最大连接数量 
maxActive=20 
 
# 最大空闲连接 
maxIdle=20 
 
# 最小空闲连接 
minIdle=1 
 
# 超时等待时间 
maxWait=60000 

第二步:添加mybatis配置文件

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
 <!-- 配置全局属性 --> 
 <settings> 
  <!-- 使用jdbc的getGeneratedKeys获取主键 --> 
  <setting name="useGeneratedKeys" value="true"/> 
 
  <!-- 使用别名替换列名, 默认ture --> 
  <setting name="useColumnLabel" value="true"/> 
 
  <!-- 开启驼峰命名转换 --> 
  <setting name="mapUnderscoreToCamelCase" value="true"/> 
 </settings> 
</configuration> 

第三步:添加Spring配置文件

在resources/spring目录下新建二个文件:

│   └── spring
│       ├── spring-mybatis.xml
│       ├── spring-service.xml
│       └── spring-mvc.xml

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
  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-4.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
 
 <!-- 注册组件扫描器 --> 
 <context:component-scan base-package="com.ray.controller"/> 
 
 <!-- 访问静态资源 --> 
 <mvc:default-servlet-handler/> 
 
 <!-- 开启注解模式 --> 
 <mvc:annotation-driven> 
  <mvc:message-converters> 
   <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
    <property name="supportedMediaTypes"> 
     <list> 
      <!-- 解决中文乱码 --> 
      <value>text/plain;charset=UTF-8</value> 
      <value>text/html;charset=UTF-8</value> 
      <value>application/json;charset=UTF-8</value> 
     </list> 
    </property> 
   </bean> 
  </mvc:message-converters> 
 </mvc:annotation-driven> 
 
 <!-- 视图解析器 --> 
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
  <!-- 前缀 --> 
  <property name="prefix" value="/WEB-INF/views/"/> 
  <!-- 后缀 --> 
  <property name="suffix" value=".jsp"/> 
 </bean> 
</beans> 

spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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" xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.1.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx.xsd"> 
 
 <!-- 1.配置数据库相关参数 --> 
 <context:property-placeholder location="classpath:jdbc.properties"/> 
 
 <!-- 2.配置druid数据源 --> 
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
  <!-- 配置连接池属性 --> 
  <property name="driverClassName" value="${jdbc.driver}"/> 
  <property name="url" value="${jdbc.url}"/> 
  <property name="username" value="${jdbc.username}"/> 
  <property name="password" value="${jdbc.password}"/> 
 
  <!-- 配置初始化大小,最小,最大值 --> 
  <property name="initialSize" value="1"/> 
  <property name="minIdle" value="1"/> 
  <property name="maxActive" value="10"/> 
 
  <!-- 配置获取连接等待超时的时间 --> 
  <property name="maxWait" value="10000"/> 
 
  <!-- 配置间隔多久才进行一次检测,检测需要关闭空闲连接,单位毫秒 --> 
  <property name="timeBetweenEvictionRunsMillis" value="60000"/> 
 
  <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
  <property name="minEvictableIdleTimeMillis" value="300000"/> 
 
  <!-- 验证连接有效与否的SQL,不同的数据配置不同 --> 
  <property name="validationQuery" value="SELECT 1" /> 
 
  <!-- 如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效 --> 
  <property name="testWhileIdle" value="true"/> 
 
  <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> 
  <property name="testOnBorrow" value="true"/> 
  <property name="testOnReturn" value="false"/> 
 
  <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
  <property name="poolPreparedStatements" value="true"/> 
  <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> 
 
  <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 --> 
  <property name="defaultAutoCommit" value="true" /> 
 
  <!-- 开启Druid的监控统计功能 --> 
  <property name="filters" value="stat"/> 
 </bean> 
 
 <!-- 3.配置Mybatis的SqlSessionFactory对象 --> 
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
  <!-- 配置MyBatis全局配置文件 --> 
  <property name="configLocation" value="classpath:mybatis-config.xml"/> 
  <!-- 注入数据库连接池 --> 
  <property name="dataSource" ref="dataSource"/> 
  <!-- 扫描配置文件 --> 
  <property name="mapperLocations" value="classpath:mapping/*.xml"/> 
 </bean> 
 
 <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> 
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
  <!-- 给出需要扫描Dao接口包 --> 
  <property name="basePackage" value="com.ray.dao"/> 
 </bean> 
 
</beans> 

spring-service.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd"> 
 
 <!-- 自动扫描 --> 
 <context:component-scan base-package="com.ray"/> 
 
 <!-- 事务管理 --> 
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
  <property name="dataSource" ref="dataSource"/> 
 </bean> 
 
 <!-- 开启事务控制的注解支持 --> 
 <tx:annotation-driven transaction-manager="transactionManager"/> 
</beans> 

 第四步:添加logback配置文件

logback配置比log4j要简单点,功能类似

├── resources
│   ├── logback.xml

在resources文件夹下新建文件:logback.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<configuration debug="true"> 
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
 <encoder> 
  <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> 
 </encoder> 
 </appender> 
 <!--开启debug日志模式,在控制台打印日志--> 
 <root level="debug"> 
 <appender-ref ref="STDOUT" /> 
 </root> 
</configuration>

第五步:配置web.xml

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_3_1.xsd" 
   version="3.1" metadata-complete="true"> 
 <display-name>Archetype Created Web Application</display-name> 
 
 <!-- 配置DispatcherServlet --> 
 <servlet> 
 <servlet-name>seckill-dispatcher</servlet-name> 
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 <!-- 配置springMVC需要加载的配置文件 
  spring-dao.xml,spring-service.xml,spring-web.xml 
  Mybatis - > spring -> springmvc 
  --> 
 <init-param> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>classpath:spring/spring-*.xml</param-value> 
 </init-param> 
 <load-on-startup>1</load-on-startup> 
 <async-supported>true</async-supported> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>seckill-dispatcher</servlet-name> 
 <!-- 默认匹配所有的请求 --> 
 <url-pattern>/</url-pattern> 
 </servlet-mapping> 
 
 <!-- 处理中文乱码 --> 
 <filter> 
 <filter-name>CharacterEncodingFilter</filter-name> 
 <filter-class> 
  org.springframework.web.filter.CharacterEncodingFilter 
 </filter-class> 
 <init-param> 
  <param-name>encoding</param-name> 
  <param-value>UTF-8</param-value> 
 </init-param> 
 </filter> 
 <filter-mapping> 
 <filter-name>CharacterEncodingFilter</filter-name> 
 <url-pattern>/*</url-pattern> 
 </filter-mapping> 
</web-app> 

项目结构:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Springboot+echarts实现可视化

    Springboot+echarts实现可视化

    这篇文章主要为大家详细介绍了Springboot+echarts实现可视化,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • 详解将Eclipse代码导入到AndroidStudio的两种方式

    详解将Eclipse代码导入到AndroidStudio的两种方式

    本篇文章主要介绍了详解将Eclipse代码导入到AndroidStudio的两种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • 详解Java中LinkedStack链栈的实现

    详解Java中LinkedStack链栈的实现

    这篇文章主要为大家详细介绍了Java中LinkedStack链栈的相关知识,文中的示例代码讲解详细,对我们学习Java有一定帮助,需要的可以参考一下
    2022-11-11
  • Java 中 synchronized 的使用方式和锁升级

    Java 中 synchronized 的使用方式和锁升级

    Java中的synchronized关键字用于实现线程同步,保证同一时刻只有一个线程可以访问被同步的代码块或方法,JVM引入了锁升级机制,从无锁状态开始,根据竞争情况逐步升级为偏向锁、轻量级锁和重量级锁,以提高性能,感兴趣的朋友一起看看吧
    2025-03-03
  • JAVA调用JavaScript方法举例详解

    JAVA调用JavaScript方法举例详解

    之前在一次机缘巧合的情况下,需要时用JAVA执行js方法,查阅了一些文档,找到了相关解决方法,这里和大家分享一下,下面这篇文章主要给大家介绍了关于JAVA调用JavaScript方法的相关资料,需要的朋友可以参考下
    2023-10-10
  • java 实现文件夹的拷贝实例代码

    java 实现文件夹的拷贝实例代码

    这篇文章主要介绍了java 实现文件夹的拷贝实例代码的相关资料,需要的朋友可以参考下
    2017-04-04
  • SpringBoot跨域Access-Control-Allow-Origin实现解析

    SpringBoot跨域Access-Control-Allow-Origin实现解析

    这篇文章主要介绍了SpringBoot跨域Access-Control-Allow-Origin实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • SpringBoot请求参数加密、响应参数解密的实现

    SpringBoot请求参数加密、响应参数解密的实现

    在项目开发工程中,有的项目可能对参数安全要求比较高,在整个http数据传输的过程中都需要对请求参数、响应参数进行加密,本文主要介绍了SpringBoot请求参数加密、响应参数解密的实现,感兴趣的可以了解一下
    2024-01-01
  • Maven仓库镜像配置的方法实现

    Maven仓库镜像配置的方法实现

    本文介绍了如何使用Maven仓库镜像来加速构建过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-12-12
  • IntelliJ IDEA2020.3详细安装教程

    IntelliJ IDEA2020.3详细安装教程

    这篇文章主要介绍了IntelliJ IDEA2020.3详细安装教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12

最新评论