当前位置:网站首页>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
边栏推荐
- 使用哈工大LTP测试分词并且增加自定义字典
- 怎么加入自媒体,了解这5种变现模式,让账号快速变现
- Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
- 2022年裁员潮,失业程序员何去何从?
- 建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
- 苹果逆势扩大iPhone 14系列备货,总量或达9500万部
- 力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
- LeetCode 86. 分隔链表
- ViT结构详解(附pytorch代码)
- Analysis of the name matching process between the LCD driver and the device (Tiny4412)
猜你喜欢

A case of violent parameter tuning in machine learning

负载均衡原理分析与源码解读

关于振弦采集模块及采集仪振弦频率值准确率的问题

Module 9 - Designing an e-commerce seckill system

Where can I view the version record of WeChat applet submission review history?

Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇

接口定义与实现

Network sockets (UDP and TCP programming)

Network Fundamentals (Section 1)

2022年裁员潮,失业程序员何去何从?
随机推荐
三星计划2023年开始在越南生产半导体零部件
苹果逆势扩大iPhone 14系列备货,总量或达9500万部
LeetCode 82. 删除排序链表中的重复元素 II
How many constants and data types do you remember?
迈矽科推出高性能77GHz毫米波雷达芯片,尚未量产就已获数万颗订单
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
网络基础(第一节)
不止跑路,拯救误操作rm -rf /*的小伙儿
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
LeetCode_443_压缩字符串
单目操作符(含原码反码补码转换)
Nocalhost - 让云原生时代的开发更高效
快速上手,征服三种不同分布式架构调用方案
Licking Exercise - 63 Find all anagrams in a string
中小规模网站架构
Module 9 - Designing an e-commerce seckill system
StoneDB 文档捉虫活动第一季
Nocalhost - Making development more efficient in the cloud-native era
接口定义与实现
Licking Exercise - 58 Verifying Binary Search Trees