当前位置:网站首页>SSM整合学习笔记(主要是思路)
SSM整合学习笔记(主要是思路)
2022-06-25 22:08:00 【程の编程日记】
ssm就是将spring,springmvc,mybatis这三个框架结合到了一起来使用。
首先就是导入jar包:
- mybatis所需jar
- spring所需jar
- 数据库驱动jar
- 数据源所需jar
- spring与mybatis整合的中间jar
- springmvc的jar

导入完jar包然后就是先展示一下项目目录吧:
如下图所示:
首先说明一下思路,就是写一个简单的查询,前台传入一个id,后台根据id可以查找到用户信息并且传到前台页面显示。
先说一下我写这个demo的主要步骤吧: - 首先创建表都不用我说了吧,表有两个字段,id和username
- 然后创建web工程,写web.xml文件,里面主要写一个加载spirng配置文件的监听器和springmvc的前端控制器
- 然后在src下创建这四个配置文件,applicationContext.xml, db.properties, mybatis-config.xml, springmvc-config.xml
- 然后创建包,就是上图的几个包,controller,po, dao,service还有service的实现包
- 然后编写springmvc得配置文件,里面主要写三个内容,写上扫描的包,配置注解驱动,配置视图解析器
- 然后编写mybatis的配置文件,里面就写一个内容,就是给po的包加上别名,这样在mapper里面就不用写类的全名了
- 然后编写db.properties 里面就是一些数据库的配置
- 最后写applicationContext这个是最麻烦的,写的内容主要有:先是引入db.properties文件,配置数据源,配置事务管理器,开启事务注解,配置mybatis工厂的sqlsessionfactory,配置mapper扫描器,配置扫描service包
- 然后就可以快乐的写User, UserDao,UserDao.xml,service,serviceImpl,controller,这就是一些简单的逻辑了,要在业务层service的实现类上写上事务的注解和业务层的注解并且要注入dao,在controller写上控制层注解,注入service,并且在方法上面配置访问路径,return success完成跳转到jsp页面
- 接下来就是写jsp了,在WEB-INF下创建文件夹jsp,创建success.jsp就可以完成跳转了~
接下来是具体代码:
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">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置springmvc的前端控制器 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- 不拦截jsp -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置加载spring文件的监听器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
springmvc-config.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.0.xsd">
<!-- 扫描,指定扫描的包 -->
<context:component-scan base-package="com.hou.controller"></context:component-scan>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
mybatis-config.xml
<?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>
<typeAliases>
<package name="com.hou.po"/>
</typeAliases>
</configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///user
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
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:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxTotal" value="${jdbc.maxTotal}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<!-- 事务管理器 依赖于数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven />
<!--
配置mybatis工厂sqlsessionfactory
需要给他一个数据源和mybatis的核心配置文件
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
</bean>
<!-- 配置mapper扫描器 基于MapperScannerConfigure 自动通过包中的接口来生成映射器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定包 -->
<property name="basePackage" value="com.hou.dao"></property>
</bean>
<!-- 扫描service -->
<context:component-scan base-package="com.hou.service"></context:component-scan>
</beans>
UserDao.java
package com.hou.dao;
import com.hou.po.User;
public interface UserDao {
/**
* 根据id查询用户信息
*/
public User findUserById(Integer id);
}
UserDao.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.hou.dao.UserDao">
<select id="findUserById" parameterType="Integer" resultType="User">
select * from t_user where id=#{id}
</select>
</mapper>
UserService.java
package com.hou.service;
import com.hou.po.User;
public interface UserService {
public User findUserById(Integer id);
}
UserSercviceImpl.java
package com.hou.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hou.dao.UserDao;
import com.hou.po.User;
import com.hou.service.UserService;
@Service
@Transactional
public class UserSercviceImpl implements UserService {
//注入UserDao
@Autowired
private UserDao userDao;
public User findUserById(Integer id) {
return this.userDao.findUserById(id);
}
}
UserController.java
package com.hou.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hou.po.User;
import com.hou.service.UserService;
@Controller
public class UserController {
//注入service
@Autowired
private UserService userService;
@RequestMapping("/findUserById")
public String findUserById(Integer id, Model model) {
User user = this.userService.findUserById(id);
System.out.println(user);
model.addAttribute("user", user);
return "success";
}
}
success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
ok
<br>以下是找到的用户信息:<br>
id:${user.id}<br>
username:${user.username}
</body>
</html>
运行环境:tomcat v8.5
运行截图:手动在后面加上/findUserById?id=2(带着一个参数进行查询,成功跳转到success并显示)
控制台输出:上面关于SSL的警告,咱们是测试,不用理会。关于SSL:可自行百度或者参考:https://www.cnblogs.com/mysql-dba/p/7061300.html
数据库表结构:真实情况的话,要考虑字段的长度,我为了方便用了255
数据库表数据:
目前在考虑用ajax和json来进行前后台交互…
边栏推荐
- SVN
- 213.打家劫舍 II
- Database - mongodb
- Leetcode-1528- rearrange string - hash table - string
- 28 rounds of interviews with 10 companies in two and a half years (including byte, pinduoduo, meituan, Didi...)
- Problems encountered in Doris operation and maintenance
- CXF
- Solve 'tuple' object has no attribute 'lower‘
- Line height for small use
- 平衡二叉树AVL
猜你喜欢
![Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]](/img/3a/fb3bfe673db5123e3124404f3905c0.png)
Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]

音频基础知识以及PCM转WAV

Blob

UE4 learning record 2 adding skeleton, skin and motion animation to characters

QT custom implemented calendar control

idea Kotlin版本升级

The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high

Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!

line-height小用

流数据
随机推荐
权限设计=功能权限+数据权限
Run the dronekit flight control application on Shumei faction under px4-jmavsim software simulation environment
Qlabel text scrolling horizontally
Line height for small use
MySQL自定义函数实例
格式化编号,不够位数的补0,例如1:0001,25:0025
Visual studio code create minimal web API (asp.net core)
聊聊swoole或者php cli 进程如何热重启
C# IO Stream 流(二)扩展类_封装器
二叉排序树
一文讲透研发效能!您关心的问题都在
实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式
Ad20 learning notes I
util. Collection and encapsulation of JS tool functions
Px4 multi computer simulation (gazebo)
Apache doris1.0 cluster setup, load balancing and parameter tuning
C. Yet Another Card Deck-Educational Codeforces Round 107 (Rated for Div. 2)
C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
Hibernate architecture introduction and environment construction (very detailed)
Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)