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 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=""
    xmlns:context="" xmlns:p=""
    xmlns:aop="" xmlns:tx=""
	<!-- Configuration read configuration file -->
	<context:property-placeholder location=""/>
	<!-- Configure scan Service -->
	<context:component-scan base-package="com.situ.ssm.service"/>
	<!-- ******************Database related configuration************************ -->
	<!-- Database related configuration -->
	<!-- Configure data sources -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	<!-- 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" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- load MyBatis Core profile -->
		<property name="configLocation" value="classpath:mybatis.xml"/>
		<!-- Configure data sources -->
		<property name="dataSource" ref="dataSource"/>
	<!-- 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 class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- to configure Mapper Scan package -->
		<property name="basePackage" value="com.situ.ssm.dao"/>
	<!-- ********************Things related configuration********************** -->
	<!-- Things Manager -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 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"/>
	<!-- notice -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
			<!-- 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" />
	<!-- section -->
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.situ.ssm.service.impl.*.*(..))"/>

2.  mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-// Config 3.0//EN"
	<!-- Define type alias -->
		<!-- 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 -->


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:p=""

    <! -- scan @ Controller is the package to scan annotation -- >
	<context:component-scan base-package="com.situ.ssm.controller"/>
	<! -- 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 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<! -- path prefix -- >
		< property name = "prefix" value = "/ WEB-INF / JSP /" >
		<! -- path suffix -- >
		<property name="suffix" value=".jsp"></property>

The other two will be pasted in by the way

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

jdbc.url=jdbc:mysql://localhost:3306/ **(database name)? characterEncoding=UTF-8
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="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.1">
  <!-- Configure listener loading Spring -->
	<!-- load Spring Core profile -->

	<!-- solve POST Random code problem -->

	<!-- to configure DispatcherServlet(Front end controller (scheduler)) -->
			<!-- classpath:representative src This path -->
		<!-- All action All requests at the end are given to DispatcherServlet handle -->
		<!-- /student.action /user.action -->
		<!-- Block all action Ending -->

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

Entity, controller, service, dao


To really understand, first look at a picture

Tags: xml Spring JDBC Mybatis

Posted on Tue, 23 Jun 2020 04:20:34 -0400 by galewis