This article is mainly to build some structures of the project, complete the necessary packages, resources and configuration files, and configure the server to see if there is a problem. Now let's study together!
1. Use maven to build the project
Add the dependencies we need in the pom file, including spring MVC, spring and mybatis, jackson dependency, mysql driver, druid connection pool, jsp, servlet dependency, etc.
2. Write web XML file
There are three main things to do in it:
2.1 register the dispatcher servlet of the central scheduler
This is needed by the spring MVC framework. The purpose is to: create a spring MVC container object to create a Controller class object; A Servlet is created to accept the user's request.
<!--Register central scheduler--> <servlet> <servlet-name>crmWeb</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>crmWeb</servlet-name> <url-pattern>*.do</url-pattern><!--there*.do Indicates that all do All requests at the end have this DispatcherServlet To handle--> </servlet-mapping>
2.2 register spring listener ContextLoaderListener
If there is a problem, watch the video directly (easy to understand and reliable): https://www.bilibili.com/vide...
This is used by spring. The purpose is to create spring container objects to create service, dao(dao is needed by Mybatis, but the SSM framework gives it to spring to manage together) and other objects.
<!--register spring Monitor for--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
2.3 register character set filtercharacterencoding filter
This is to solve the problem of garbled post requests.
<!--Register character set filter: solve the problem of garbled code--> <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> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> <!--Set here as/*Indicates that all resources must pass through this filter--> </filter-mapping>
3. Create the required package
Controller package, service, dao, entity class package, etc. Create the configuration file of the framework, including the configuration file of spring MVC, the configuration file of spring, the main configuration file of mybatis, the file connecting to the database, etc
4. Write the configuration files of spring MVC, spring and mybatis
4.1 spring MVC configuration file: declare controller and other web related objects
Including: component scanner, view parser, etc. There should also be: annotation driven (respond to ajax and return json. Solve the problem of static resource access)
<!-- Spring MVC configuration file, declaring controller and other web related objects -- >
<context:component-scan base-package="com.bjpowernode.crm.settings.web.controller" /> <!--There is no view parser. I plan to use the full path instead of this one--> <!--Annotation driven--> <mvc:annotation-driven />
4.2 spring configuration file: declare service, dao, tool class and other objects
Including: declaring the data source to connect to the database; Create SqlSessionFactory using SqlSessionFactoryBean; Declare the scanner of mybatis to create dao objects; The component scanner creates service objects, transaction configurations, etc.
<!--Declare data source and connect to database--> <context:property-placeholder location="classpath:jdbc.properties" /> <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.user}" /> <property name="password" value="${jdbc.password}" /> </bean> <!--SqlSessionFactoryBean establish SqlSessionFactory,mybatis To be used--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!--statement mybatis Scanner, creating dao object--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="com.bjpowernode.crm.settings.dao" /> </bean> <!--statement service Notes for@Service Package name and location--> <context:component-scan base-package="com.bjpowernode.crm.settings.service" /> <!--Declare tool class--> <context:component-scan base-package="com.bjpowernode.crm.utils" />
4.3mybatis master configuration file
In this file: set the alias and specify the location of mapper(sql mapping file).
<typeAliases> <package name="com.bjpowernode.crm.settings.domain"/><!--Set aliases, which are their corresponding class names--> </typeAliases> <mappers> <package name="com.bjpowernode.crm.settings.dao"/> <!--appoint mapper file location--> </mappers>
5. Write code
At the beginning, everything is related to user login. The following are similar to Userxxx
5.1 create dao interface and mapper file
Just create these two temporarily. There is no content in them for the time being. Write code in them when you need to do specific things.
5.2 create service interface and implementation class
The annotation @ Service is used to complete the object creation of the implementation class, and the attribute is Dao (add @ Autowired to complete the automatic injection of its value, which is byType injection by default).
@Service public class UserServiceImpl implements UserService { @Autowired //Automatic injection of reference type, byType by default. There can only be one such type private UserDao userDao; }
5.3 create controller class
Add @ Controller to class to complete object creation. Attribute Service (add @ Autowired above to complete the automatic injection of its value).
@Controller public class UserController { @Autowired private UserService userService; }
If a method is written later: add @ RequestMapping("/xxx.do") to the specific method to specify which request this method should process. When returning objects, I need to add @ ResponseBody annotation. When I made the first requirement, I didn't add this annotation, which led to the wrong path of the back end to the front end, and several identical files were repeated in the middle. Be sure to pay attention!!!
5.4 creating entity classes, etc
6. Configure the server
You can configure the server or something, and then start the server to see if the server can start normally! Usually no problem. Then use the browser to access the website~
Just come in ~
Well, I'll say goodbye to you here,