当前位置:网站首页>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 .
边栏推荐
- 初探元宇宙存储,数据存储市场下一个爆点?
- [North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss
- 【世界海洋日】TcaplusDB号召你一同保护海洋生物多样性
- 为什么信息化 ≠ 数字化?终于有人讲明白了
- 设备监理师证书含金量怎样?值得考吗?
- Requirements analysis specification and requirements specification
- Different binary search trees [bottom-up backtracking spanning tree + memory search -- space for time]
- 无法定位程序输入点 [email protected]
- 如何理解MySQL的索引?
- Mobaihe box, ZTE box, Migu box, Huawei box, Huawei Yuehe box, Fiberhome box, Skyworth box, Tianyi box and other operators' box firmware collection and sharing
猜你喜欢

20款IDEA 神级插件 效率提升 30 倍,写代码必备
![[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

go-redsync分布式锁源码解析

seekbar 自定义图片上下左右显示不全 / bitmapToDrawable / bitmapToDrawable互转 / paddingStart/paddingEnd /thumbOffset

FPGA (VIII) RTL code IV (basic circuit design 1)

迅为龙芯开发板pmon下Ejtag-设置硬件断点指令

Yyds dry inventory difference between bazel and gradle tools

問題——adb shellerror: insufficient permissions for device: verify udev rules.

嵌入式开发如何进行源代码保密工作

19.03 vessel description and simple application examples continued
随机推荐
目前市面上增额终身寿险利率最高的产品是哪个?
【资料上新】基于3568开发板的NPU开发资料全面升级
VIM configuration and use
Laravel, execute PHP artist migrate and report an error alter table `users`add unique `users_ email_ unique`(`email`))
2022-2028 global MWIR camera industry research and trend analysis report
【TcaplusDB知识库】修改业务修改集群cluster
一个注解优雅的实现 接口数据脱敏
2022-2028 global CAE engineering service industry research and trend analysis report
迅为龙芯开发板pmon下Ejtag-设置硬件断点指令
FarrowTech的无线传感器采用橙群微电子的NanoBeacon蓝牙信标技术
2022-2028 global pneumatic test probe industry survey and trend analysis report
Etcd教程 — 第六章 Etcd之核心API V3
Tupu software intelligent energy integrated management and control platform
87.(cesium篇)cesium热力图(贴地形)
How to understand MySQL indexes?
[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions
[tcapulusdb knowledge base] tcapulusdb technical support introduction
What is the gold content of the equipment supervisor certificate? Is it worth it?
2022-2028 global bubble CPAP system industry survey and trend analysis report
Access 500 error after modstart migrates the environment
