当前位置:网站首页>Preliminary construction of SSM project environment
Preliminary construction of SSM project environment
2022-06-29 03:33:00 【_ Xiao Xu_】
ssm First time to know
ssm The frame is spring,spring web mvc and mybatis The combination of , In the building ssm The framework does not need to have the foundation of the three frameworks .
You also need to master the following technical specifications :
development environment :
- Install and configure jdk8 above ( Configure environment variables )
- Install and configure mysql8.0 above
- download tomcat,maven middleware
- Java Integrated development environment ide
Maven Configure central warehouse :

Maven Configure local warehouse :
IDEA To configure maven:

New project directory :
change pom To configure :
build ssm frame
pom Import required dependencies :
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.19.RELEASE</version>
</dependency>
<!--jdbc Of orm Used to integrate other persistence layer frameworks -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.19.RELEASE</version>
</dependency>
<!--spring The default tool for parsing data , Import is ready to use without configuration -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--mybatis And spring combination -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Druid connection pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
To configure datasource(db.properties):
Use druid The connection pool and datasource, Not default mybatis Of .
druid.driver=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/db?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
druid.username=root
druid.password=root
# Connection pool
druid.pool.init=1
druid.pool.minIdle=3
druid.pool.maxActive=20
druid.pool.timeout=30000
Whereas spring Of Aop The characteristics of everything bean Can be handed over to Ioc Container management ,DataSource Objects will also be handed over to spring Container management , No need to mybatis The configuration file of .
This file configures the parameters related to the database connection .
To configure mybatis Configuration file for (mybatis-config.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>
<!--datasource Leave it to spring Container management does not need to be imported -->
</configuration>
To configure
mybatis-config.xmlThe meaning of is to generate aSqlSessionFactoryAnd then through mybatis-spring Leave it to IoC Container management .
To configure spring Configuration file for (spring-config.xml):
spring The main function of the configuration file is to enable annotation driven .
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/context/spring-mvc.xsd">
<!-- Annotation development -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.ssm"></context:component-scan>
<!-- Turn on mvc Annotation scan -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
spring And mybatis combination
Previous pair spring Configured , After that, we need to spring And mybatis combination , Need to use mybatis-spring The dependency package of .
Specific configuration :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Import configuration information -->
<context:property-placeholder location="classpath:druid.properties"/>
<!-- be based on IoC The container creates a data source DataResource -->
<bean id="druidDataResource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${druid.driver}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="initialSize" value="${druid.pool.init}"/>
<property name="minIdle" value="${druid.pool.minIdle}"/>
<property name="maxActive" value="${druid.pool.maxActive}"/>
<property name="maxWait" value="${druid.pool.timeout}"/>
</bean>
<!-- The assignment expression will automatically assign the imported configuration information to the corresponding attribute by name -->
<!-- production mybatis-spring Provided SqlSessionFactoryBean receive mybatis Of SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- SqlSessionFactory You need to configure Mapper and DataResource -->
<property name="dataSource" ref="druidDataResource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- Query result mapping POJO,typeAliasesPackage Make the first letter of the interface lower case id, adopt id obtain -->
<property name="typeAliasesPackage" value="cms.ssm.model"/>
<!-- load mybatis-config.xml The file is used to create SqlSessionFactory -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- scanning dao Interface , And name it and submit it to IoC Container management , Easy java Get in code -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="cms.ssm.dao"/>
</bean>
<!-- Declarative transaction management , Configure transaction manager -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="druidDataResource"/>
</bean>
<!-- Enable transaction annotation scanning -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
The first step is to import the information of the Druid connection pool and the database connection information ; The second part configures through the imported link information DataSource Of bean object , Hand over to IoC Container management ; The third part introduces mybatis The configuration file , Create... From a configuration file SqlSessionFactory Object and handed over to IoC Container management ( The object has mybatis Create a configuration file for , The original file does not require any configuration , stay bean You can configure properties in the initialization object of ); Step four ,BolgMapper The creation of ,SqlSessionFactory The main function of is for each mapper Create a query channel , That is to say SqlSession Objects are dynamically created and destroyed ,
MapperScannerConfigurerObject manages this process for us , Dynamically create and destroy .
To configure Dispachterservlet
dispacherservlet The configuration of is an important part ,spring mvc The core of DispatcherServlet This is a general servlet Control several controllers , The annotation of the annotation controller will be annotated by DispatcherServlet To control .
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- The server starts loading web.xml file contextConfigLocation Used for loading spring Related configuration files -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<!--classpath It maps to resources Under the table of contents ,file Mapped to the root directory spring-* Identify and load all files starting with this -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Processing of static resources -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/calender/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
DispatcherServlet yes spring mvc The core , It is the entrance of each controller .
The interface test
After the above configuration , Each controller is controlled by DispatcherServlet forward , Write controller test mvc Configuration :
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping(value = "/test")
public String method1(){
return "hello world";
}
}
If you enter the correct url There is hello world Indicates that the controller configuration is complete .
Database connection interface test
The back-end data ultimately comes from the database , Use mysql For example , You've created properties file , The database is configured ,spring-mybatis Yes SqlSessionFactory To configure , Therefore, you can query the database directly .
Interface for querying database :
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class TestController {
@Resource
private TestMapper testMapper;
@GetMapping(value = "/test")
public String method1(){
return "hello world";
}
@GetMapping(value = "/mybatis")
public List<User> users(){
List<User> method = testMapper.method();
return method;
}
}
Configured with jackson, Will automatically java bean or java The basic data type of is converted to json type .
maper Mapping and normal mvc Model as ,mapper Unwanted @Coponent annotation , because spring-mybatis Already configured mapper Of scanning bean Will bring all mapper Inject after scanning IoC; Corresponding java Bean There is no need for comments , Because of the configuration SqlSessionFactory Will scan the java bean All into the IoC Containers .
This error was encountered during development :
Because in spring-mybatis Configuration of the , Three bean Is initialized bean contain DataSource,SqlSessionFactory,MapperScannerConfigurer( scanning mapper Of bean, So you don't have to use it every time @Component Annotations for dependency injection ).
The above error is Mapper Scanned bag , To configure MapperScannerConfigurer The scanning position of does not correspond to the actual position , You can modify the corresponding .
Start the server after the interface is written , The basic configuration is completed when the corresponding data is queried :
On the basis of spring mvc The architecture of is built , image AOP,TX Self access to information about things , To configure .
边栏推荐
- Ugui slider minimum control
- Shell script to count files, then remove oldest files
- Sequence traversal of binary tree ii[one of sequence traversal methods - > recursive traversal + level]
- [North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss
- 无法定位程序输入点 [email protected]
- Différents arbres de recherche binaires [arbre de génération rétrospectif ascendant + recherche de mémoire - - espace - temps]
- Grafana入门教程
- 2022-2028 global bubble CPAP system industry survey and trend analysis report
- FarrowTech的无线传感器采用橙群微电子的NanoBeacon蓝牙信标技术
- Etcd教程 — 第七章 Etcd之事务API
猜你喜欢

88.(cesium篇)cesium聚合图

Deeply analyzing the business logic of "chain 2+1" mode

分布式id解决方案

Grafana Getting Started tutorial
![[World Ocean Day] tcapulusdb calls on you to protect marine biodiversity together](/img/87/373af42f3a2ffa6b9f7fb0c0c3735b.png)
[World Ocean Day] tcapulusdb calls on you to protect marine biodiversity together
![[flutter topic] 66 diagram basic constraints box (I) yyds dry goods inventory](/img/8b/55a43383e1cb6b37231dba980c7b11.jpg)
[flutter topic] 66 diagram basic constraints box (I) yyds dry goods inventory

【TcaplusDB知识库】查看tcapdir目录服务器

【TcaplusDB知识库】修改业务修改集群cluster

Open source demo| you draw and I guess -- make your life more interesting

【TcaplusDB知识库】批量复制游戏区
随机推荐
2022-2028 global long wave infrared camera and camera core industry research and trend analysis report
An internal error occurred during: 'Retrieving archetypes:'.
【TcaplusDB知识库】修改业务修改集群cluster
【TcaplusDB知识库】TcaplusDB-tcaplusadmin工具介绍
Jerry's monitoring alarm clock [chapter]
Différents arbres de recherche binaires [arbre de génération rétrospectif ascendant + recherche de mémoire - - espace - temps]
不同的二叉搜索樹[自下而上回溯生成樹+記憶搜索--空間換時間]
[dynamic planning] change exchange
An internal error occurred during: 'Retrieving archetypes:'.
MATALB signal processing - signal transformation (7)
Laravel v. about laravel using the pagoda panel to connect to the cloud database (MySQL)
2022-2028 global CAE engineering service industry research and trend analysis report
Requirements analysis specification and requirements specification
[Ubuntu] [MySQL] Ubuntu installs mysql, but the compilation error is mysql h: No such file or directory
Restore the binary search tree [simulate according to the meaning of the question - > find the problem - > analyze the problem - > see the bidding]
Yyds dry inventory everything a primary developer should know about activity
Want to be an equipment manager? If you meet these three conditions, you can
Installation and deployment of sw-x framework
Different binary search trees [bottom-up backtracking spanning tree + memory search -- space for time]
Laravel, execute PHP artist migrate and report an error alter table `users`add unique `users_ email_ unique`(`email`))
