当前位置:网站首页>Integration of SSM
Integration of SSM
2022-07-24 05:22:00 【Wang Jiujiu】
ssm Integration of , the truth is that spring and mybatis Integration of , hold mybatis The configuration in is consolidated into spring in spring Should be encapsulated in a corresponding class , To encapsulate mybatis These configurations
step :
1. Create a maven-web engineering
Create good , Create your own java and resources
Be careful : Be sure to replace web.xml file , The built-in version is too low
<?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 related dependencies
<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>repMaven.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>3.1.0</version>
</dependency>
<dependency>
<groupId>repMaven.org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.12</version>
</dependency>
<!-- Code generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.1</version>
</dependency>
<!-- Log generator -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>repMaven.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>3. To write spring Configuration file for
<!-- About springMVC Configuration of -->
<!-- Package scanning -->
<context:component-scan base-package="qy151.guan"/>
<!-- Open the annotation -->
<mvc:annotation-driven/>
<!-- Release of static resources -->
<mvc:default-servlet-handler/>
<!-- Interceptor -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user"/>
<!-- <mvc:exclude-mapping path="/login.jsp"/>-->
<mvc:exclude-mapping path="/css/**"/>
<mvc:exclude-mapping path="/js/**"/>
<bean class="qy151.guan.filter.Interceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- About spring Configuration of -->
<!-- Configuration of data source -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/five?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="grt081141"/>
<!-- Initial value -->
<property name="initialSize" value="5"/>
<!-- minimum value -->
<property name="minIdle" value="5"/>
<!-- Maximum -->
<property name="maxActive" value="10"/>
<!-- Maximum waiting time -->
<property name="maxWait" value="3000"/>
</bean>
<!--SqlSessionFactory Integration of -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- Be careful ref If the above data source configuration id Agreement -->
<property name="dataSource" ref="ds"/>
<!-- Set up mybatis Path to map file -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- by dao Generate proxy class basePackage: Package name -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="qy151.guan.dao"/>
</bean>4. To configure web.xml
<?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">
<!-- Coding filter -->
<filter>
<filter-name>b</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>b</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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:spring.xml</param-value>
</init-param>
<!-- When Tomcat Startup time , establish DispatcherServlet Default controller Path time -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>5.generator generator
1. Introduce dependencies , If dependency is introduced , It seems that there is no need to introduce
2. Import profile
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- Find yourself mysql affirmatory jar package -->
<classPathEntry location="D:\mysql-connector-java-8.0.20.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- Configuration information of data source -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/five?serverTimezone=Asia/Shanghai
&characterEncoding=UTF-8&nullCatalogMeansCurrent=true"
userId="root"
password="grt081141">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- Configuration of entity class
targetPackage: The name of the bag
targetProject: The path of the package
-->
<javaModelGenerator targetPackage="qy151.guan.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="qy151.guan.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- Mapping relationship between database and entity class -->
<table schema="five" tableName="student" domainObjectName="Student" enableUpdateByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false">
</table>
</context>
</generatorConfiguration>3. test
package qy151.guan.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* TODO
*
* @author lenovo
* @version 1.0
* @since 2022-06-14 16:31:43
*/
public class GeneratorTest {
public static void main(String[] args)throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
<!-- To write yourself generator The name of the file -->
File configFile = new File("generator02.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. Code
Add, delete, modify and check the back-end code controller
package qy151.guan.controllor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qy151.guan.entity.Student;
import qy151.guan.service.StudentService;
import qy151.guan.until.CommonResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
* TODO
*
* @author lenovo
* @version 1.0
* @since 2022-06-14 20:34:20
*/
@RestController
public class StudentController {
// Query all
@Autowired
private StudentService studentService;
@RequestMapping("student")
public CommonResult all(){
List<Student> all = studentService.All();
return new CommonResult(2000," The query is successful ",all);
}
// according to id Inquire about
@Autowired
private StudentService studentServiceId;
@RequestMapping("studentId")
public CommonResult getId(Integer id){
int removeId = studentServiceId.removeId(id);
return new CommonResult(2000," Delete successful ",null);
}
// modify
@Autowired
private StudentService studentServiceUpdate;
@RequestMapping("studentUpdate")
public CommonResult update(Student row){
int amend = studentServiceUpdate.update(row);
return new CommonResult(2000," Modification successful ",null);
}
// add to
@Autowired
private StudentService studentServiceAdd;
@RequestMapping("studentAdd")
public CommonResult addId(Student row){
int add = studentServiceAdd.add(row);
return new CommonResult(2000," Add success ",null);
}
}
service
package qy151.guan.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qy151.guan.dao.StudentMapper;
import qy151.guan.entity.Student;
import qy151.guan.service.StudentService;
import java.util.List;
/**
* TODO
*
* @author lenovo
* @version 1.0
* @since 2022-06-14 20:28:55
*/
@Service
public class StudentServiceImpl implements StudentService {
// Query all
@Autowired
private StudentMapper studentMapper;
public List<Student> All() {
List<Student> students = studentMapper.queryAll();
return students;
}
// Delete
@Autowired
private StudentMapper studentMapperId;
public int removeId(Integer id) {
int i = studentMapperId.deleteByPrimaryKey(id);
return i;
}
// modify
@Autowired
private StudentMapper studentMapperUpdate;
public int update(Student row) {
int update = studentMapperUpdate.updateByPrimaryKeySelective(row);
return update;
}
// add to
@Autowired
private StudentMapper studentMapperAdd;
public int add(Student row) {
int add = studentMapperAdd.insertSelective(row);
return add;
}
}
package qy151.guan.service;
import qy151.guan.entity.Student;
import java.util.List;
/**
* TODO
*
* @author lenovo
* @version 1.0
* @since 2022-06-14 20:25:40
*/
public interface StudentService {
// Query all
public List<Student> All();
// according to id Delete
public int removeId(Integer id);
// modify
public int update(Student row);
// add to
public int add(Student row);
}
边栏推荐
- Ren Xudong, chief open source liaison officer of Huawei: deeply cultivate basic software open source and jointly build the root technology of the digital world
- 【常用技巧】
- finally和return的执行顺序
- 【深度学习】(三)图像分类
- DNS domain name resolution service
- Do you want to have a robot that can make cartoon avatars in three steps?
- Binary SCA fingerprint extraction black Technology: go language Reverse Technology
- 明星逆市入局的NFT,如何能走出独立行情?
- Update C language notes
- [Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket
猜你喜欢
随机推荐
【【【递归】】】
Read the summary of "machine learning - Zhou Zhihua"
scikit-learn笔记
Image to image translation with conditional advantageous networks paper notes
Pointer learning diary (V) classic abstract data types and standard function libraries
Support complex T4 file systems such as model group monitoring and real-time alarm. e
MySQL深入了解
Knowledge record of College Physics C in advance in summer [update]
c2-随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习+转载整理
NFS shared services
AiN 0722 签到
Tips for using the built-in variable props of BeanShell
酒店IPTV数字电视系统解决方案
Learning some contents of vector and iterator
Drools development decision table
web开发
MySQL connection
Pointer learning diary (III)
C primer plus learning notes - 6. Arrays and pointers
scikit-learn笔记









