本文实例为大家分享了librarysystem图书管理系统第二篇,供大家参考,具体内容如下
第一步:添加数据库配置文件
jdbc.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# 数据库驱动
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<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>
|
项目结构:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/q343509740/article/details/80411511