Spring+SprinMVC+MyBatis配置方式简易模板代码Demo GitHub访问 ssm-tpl-cfg
一、SQL数据准备
创建数据库test,执行下方SQL创建表ssm-tpl-cfg
/*
Navicat Premium Data Transfer
Source Server : 127.0.0.1
Source Server Type : MySQL
Source Server Version : 80030
Source Host : 127.0.0.1:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80030
File Encoding : 65001
Date: 17/10/2022 00:52:07
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for ssm-tpl-cfg
-- ----------------------------
DROP TABLE IF EXISTS `ssm-tpl-cfg`;
CREATE TABLE `ssm-tpl-cfg` (
`id` bigint NOT NULL COMMENT '主键编号',
`name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '测试名称',
PRIMARY KEY (`id`)
) COMMENT '初始SSM表结构数据' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- ----------------------------
-- Records of ssm-tpl-cfg
-- ----------------------------
BEGIN;
INSERT INTO `ssm-tpl-cfg` (`id`, `name`) VALUES (2210162246100000, '王飞飞');
INSERT INTO `ssm-tpl-cfg` (`id`, `name`) VALUES (2210162257100000, '练卓神');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
二、代码实现
2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tpl.ssm.cfg</groupId>
<artifactId>ssm-tpl-cfg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring_version>5.1.18.RELEASE</spring_version>
<jackson_version>2.9.7</jackson_version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring_version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.2 web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!-- 加载Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>
<init-param>
<param-name>characterEncodingFilter</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.3 resources/config/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!-- 加载数据库属性文件 -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 四大连接参数 -->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 连接池配置信息 -->
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxWait" value="${jdbc.maxWait}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- Druid用来测试连接是否可用的SQL语句,默认值每种数据库都不相同-->
<property name="validationQuery" value="SELECT 'x'" />
<!-- 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除. -->
<property name="testWhileIdle" value="true" />
<!-- 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 -->
<property name="testOnBorrow" value="false" />
<!-- 指明是否在归还到池中前进行检验 -->
<property name="testOnReturn" value="false" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="wall,stat" />
</bean>
<!-- 配置sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- mapper文件位置 -->
<property name="mapperLocations" value="classpath:mapper/*DAO.xml"/>
<!-- mybatis核心配置文件位置 -->
<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
</bean>
<!-- 配置扫描mapper生成代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tpl.ssm.cfg.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 扫描service -->
<context:component-scan base-package="com.tpl.ssm.cfg.service"/>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2.4 springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc/spring-mvc.xsd">
<!-- 开启springMVC注解驱动 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.tpl.ssm.cfg.controller"/>
<!-- 配置SpringMVC视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 释放静态资源文件 -->
<mvc:resources mapping="/js/" location="/js/**"/>
<mvc:resources mapping="/css/" location="/css/**"/>
<mvc:resources mapping="/images/" location="/images/**"/>
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="20971520"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
</beans>
2.5 mybatis配置文件
<?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>
<!-- 配置信息 -->
<settings>
<!-- 映射下划线到驼峰命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 配置开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 开启控制台打印SQL -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 别名 -->
<typeAliases>
<package name="com.tpl.ssm.cfg.entity"/>
</typeAliases>
</configuration>
2.6 数据库连接信息
# 连接数据库的url地址
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
# 加载的类的驱动
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
# 数据库用户名
jdbc.username=root
jdbc.password=root
# 初始化连接大小
jdbc.initialSize=10
# 连接池最大使用连接数量
jdbc.maxActive=500
# 连接池最小空闲
jdbc.minIdle=10
# 获取连接最大等待时间
jdbc.maxWait=60000
2.7 Entity
package com.tpl.ssm.cfg.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 测试实体
*/
public class TestEntity {
/**
* 主键编号
*/
private Long id;
/**
* 测试名称
*/
private String name;
/**
* 扩展字段
*/
@JsonIgnore
@TableField(exist = false)
private Map<String, Object> ext = new LinkedHashMap<>(5);
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Object> getExt() {
return ext;
}
public void setExt(Map<String, Object> ext) {
this.ext = ext;
}
}
2.8 Controller
package com.tpl.ssm.cfg.controller;
import com.tpl.ssm.cfg.entity.TestEntity;
import com.tpl.ssm.cfg.service.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* 1. Spring5.1.x -> jackson2.9.x
* 2.The origin server did not find a current representation for the target resource
* 原因是WEB-INF只能转发进去, 重定向是进不去的
*/
@Controller
@RequestMapping("/test")
public class TestController {
private final TestService testService;
public TestController(TestService testService) {
this.testService = testService;
}
@GetMapping("/")
public String listTests(TestEntity test, Model model) {
List<TestEntity> tests = testService.listTests(test);
model.addAttribute("tests", tests);
return "test";
}
@PostMapping("/save")
public String saveTest(TestEntity test) {
testService.saveTest(test);
return "redirect:/test/";
}
@PostMapping("/modify")
public String modifyTest(TestEntity test) {
testService.modifyTest(test);
return "redirect:/test/";
}
@RequestMapping("/remove")
public String removeTest(TestEntity test) {
testService.removeTest(test);
return "redirect:/test/";
}
}
2.9 Service
package com.tpl.ssm.cfg.service;
import com.tpl.ssm.cfg.entity.TestEntity;
import java.util.List;
public interface TestService {
/**
* 测试集
*
* @param cond 查询条件
* @return 测试集
*/
public List<TestEntity> listTests(TestEntity cond);
/**
* 单一测试实体
*
* @param cond 查询条件
* @return 测试实体
*/
public TestEntity singleTest(TestEntity cond);
/**
* 新增测试记录
*
* @param cond 查询条件
* @return 受影响的条数
*/
public boolean saveTest(TestEntity cond);
/**
* @param cond 查询条件
* @return 受影响的条数
*/
public boolean modifyTest(TestEntity cond);
/**
* 删除一条测试记录
*
* @param cond 查询条件
* @return 受影响的条数
*/
public boolean removeTest(TestEntity cond);
/**
* 删除多条测试记录
*
* @param testIds 测试实体主键集
* @return 受影响的条数
*/
public boolean removeTests(List<Long> testIds);
}
package com.tpl.ssm.cfg.service.impl;
import com.tpl.ssm.cfg.dao.TestDAO;
import com.tpl.ssm.cfg.entity.TestEntity;
import com.tpl.ssm.cfg.service.TestService;
import com.tpl.ssm.cfg.util.MajorKeyUtil;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestServiceImpl implements TestService {
private final TestDAO testDAO;
public TestServiceImpl(TestDAO testDAO) {
this.testDAO = testDAO;
}
@Override
public List<TestEntity> listTests(TestEntity cond) {
return testDAO.listTests(cond);
}
@Override
public TestEntity singleTest(TestEntity cond) {
return testDAO.singleTest(cond);
}
@Override
public boolean saveTest(TestEntity cond) {
cond.setId(MajorKeyUtil.idSeq());
return testDAO.insertTest(cond) > 0;
}
@Override
public boolean modifyTest(TestEntity cond) {
return testDAO.updateTest(cond) > 0;
}
@Override
public boolean removeTest(TestEntity cond) {
return testDAO.deleteTest(cond) > 0;
}
@Override
public boolean removeTests(List<Long> testIds) {
return testDAO.deleteTests(testIds) > 0;
}
}
2.10 DAO
package com.tpl.ssm.cfg.dao;
import com.tpl.ssm.cfg.entity.TestEntity;
import java.util.List;
/**
* 测试DAO
* <p>
* 1.查询测试记录列表 - listTests
* 2.查询单个测试记录 - singleTest
* 3.新增测试记录 - insertTest
* 4.修改测试记录 - updateTest
* 5.删除测试记录 - deleteTest
* 6.根据主键集删除测试记录 - deleteTests
*/
public interface TestDAO {
/**
* 测试集
*
* @param cond 查询条件
* @return 测试集
*/
public List<TestEntity> listTests(TestEntity cond);
/**
* 单一测试实体
*
* @param cond 查询条件
* @return 测试实体
*/
public TestEntity singleTest(TestEntity cond);
/**
* 新增测试记录
*
* @param cond 查询条件
* @return 受影响的条数
*/
public int insertTest(TestEntity cond);
/**
* @param cond 查询条件
* @return 受影响的条数
*/
public int updateTest(TestEntity cond);
/**
* 删除一条测试记录
*
* @param cond 查询条件
* @return 受影响的条数
*/
public int deleteTest(TestEntity cond);
/**
* 删除多条测试记录
*
* @param testIds 测试实体主键集
* @return 受影响的条数
*/
public int deleteTests(List<Long> testIds);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tpl.ssm.cfg.dao.TestDAO">
<!-- 基础查询 sql 片段 -->
<sql id="baseSelect">
SELECT id, name
</sql>
<!-- 基础查询 where 片段 -->
<sql id="baseWhere">
<if test="id != null">
AND id = #{id}
</if>
<if test="name != null and name != ''">
AND name like CONCAT('%', #{name})
</if>
</sql>
<!-- 查询测试记录列表 -->
<select id="listTests" parameterType="com.tpl.ssm.cfg.entity.TestEntity" resultType="com.tpl.ssm.cfg.entity.TestEntity">
<include refid="baseSelect"/>
FROM `ssm-tpl-cfg`
WHERE
1 = 1
<include refid="baseWhere"/>
</select>
<!-- 查询单个测试记录 -->
<select id="singleTest" parameterType="com.tpl.ssm.cfg.entity.TestEntity" resultType="com.tpl.ssm.cfg.entity.TestEntity">
<include refid="baseSelect"/>
FROM `ssm-tpl-cfg`
WHERE
1 = 1
<include refid="baseWhere"/>
limit 1
</select>
<!-- 新增测试记录 -->
<insert id="insertTest" parameterType="com.tpl.ssm.cfg.entity.TestEntity">
INSERT INTO `ssm-tpl-cfg`(id, name)
VALUES (#{id}, #{name})
</insert>
<!-- 修改测试记录 -->
<update id="updateTest" parameterType="com.tpl.ssm.cfg.entity.TestEntity">
UPDATE `ssm-tpl-cfg`
SET
<if test="name != null and name != ''">
name = #{name}
</if>
WHERE id = #{id}
</update>
<!-- 删除测试记录 -->
<delete id="deleteTest" parameterType="com.tpl.ssm.cfg.entity.TestEntity">
DELETE
FROM `ssm-tpl-cfg`
WHERE 1 = 1
<include refid="baseWhere"/>
</delete>
<!-- 根据主键集删除测试记录 -->
<delete id="deleteTests" parameterType="long">
DELETE
FROM `ssm-tpl-cfg`
WHERE id in
<foreach collection="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
</mapper>
2.11 MajorKeyUtil
package com.tpl.ssm.cfg.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 主键生成器
* <p>
* https://blog.csdn.net/weixin_42871989/article/details/100133736
* 本类用于获取mysql全局自增主键,格式为
* 时间戳(yyMMddHHmm) + 5 位自增,自增范围根据配置获取(也可以加上秒yyMMddHHmmss)
*/
public class MajorKeyUtil {
private static final DateFormat df = new SimpleDateFormat("yyMMddHHmm");//主键时间戳
private static final int MAX_SEQ = 0; // 计数位最大值
private static final int RESET_SEQ = 99999; //计数位重置初始值(可以自行设置)
private static final AtomicInteger seq = new AtomicInteger(RESET_SEQ); //计数器
/**
* 生成全局自增主键,字符串类型
*
* @return 生成的主键
*/
public static String getSeq() {
seq.compareAndSet(MAX_SEQ, RESET_SEQ); //判断是否进行重置
return df.format(new Date()) + String.format("%05d", seq.incrementAndGet());
}
/**
* 生成全局自增主键,long类型
*
* @return 生成的主键
*/
public static Long idSeq() {
String str = getSeq();
return Long.parseLong(str);
}
}
2.12 webapps/pages/test.jsp
<%--
Created by IntelliJ IDEA.
User: wangfeihu
Date: 2022/10/16
Time: 18:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>CRUD TEST</title>
</head>
<body>
<table>
<c:forEach items="${tests}" var="test">
<tr>
<td>${test.id}</td>
<td>${test.name}</td>
<td>
<button onclick="let modifyForms = document.getElementsByName('modifyForm'); modifyForms.forEach(item => item.style.display = 'none'); document.getElementById('form${test.id}').style.display = 'inline'">修改</button>
<button onclick="location.href='${pageContext.request.contextPath}/test/remove?id=${test.id}'">删除</button>
</td>
<td>
<form id="form${test.id}" name="modifyForm" action="${pageContext.request.contextPath}/test/modify" method="post" style="display: none">
<input hidden name="id" value="${test.id}">
<input name="name" value="${test.name}">
<button type="submit">确认修改</button>
</form>
</td>
</tr>
</c:forEach>
</table>
<form action="${pageContext.request.contextPath}/test/save" method="post">
<label>
<input name="name" id="name" placeholder="请输入测试名称~">
</label>
<button type="submit">提交</button>
</form>
</body>
</html>
2.13 配置项目访问路径
三、访问测试
访问 http://127.0.0.1:8080/ssm_tpl_cfg/test/
文章来源:https://www.toymoban.com/news/detail-816032.html
一个简易基于配置的增删改查就实现了文章来源地址https://www.toymoban.com/news/detail-816032.html
到了这里,关于Spring+SprinMVC+MyBatis配置方式简易模板的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!