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
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xmlns=" Http://java.sun.com/xml/ns/javaee " xsi:schemalocation="/HTTP/ Java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " 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/ Log4j.properties</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= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance " xmlns:context=" http://www.springframework.org/ Schema/context " xmlns:mvc=" http://www.springframework.org/ Schema/mvc " xsi:schemalocation=" Http://www.springframework.org/schema /mvc http://www.springframework.org/schema/mvc/ spring-mvc-4.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans-4.1.xsdhttp://www.springframework.org/schema/context http://wWw.springframework.org/schema/context/spring-context-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/jdbc.properties"/> <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 Jdbc.properties 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 "-//mybatis.org//dtd config 3.0//en" "HTTP// Mybatis.org/dtd/mybatis-3-config.dtd "><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.properties
Jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://127.0.0.1:3306/coursewarejdbc.username= Coursewarejdbc.password=courseware
log4j Properties File: Log4j.properties
Log4j.rootlogger=info,ca#trace mybatis--if You need log mybatis SQL Statements,add Interface (Mapper) at here.log4j.logger.com.zjc.dao.iusermapper=trace#log4j.logger.com.zjc.dao.icommentmapper=tracelog4j.appender.ca =org.apache.log4j.consoleappenderlog4j.appender.ca.layout= org.apache.log4j.patternlayoutlog4j.appender.ca.layout.conversionpattern=%d5p%c%x-%m%n
Directory structure:
650) this.width=650; "src=" http://img.blog.csdn.net/20151126151957044 "alt=" 20151126151957044 "/>
Code directory:
650) this.width=650; "src=" http://img.blog.csdn.net/20151126152030131 "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