SSM learning notes - (spring MVC + Spring + mybatis) configuration (eclipse)

I've heard the teacher said that the whole family bucket of spring framework has never known what it is. I read a lot wi...

I've heard the teacher said that the whole family bucket of spring framework has never known what it is. I read a lot with a sense of mystery and didn't really understand it. I simply learned the configuration of SSM framework by taking the opportunity of practical training. Spring, as a novice of Xiaobai, really needs to learn how to do it step by step. I don't know which folder to put the code on the po directly, so Xiaobai Ben People should record the configuration process in detail.

(eclipse based)

Create a normal web project (check it below to make the core web file web.xml)

In webcontext - > Web inf - > lib, you need to import the relevant jar package

There are 42 of them. Xiaobai has a lot of jar packages that he doesn 't know what to use, so he just imports them all (don' t spray them on me)

Next is the configuration:

The so-called configuration is actually to write a good. xml file. When you start to get it, you will find that there are a lot of xml files. Which one is the corresponding xml file of SSM

  • SpringMVC------------springmvc.xml
  • mybatis-------------mybatis.xml
  • spring-------------applicationConnContext.xml

The basic content of these configuration files is that we don't need to copy and paste the past directly (there are previous templates, I will paste out the code later, just create the file and copy the code by myself)

Just paste it into src. Here I import 5 files in total, as well as the file configuration and log configuration of a database

You can see that I have created many packages, which will be discussed later

Code for profile

1. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!-- Configuration read db.properties configuration file --> <context:property-placeholder location="classpath:db.properties"/> <!-- Configure scan Service --> <context:component-scan base-package="com.situ.ssm.service"/> <!-- ******************Database related configuration************************ --> <!-- Database related configuration --> <!-- Configure data sources --> <bean id="dataSource"> <property name="driverClass" value="$"/> <property name="jdbcUrl" value="$"/> <property name="user" value="$"/> <property name="password" value="$"/> </bean> <!-- to configure SqlSessionFactory alone MyBaits I need to go by myself new this SqlSessionFactory, //Now, after combining with Spring, we will give it to the Spring container to help us create a single instance of the default bean. --> <bean id="sqlSessionFactory"> <!-- load MyBatis Core profile --> <property name="configLocation" value="classpath:mybatis.xml"/> <!-- Configure data sources --> <property name="dataSource" ref="dataSource"/> </bean> <!-- to configure Mapper scanning --> <!-- MapperScannerConfigurer:mapper Scanner, put the mapper Interface //Automatically create proxy objects, automatically put them into Spring containers, bean s //The id of mapper is the class name of mapper (lowercase) -- > <bean> <!-- to configure Mapper Scan package --> <property name="basePackage" value="com.situ.ssm.dao"/> </bean> <!-- ********************Things related configuration********************** --> <!-- Things Manager --> <bean id="transactionManager"> <!-- data source, The operation of things is related to the database, because there is an exception in the middle, //Database rollback data is required. > <property name="dataSource" ref="dataSource"/> </bean> <!-- notice --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- Communication behavior --> <tx:method name="*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="query*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- section --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.situ.ssm.service.impl.*.*(..))"/> </aop:config> </beans>

2. 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> <!-- Define type alias --> <typeAliases> <!-- Scan the classes in the package. Batch aliases. Aliases are class names. Aliases are not case sensitive --> <package name="com.situ.ssm.entity"/> <!-- The writing specification of package name will not be explained in detail here --> </typeAliases> </configuration>

3.springmvc.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/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <! -- scan @ Controller is the package to scan annotation -- > <context:component-scan base-package="com.situ.ssm.controller"/> <! -- Annotation driven -- > <mvc:annotation-driven/> <! -- View resolver If the logical name of the view is written in the Controller, the view parser must be configured. Prefix + view logical name + suffix = real path If the Controller writes the real name of the view, the view parser does not need to be configured --> <bean> <! -- path prefix -- > < property name = "prefix" value = "/ WEB-INF / JSP /" > <! -- path suffix -- > <property name="suffix" value=".jsp"></property> </bean> </beans>

The other two will be pasted in by the way

log4j.properties

# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

db.properties

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ **(database name)? characterEncoding=UTF-8 jdbc.username=root jdbc.password=**(Database password)

These are just framework documents, and what works in the end is web.xml So we need to modify web.xml The configuration of files (generated automatically when the project is created), which is the core of the whole project

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SSMtest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- Configure listener loading Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- load Spring Core profile --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- solve POST Random code problem --> <filter> <filter-name>characterEncoding</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> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- to configure DispatcherServlet(Front end controller (scheduler)) --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- classpath:representative src This path --> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- All action All requests at the end are given to DispatcherServlet handle --> <!-- /student.action /user.action --> <url-pattern>*.action</url-pattern> <!-- Block all action Ending --> </servlet-mapping> </web-app>

The basic work is almost finished. The next step is to build the package

Entity, controller, service, dao

Details:

To really understand, first look at a picture

23 June 2020, 04:20 | Views: 2858

Add new comment

For adding a comment, please log in
or create account

0 comments