Cet article partage le deuxième article du système de gestion de bibliothèque de bibliothèque pour votre référence. Le contenu spécifique est le suivant
Étape 1: Ajouter le fichier de configuration de la base de données
jdbc.properties
# Database Driver jdbc.Driver = com.mysql.jdbc.driver # Adresse de base de données jdbc.url = jdbc: mysql: // localhost: 3306 / bibliothèque? Useunicode = true & caractères jdbc. des connexions maxactive = 20 # connexion maximale de ralenti maxidle = 20 # connexion inactive minimum minidle = 1 # temps d'attente de temps mort maxwait = 60000
Étape 2: Ajouter le fichier de configuration 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-3config.dtd"> <configuration> <! GetGeneratedKeys de JDBC pour obtenir la touche principale -> <paramètres name = "useGeneratedKeys" value = "true" /> <! - Remplacez les noms de colonne par un alias, par défaut -> <paramètre name = "UseColumnLabel" value = "true" /> <! - Activer la conversion de noms de camel " </FIGIGURATION>
Étape 3: Ajouter le fichier de configuration de Spring
Créez deux nouveaux fichiers dans le répertoire Resources / Spring:
│ └fiques
│ ├── printemps-mybatis.xml
│ ├── Spring-Service.xml
│ └fiques-Spring-Mvc.xml
printemps-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/mvc" xsi: schemalocation = "http://www.springfrrameworkwork.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.xsddd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <! - Enregistrer les composants Scanner -> <Context: Component-Scan Base-Package =" Com.raray.Con <mvc: par défaut-service-handler /> <! - Activer le mode d'annotation -> <mvc: annotation-driven> <mvc: Message-converters> <ean> <propriété name = "SupportEdMediaTypes"> <sist> <! - Solve chinois Barbled -> <value> text / plain; charset = utf-8 </value> <valeur> <value> application / json; charset = utf-8 </value> </sist> </ propriété> </ bean> </ mvc: Message-convertisseurs> </ mvc: annotation-driven> <! - Afficher l'analyseur -> <anan> <! - Prefix -> <propriété name = "Prefix" value = "/ web-inf / vues /" /> <! Value = ". JSP" /> </bEAN> </EANS>
printemps-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.springfrrameworkwork.org/schema/bans 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. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <! - 1. Configurer les paramètres de données de données -> <Contexte: Imptoperholder Floy =" ClassPath: JDBC.properes "/ <! La source de données Druid -> <bean id = "dataSource" init-méthod = "init" destrie-méthod = "close"> <! - Configurez les propriétés du pool de connexion -> <propriété name = "driverclassname" value = "$ {jdbc.driver}" /> <propriété name = "url" value = "$ {jdbc.url}" /> <woftaméli Value = "$ {jdbc.Username}" /> <propriété name = "mot de passe" value = "$ {jdbc.password}" /> <! - Configurez la taille d'initialisation, minimum, valeur maximale -> <propriété name = "initialSize" value = "1" /> <propriété name = "minidle" value = "1" /> <propriété Nom = "Maxcactive" Value = " Pour obtenir le temps out en attente de connexion -> <propriété name = "maxwait" value = "10000" /> <! - Configurez le temps qu'il faut pour effectuer un intervalle de détection, et la détection nécessite la fermeture de la connexion inactive en millisecondes -> <propriété name = "TimeBetweenvictionRunsmillis" <propriété name = "MineVictableIdleTimemillis" value = "300000" /> <! - SQL qui vérifie que la connexion est valide, différentes configurations de données sont différentes -> <propriété name = "validationQuery" value = "select 1" /> <! Value = "true" /> <! - Il est recommandé de le configurer comme vrai ici pour empêcher que la connexion récupérée ne soit indisponible -> <propriété name = "testonBorrow" value = "true" /> <propriété name = "testonborrow" value = "true" /> <propriété name = "testonreturn" value = "false" /> <! - Ouvrir pscache et spécifier la taille name = "poolPreparedStatements" value = "true" /> <propriété name = "maxpoolPreparedStatementPerConnectionSize" value = "20" /> <! - Configurez la méthode de soumission ici, la valeur par défaut est vraie, vous pouvez faire sans configuration -> <propriété name = "DefaultAutoMit" value = "stat" /> </ank> <! - 3. Configurez l'objet SqlSessionFactory de MyBatis -> <bean id = "SqlSessionFactory"> <! - Configurez MyBatis Global Configuration Fichier -> <propriété Name = "ConfigLocation" Value = "ClassPath: Mybatis-Cong.xml" /> <! - Dans DataRATAT Ref = "DataSource" /> <! - Scannez le fichier de configuration -> <propriété name = "MapperLocations" value = "classPath: Mapping / *. Xml" /> </Ean> <! - 4. Configurez le package d'interface Dao numérisé, implémentez dynamiquement l'interface DAO et injectez-la dans le récipient à ressort -> <Ean> <! name = "basepackage" value = "com.ray.dao" /> </ bean> </bans>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.springfrrameworkwork.org/schema/bans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <! - Automatic scan -> < name = "dataSource" ref = "dataSource" /> </ bean> <! - Activez la prise en charge de l'annotation pour le contrôle des transactions -> <tx: transaction-manager = "TransactionManager" /> </bans> annotation
Étape 4: Ajouter un fichier de configuration de journal
La configuration du journal est plus simple que Log4j, et les fonctions sont similaires
Ressources
│ ├── Logback.xml
Créez un nouveau fichier dans le dossier Ressources: logback.xml
<? xml version = "1.0" Encoding = "utf-8"?> <configuration debug = "true"> <appender name = "stdout"> <ecoder> <matter>% d {hh: mm: ss.sss} [% thread]% -5level% logger {36} -% msg% n </matter> </codeur> Imprimez les journaux dans la console -> <root niveau = "Debug"> <appender-ref ref = "stdout" /> </root> </fongucial>Étape 5: Configurer web.xml
web.xml
<web -pp 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/javaee/web-app_3_1. Version = "3.1" Metadata-Completet = "true"> <splay-name> Application Web créée par archétype </ Display-Name> <! - Configurez DispatcherServlet -> <Servlet> <Servlet-Name> Seckill-Dispatcher </vrevlet-name> <Servlet-Class> Org.springframework.web.Servlet.Dispatcherserservlet </ Servlett-Class> <! - être chargé pour Springmvc Spring-dao.xml, Spring-Service.xml, Spring-web.xml MyBatis -> Spring -> SpringMvc -> <Init-Param> <Amam-Name> ContextConfigLocation </ Param-Name> <Charot-on-startup> 1 </ Load-on-startup> <async-supporté> true </ async-supported> </ servlet> <servlet-mapping> <servlet-mapping> <servlet-name> seckill-dispatcher </ servlet-name> <! - Formez toutes les demandes par défaut -> <url-Pattern> / </ url-stattern> <filter> <filter-name> CaracterEncodingFilter </ Filter-Name> <Lilter-Class> org.springframework.web.filter.characterencodingfilter </filter-class> <itolin-param> <param-name> Encoding </ param-name> </ param-valu> utf-8 </ons param-onduue> <Lifter-Mapping> <Filter-Name> CaracterEncodingFilter </ Filter-Name> <URL-Pattern> / * </url-Pattern> </ Filter-Mapping> </low-App>
Structure du projet:
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.