บทความนี้แบ่งปันบทความที่สองของระบบการจัดการห้องสมุด LibrarySystem สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
ขั้นตอนที่ 1: เพิ่มไฟล์การกำหนดค่าฐานข้อมูล
jdbc.properties
# ไดรเวอร์ฐานข้อมูล jdbc.driver = com.mysql.jdbc.driver # ที่อยู่ฐานข้อมูล jdbc.url = jdbc: mysql: // localhost: 3306/ไลบรารี? useunicode = true & aritialencoding = utf-8 # username jdbc.username = รูท maxactive = 20 # การเชื่อมต่อที่ไม่ได้ใช้งานสูงสุด maxidle = 20 # การเชื่อมต่อขั้นต่ำที่ไม่ได้ใช้งาน minidle = 1 # เวลารอเวลารอ MaxWait = 60000
ขั้นตอนที่ 2: เพิ่มไฟล์กำหนดค่า MyBatis
mybatis-config.xml
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! การกำหนดค่า Doctype สาธารณะ "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd" GetGeneratedKeys ของ JDBC เพื่อรับคีย์หลัก-> <การตั้งค่าชื่อ = "useGeneratedKeys" value = "true"/> <!-แทนที่ชื่อคอลัมน์ด้วยนามแฝง, ture เริ่มต้น-> <การตั้งค่าชื่อ = "usecolumnlabel" ค่า = "true"/> <! </การกำหนดค่า>
ขั้นตอนที่ 3: เพิ่มไฟล์การกำหนดค่าสปริง
สร้างไฟล์ใหม่สองไฟล์ในไดเรกทอรีทรัพยากร/สปริง:
spring
│── Spring-mybatis.xml
│── spring-service.xml
│── Spring-MVC.XML
Spring-mvc.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: บริบท = "http://www.springframework.org/schema/mvc" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <! -> <MVC: Default-Servlet-Handler/> <!-เปิดใช้งานโหมดคำอธิบายประกอบ-> <MVC: คำอธิบายประกอบ-ไดรฟ์> <MVC: ข้อความคอนเวอร์เตอร์ข้อความ> <bean> <ชื่อทรัพย์สิน = "Supportedediatypes"> <yts> <!-แก้ปัญหาภาษาจีน <value> text/html; charset = utf-8 </value> <value> แอปพลิเคชัน/json; charset = utf-8 </alue> </list> </perty> </ebean> </mvc: ตัวแปลงข้อความ> </mvc: Prefix-prefix-prefix-prefix value = "/web-inf/views/"/> <!-คำต่อท้าย-> <property name = "suffix" value = ". jsp"/> </ebean> </ebeans>
Spring-mybatis.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-1. การกำหนดค่าพารามิเตอร์ที่เกี่ยวข้องกับฐานข้อมูล แหล่งข้อมูล druid-> <bean id = "dataSource" init-method = "init" destroy-method = "close"> <!-กำหนดค่าคุณสมบัติพูลการเชื่อมต่อ-> <property name = "driverclassname" value = "$ {jdbc.driver}"/> <property name = "url" value = "$ {jdbc.username}"/> <property name = "รหัสผ่าน" value = "$ {jdbc.password}"/> <!-กำหนดค่าขนาดเริ่มต้น, ค่าสูงสุด, ค่าสูงสุด-> <property name = "ค่าเริ่มต้น" value "1"/> <ชื่อคุณสมบัติ = "minidle" หมดเวลาการเชื่อมต่อ-> <property name = "maxwait" value = "10,000"/> <!-กำหนดระยะเวลาในการดำเนินการตรวจจับช่วงเวลาและการตรวจจับต้องปิดการเชื่อมต่อที่ไม่ได้ใช้งานในมิลลิวินาที-> <property name = "timebetweenevictionrunsmillis name = "minevictableidletimeMillis" value = "300000" /> <!-SQL ที่ตรวจสอบว่าการเชื่อมต่อนั้นถูกต้องการกำหนดค่าข้อมูลที่แตกต่างกันนั้นแตกต่างกัน-> <ชื่อคุณสมบัติ = "validationQuery" value = "เลือก 1" /> <! value = "true"/> <!-ขอแนะนำให้กำหนดค่าเป็นจริงที่นี่เพื่อป้องกันไม่ให้การเชื่อมต่อที่เรียกคืนไม่สามารถใช้งานได้-> <property name = "testonBorrow" value = "true"/> <property name = "testOnBorrow" value = "true"/> <property name = "testOnReturn name = "poolpreparedStatements" value = "true"/> <property name = "MaxPoolPreparedStatementPerConnectiones" value = "20"/> <!-กำหนดค่าวิธีการส่งที่นี่ค่าเริ่มต้นเป็นจริง value = "stat"/> </ebean> <!-3. กำหนดค่าวัตถุ SQLSessionFactory ของ MyBatis-> <bean id = "SQLSessionFactory"> <!-กำหนดค่าไฟล์การกำหนดค่าส่วนกลาง myBatis-> <property name = "configlocation" value = "classpath: mybatis-config.xml ref = "dataSource"/> <!-สแกนไฟล์การกำหนดค่า-> <property name = "mapperlocations" value = "classpath: การแมป/*. xml"/> </ebean> <!-4. กำหนดค่าอินเตอร์เฟส Dao ที่สแกน name = "basepackage" value = "com.ray.dao"/> </ebean> </ebeans>Spring-service.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <! <property name = "dataSource" ref = "dataSource"/> </ebean> <!-เปิดการสนับสนุนคำอธิบายประกอบสำหรับการควบคุมธุรกรรม-> <tx: Transaction-Driven Transaction-Manager = "TransactionManager"
ขั้นตอนที่ 4: เพิ่มไฟล์การกำหนดค่า logback
การกำหนดค่า logback นั้นง่ายกว่า log4j และฟังก์ชั่นคล้ายกัน
├──ทรัพยากร
│── logback.xml
สร้างไฟล์ใหม่ในโฟลเดอร์ทรัพยากร: logback.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <การกำหนดค่า debug = "true"> <appender name = "stdout"> <encoder> <matter> %d {hh: mm: ss.ss} [thread] %-5 พิมพ์บันทึกในคอนโซล-> <root level = "debug"> <appender-ref ref = "stdout"/> </root> </configuration>ขั้นตอนที่ 5: กำหนดค่า 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 http://xmlns.jcp.org/xml/ns/nsds เวอร์ชัน = "3.1" metadata-complete = "true"> <pramplay-name> archetype แอปพลิเคชันเว็บที่สร้างขึ้น </display-name> <!-กำหนดค่า dispatcherServlet-> <servlet> โหลดสำหรับ SpringMVC Spring-Dao.xml, Spring-Service.xml, Spring-Web.xml MyBatis-> Spring-> SpringMvc-> <init-Param> <param-Name> ContextConfiglocation </param-name> <param-value> <load-on-startup> 1 </load-on-startup> <sync-upported> True </async-supported> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name> seckill-dispatcher </servlet-name> <tilter> <melter-Name> org.springframework.web.filter.characterencodingfilter </filter-class> <int-param> <param-name> <filter-mapping> <filter-name> ตัวละคร actialencodingfilter </filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </eb-app>
โครงสร้างโครงการ:
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น