当前位置:网站首页>Ssm framework construction process [easy to understand]
Ssm framework construction process [easy to understand]
2022-08-10 11:57:00 【Full stack programmer webmaster】
大家好,又见面了,我是你们的朋友全栈君.
1.新建一个maven web项目(Please refer to the new process:https://blog.csdn.net/AinUser/article/details/78185128),项目结构如下:
2.项目中需要引入的jar包:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>In fact, you can introduce related ones when neededjar包即可,This will avoid introducing some useless ones. 3.mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!--later for testing-->
<typeAlias type="com.tgb.entity.User" alias="User" />
</typeAliases>
</configuration>4.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mapping/UserMapper.xml"/>
</bean>
<!-- DAO接口所在包名,SpringIt will automatically find the class in it -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tgb.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解扫描-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>5.applicationContext-mvc.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: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">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.tgb"/>
<!-- <mvc:resources mapping="/static/**" location="WEB-INF/static/"/>-->
<!--配置springMVC的视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>6.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<!--加载spring容器配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Spring容器加载所有的配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext.xml</param-value>
</context-param>
<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*:spring/applicationContext-mvc.xml</param-value>
</init-param>
<!--用来标记是否在项目启动时就加在此Servlet,0或正数表示容器在应用启动时就加载这个Servlet, 当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载.正数值越小启动优先值越高 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>到此为止,The configuration file is basically ready,We need to write a function to test it. 7.生成实体,mapper等文件.(逆向工程参考:https://blog.csdn.net/zhshulin/article/details/23912615) 建表Sql:
CREATE TABLE `tb_user` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(18) NOT NULL, `SEX` varchar(18) NOT NULL, `AGE` int(11) DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;User实体:
package com.tgb.entity;
public class User {
private Integer id;
private String name;
private String sex;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}UserMapper:
package com.tgb.dao;
import com.tgb.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List<User> selectAllUser();
}UserMapper.xml
<?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.tgb.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.tgb.entity.User" >
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
<result column="SEX" property="sex" jdbcType="VARCHAR" />
<result column="AGE" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
ID, NAME, SEX, AGE
</sql>
<select id="selectAllUser" resultType="User">
select * from tb_user;
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from tb_user
where ID = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from tb_user
where ID = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="User" >
insert into tb_user (ID, NAME, SEX,
AGE)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="User" >
insert into tb_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
ID,
</if>
<if test="name != null" >
NAME,
</if>
<if test="sex != null" >
SEX,
</if>
<if test="age != null" >
AGE,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="User" >
update tb_user
<set >
<if test="name != null" >
NAME = #{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
SEX = #{sex,jdbcType=VARCHAR},
</if>
<if test="age != null" >
AGE = #{age,jdbcType=INTEGER},
</if>
</set>
where ID = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="User" >
update tb_user
set NAME = #{name,jdbcType=VARCHAR},
SEX = #{sex,jdbcType=VARCHAR},
AGE = #{age,jdbcType=INTEGER}
where ID = #{id,jdbcType=INTEGER}
</update>
</mapper>Service接口和实现类:
package com.tgb.service;
import com.tgb.entity.User;
import java.util.List;
/** * Created by xiaojie on 2018/7/8. */
public interface UserService {
public List<User> getUser();
}
package com.tgb.service.impl;
import com.tgb.dao.UserMapper;
import com.tgb.entity.User;
import com.tgb.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/** * Created by xiaojie on 2018/7/8. */
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource(name="userMapper")
private UserMapper userMapper;
@Override
public List<User> getUser() {
return userMapper.selectAllUser();
}
}Controller
package com.tgb.controller;
import com.tgb.entity.User;
import com.tgb.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
/** * Created by xiaojie on 2018/7/8. */
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Resource(name="userService")
UserService userService;
@RequestMapping(value = "/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();
List<User> userList = userService.getUser();
mv.addObject("userList",userList);
mv.setViewName("/show");
return mv;
}
}创建一个show.jsp文件:
<%-- Created by IntelliJ IDEA. User: xiaojie Date: 2018/7/8 Time: 15:25 To change this template use File | Settings | File Templates. --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
</tr>
<c:choose>
<c:when test="${not empty userList}">
<c:forEach items="${userList}" var="user" varStatus="vs">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.sex}</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td colspan="2">无数据!</td>
</tr>
</c:otherwise>
</c:choose>
</table>
</body>
</html>启动项目,在浏览器里输入http://localhost:8080/user/list If the following list appears,则说明ssmA line has been passed.
Follow this step to do it,Usually not displayed smoothly,可能会遇到一些问题,百度一下,都会解决的.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105615.html原文链接:https://javaforall.cn
边栏推荐
- 力扣练习——60 二叉搜索子树的最大键值和
- LeetCode 61. 旋转链表
- Where can I view the version record of WeChat applet submission review history?
- 中小规模网站架构
- codevs 2370 Small room tree (LCA)
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- 自媒体爆款标题怎么写?手把手教你写热门标题
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- LAXCUS分布式操作系统安全管理
- 怎么加入自媒体,了解这5种变现模式,让账号快速变现
猜你喜欢

mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull

网络套接字(UDP和TCP编程)

LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)

How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize

Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
![[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list](/img/87/7ac0307de54f2defe8f72c554745cd.png)
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list

怎么加入自媒体,了解这5种变现模式,让账号快速变现

Since the media hot style title how to write?Taught you how to write the title

皕杰报表在传参乱码

建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
随机推荐
力扣练习——59 从二叉搜索树到更大和树
使用哈工大LTP测试分词并且增加自定义字典
石墨文档打开文档时快速定位到上次写的位置
StoneDB 文档捉虫活动第一季
【Redis】内存回收策略
flask-restplus接口地址404问题
How many constants and data types do you remember?
负载均衡原理分析与源码解读
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
使用JMeter进行MySQL的压力测试
基于UiAutomator2+PageObject模式开展APP自动化测试实战
LeetCode 146. LRU 缓存
【LeetCode】640. 求解方程
如何使用工程仪器设备在线监测管理系统
力扣练习——63 找到字符串中所有字母异位词
暑期总结4
力扣练习——61 根据字符出现频率排序
有哪些好用的性能测试工具推荐?性能测试报告收费标准
从脚本到剪辑,影像大师亲授的后期制作秘籍
Centos7环境使用Mysql离线安装包安装Mysql5.7