当前位置:网站首页>SSM framework integration
SSM framework integration
2022-07-27 05:11:00 【My code!】
The principle of integration

1. Create a maven-web engineering
Replace web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app> 2. Add dependency
<dependencies>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--mybatis rely on -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--mybatis and spring Integration dependency -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--mysql drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!--druid Connection pool dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--lombok rely on -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<!--jackson java Object to json object @ResponseBody-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
<!--servlet-api rely on -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!-- generator The generated code -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<!-- unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!-- Output log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>3. To write spring The configuration file
<!-- Package scanning -->
<context:component-scan base-package="com.aaa" />
<!-- Static resource release -->
<mvc:default-servlet-handler />
<!-- Open the annotation -->
<mvc:annotation-driven/>
<!-- Data source configuration -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<!-- Initialize the number of connections : Evaluate according to the concurrency of your project -->
<property name="initialSize" value="5"/>
<!-- The maximum number of connections -->
<property name="maxActive" value="10"/>
<!-- Waiting time In milliseconds -->
<property name="maxWait" value="3000"/>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- Set data source -->
<property name="dataSource" ref="dataSource" />
<!-- Set up mybatis Path to map file -->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- by dao Interface to generate proxy implementation classes -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.aaa.dao" />
</bean>4. To configure web.xml
<servlet>
<servlet-name>spring</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>
<!-- When tomcat Create at startup DipatcherServlet Default when accessing controller Path creation -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> To configure tomcat And start the 
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.15.RELEASE</version> </dependency>
If you start tomcat Problems occur when , So it could be spring Error in configuration file . Import dependence 5455
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>5. generator The generated code
(1) Import dependence
<!-- Code generator generator-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>(2) The configuration file
<generatorConfiguration>
<!-- Find you. mysql drive jar The location of -->
<classPathEntry location="E:\idea\repMaven\mysql\mysql-connector-java\8.0.29\mysql-connector-java-8.0.29.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- Remove comments -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- Configuration information of data source -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--java Configuration of entity class -->
<javaModelGenerator targetPackage="com.aaa.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- Mapping file configuration -->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--dao Configuration of data access layer -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.aaa.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- Mapping relationship between database tables and entities
schema: Database name
tableName: Table name
domainObjectName: Entity class name
enableUpdateByExample: Whether to generate complex modification operations
-->
<table schema="mybatis" tableName="tbl_user" domainObjectName="User"
enableUpdateByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false">
</table>
<table schema="mybatis" tableName="tb_stu" domainObjectName="Student"
enableUpdateByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false">
</table>
</context>
</generatorConfiguration>(3) test
public class Test01 {
@Test
public void test01() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}6. add to sql journal
Import these two dependencies
<!-- Output log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>边栏推荐
- 传智教育|软件测试工程师未来的发展方向有哪些?
- A math problem cost the chip giant $500million
- Counting Nodes in a Binary Search Tree
- [error reporting]: cannot read properties of undefined (reading 'prototype')
- 事件过滤器
- Raspberry pie output PWM wave to drive the steering gear
- Knapsack problem DP
- JVM上篇:内存与垃圾回收篇十四--垃圾回收器
- Dialog data transfer
- [search] - multi source BFS + minimum step model
猜你喜欢

Hiding skills of Photoshop clipping tool

Hiding skills of Photoshop clipping tool

How idea creates a groovy project (explain in detail with pictures and texts)

MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition

安装Pygame

一道数学题,让芯片巨头亏了5亿美金

2、 MySQL advanced

Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time

Differences among left join, inner join and right join

Svn usage details
随机推荐
接口和抽象类/方法学习 demo
Detailed explanation of mvcc and its principle
Could not autowire.No beans of ‘userMapper‘ type found.
一、MySQL基础
What if Photoshop prompts that the temporary storage disk is full? How to solve the problem that PS temporary storage disk is full?
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
Raspberry pie output PWM wave to drive the steering gear
MySQL download and installation & perfect uninstall
How to sinicize the JMeter interface?
Standard dialog qmessagebox
Counting Nodes in a Binary Search Tree
Sub database and sub table
SVN使用详解
1、 MySQL Foundation
Installation and template setting of integrated development environment pychar
Translation of robot and precise vehicle localization based on multi sensor fusion in diverse city scenes
对话框简介
抽卡程序模拟
Complete Binary Tree
Svn usage details