Thin Web Development configuration file

Source: Internet
Author: User

As a beginner of web development, it is often known that the configuration files of various frameworks are unknown. This is because many of the configuration files can actually be used in many ways to achieve the same purpose. The recent study of Spring's integrated MyBatis project has finally been able to approximate these doorways.

The field of web development has always admired the MVC pattern, so there is a combo framework like SSH. But I always feel that ssh is a little bloated, even if only with the Spring MVC module, I feel that I did not write a servlet to understand directly, so the use of a more concise spring+mybatis+servlet structure. The MyBatis is a lightweight ORM framework. Unless you go back to the original way of writing JDBC SQL statements, this complexity is still needed, and the MyBatis mapping statement is SQL, and the learning curve is not high.

Originally spring can not be used, but the table more, DAO interface more, manual management is also a bit troublesome. So you can manage it with spring, at least the XML file will write a lot less.

As a persistent pursuer of "concise" transactions, I have streamlined these profiles to the shortest, and the extra text is just explanations.


<?xml version= "1.0"  encoding= "UTF-8"? ><web-app xmlns:xsi= " Xmlschema-instance " xmlns=" Http:// " xsi:schemalocation="/HTTP/ " id=" Courseware "  version= "2.5" >  <!--  standard servlet definition  -->  <servlet>     <servlet-name>CheckName</servlet-name>    <servlet-class> com.zjc.servlet.checkname</servlet-class>  </servlet>    <!--   When you use a bean in a spring context in a servlet, you need to write a springservlet as an intermediary. These servlets point to this intermediary. Of course, these servlets are also registered in spring's configuration file  -->  <servlet>    <servlet-name >initservlet</servlet-name>    <servlet-class>com.zjc.servlet.springservlet </servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet>    <servlet-name >test</servlet-name>    <servlet-class>com.zjc.servlet.springservlet</ servlet-class>  </servlet>    <!--  Specify spring configuration files and settings for monitoring, Load sping frame at startup  -->  <context-param>    <param-name> contextconfiglocation</param-name>    <param-value>web-inf/config/ Application-context.xml</param-value>  </context-param>  <listener>     <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class>  </listener>    <!--  Specify log4j Property file location and settings for monitoring, Start log4j -->  <context-param>         when spring starts   <param-name>log4jconfiglocation</param-name>          <param-value>web-inf/config/</param-value>    </context-param>  <listener>     <listener-class>org.springframework.web.util.log4jconfiglistener</ listener-class>  </listener>    <!--  Use spring's character encoding filter to reduce the probability of garbled characters  -->  <filter>      <filter-name>encodingfilter </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>      <iNit-param>       <param-name>forceencoding</param-name>        <param-value>true</param-value>     </init-param>  </filter>  <filter-mapping>       <filter-name>encodingfilter</filter-name>      <url-pattern >/*</url-pattern>  </filter-mapping>     <!--  Servlet mapping  -->  <servlet-mapping>    <servlet-name>checkname </servlet-name>    <url-pattern>/CheckName</url-pattern>  < /servlet-mapping>  <servlet-mapping>    <servlet-name>test</ servlet-name>    <url-pattern>/test</url-pattern>  </ Servlet-mapping><welcome-filE-list><welcome-file>index.html</welcome-file></welcome-file-list></web-app> 

spring configuration file: Application-context.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= " Schema/beans "        xmlns:xsi=" Xmlschema-instance "       xmlns:context=" Schema/context "        xmlns:mvc=" Schema/mvc "       xsi:schemalocation=" Http:// /mvc spring-mvc-4.1.xsd         spring-beans-4.1.xsd         ">                     <!--  Reference properties file, It can also be written directly here without the property file. But the individual thinks that the attribute file is still necessary, the direct modification this file is not easy to understand  -->    <context:property-placeholder location = "Web-inf/config/"/>        <bean id= " DataSource " class=" Org.springframework.jdbc.datasource.DriverManagerDataSource ">     <property name= "Driverclassname"  value= "${jdbc.driver}"/>    < Property name= "url"  value= "${jdbc.url}"/>    <property name= "username " value=" ${jdbc.username} "/>    <property name=" password " value=" ${ Jdbc.password} "/>    </bean>        < Bean id= "SqlsessionfaCtory " class=" Org.mybatis.spring.SqlSessionFactoryBean ">    <!--  reference above data source  -->    <property name= "DataSource"  ref= "DataSource"/>     <!--  Specify mybatis core configuration file   -->     <property name = "Configlocation"  value= "Web-inf/config/mybatis.xml"/>    <!--  Specify all mapper mapping files  *.xml location, automatically find  -->    <property name= "Mapperlocations"  value= "Classpath*:com/zjc/dao/*.xml"/>    </bean>         <!--  Auto-scan Interface (mapper) file to generate the proxy class object and add it to the spring context.     !bean id The default initial lowercase, i.e. the interface name is a single initial capitalization, is automatically converted to the first letter lowercase and automatically associated with the mapping statement in *.xml. is independent of the file name of the XML. @repository annotations can be added before the interface  -->    <bean class= " Org.mybatis.spring.mapper.MapperScannerConfigurer ">    <property name="Basepackage"  value= "Com.zjc.dao"/>        <!--  It would have been logical to add a reference to the Sqlsessionfactory property here, but it would have caused the reference value to be incorrect if added. In the case where there is only one data source, it is not wrong to add the sentence, so it is omitted.      ! Add this sentence: Property name= "Annotationclass"  value= " Org.springframework.stereotype.Repository "  indicates that only the interfaces that are annotated by @repository are scanned, not all of them are scanned, so they are omitted. -->    </bean>    <!--with the above automatic scanning, there is no need to manually define the Mapper bean , the following example is a manually defined template, which does not require      <bean id= "Usermapper"  class= "Com.zjc.bean.User" ><property name= "Mapperinterface"  value= "Com.zjc.dao.IUserMapper"/><property name= " Sqlsessionfactory " ref=" Sqlsessionfactory "/>     -->         <!--  Using spring transaction management  -->    <bean id= " TransactionManager " class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager ">    <property name=" DataSource " ref=" DataSource "/>     </bean>        <!--  Define the servlet in spring, consistent with the content in Web. XML, and, of course, if Mybatis bean is used, it should be injected as an attribute  -->    < Bean id= "Initservlet"  class= "Com.zjc.servlet.InitServlet"/>    <bean  id= "Test"  class= "com.zjc.test.UserAccess" >    <property name= "um"  ref = "Usermapper"/>    <!--  This is referred to as the Bean name in the spring context. So it can't be an interface name like Com.zjc.dao.UserMapper  -->    </bean></beans>

mybatis configuration file: Mybatis.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><! doctype configuration public  "-// config 3.0//en"   "HTTP// "><configuration><!--  Notification MyBatis, what is the log system  -->< Settings>    <setting name= "Logimpl"  value= "LOG4J"/>     </settings>        <!--  Defines the alias used by MyBatis, the mapping file *. The names used in XML depend on this  --><typeAliases><!-- <typealias alias= "User"  type= " Com.zjc.bean.User "/>   Here is a manual definition of the sample. More commonly used is the automatic scanning method--><package name= "Com.zjc.bean"/></typealiases><!--  when using spring automatic scanning, It is necessary to turn off this manually created mapping, otherwise the conflict     <mappers>    <mapper resource= " Com/zjc/bean/user.xml "/>    </mappers>     --></ Configuration>

JDBC Properties File:

Jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql:// Coursewarejdbc.password=courseware

log4j Properties File:

Log4j.rootlogger=info,ca#trace mybatis--if You need log mybatis SQL Statements,add Interface (Mapper) at

Directory structure:

650) this.width=650; "src=" "alt=" 20151126151957044 "/>

Code directory:

650) this.width=650; "src=" "alt=" 20151126152030131 "/>

changed a bit, but it's still old. The interface name should be usermapper. Java

This article from the "Empty" blog, reproduced please contact the author!

Thin Web Development configuration file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.