当前位置:网站首页>SSM integrated learning notes (mainly ideas)
SSM integrated learning notes (mainly ideas)
2022-06-25 23:50:00 【Program diary】
ssm Will be spring,springmvc,mybatis These three frameworks are used together .
The first is to import jar package :
- mybatis what is needed jar
- spring what is needed jar
- Database driven jar
- Data source needs jar
- spring And mybatis The middle of integration jar
- springmvc Of jar

Import complete jar After that, let's show the project directory :
As shown in the figure below :
First of all, explain the idea , Is to write a simple query , The front desk passes in a id, Backstage basis id The user information can be found and transferred to the front page for display .
Let me start by saying that I wrote this demo The main steps : - First of all, I don't need to say anything about creating tables , The table has two fields ,id and username
- Then create web engineering , Write web.xml file , It mainly writes a load spirng Configuration file listeners and springmvc Front end controller for
- And then in src Create these four configuration files under ,applicationContext.xml, db.properties, mybatis-config.xml, springmvc-config.xml
- Then create the package , These are the bags in the above figure ,controller,po, dao,service also service Implementation package for
- Then write springmvc You have to configure the file , There are three main contents in it , Write scanned packages , Configure annotation driver , Configure the view parser
- Then write mybatis Configuration file for , There is only one content in it , Just give po Add an alias to your package , In this way mapper There is no need to write the full name of the class
- Then write db.properties There are some database configurations
- Last write applicationContext This is the most troublesome , The main contents of the writing are : First, introduction db.properties file , Configure data sources , Configure transaction manager , Open transaction annotation , To configure mybatis Factory sqlsessionfactory, To configure mapper Scanner , Configure scan service package
- Then you can write happily User, UserDao,UserDao.xml,service,serviceImpl,controller, This is some simple logic , In the business layer service Write the transaction annotations and business layer annotations on the implementation class of and inject dao, stay controller Write the control layer annotation , Inject service, And configure the access path on the method ,return success Complete jump to jsp page
- The next step is to write jsp 了 , stay WEB-INF Create folder jsp, establish success.jsp You can complete the jump ~
Next is the specific code :
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>
<!-- To configure springmvc Front end controller for -->
<!-- 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>
<!-- Don't intercept jsp -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Configuration is loaded spring File listener -->
<!-- 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">
<!-- scanning , Specify the package to scan -->
<context:component-scan base-package="com.hou.controller"></context:component-scan>
<!-- view resolver -->
<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>
<!-- Configure annotation driver -->
<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">
<!-- Read db.properties -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- Configure data sources -->
<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>
<!-- Transaction manager Depending on the data source -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- Open transaction annotation -->
<tx:annotation-driven />
<!--
To configure mybatis factory sqlsessionfactory
You need to give him a data source and mybatis Core profile for
-->
<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>
<!-- To configure mapper Scanner be based on MapperScannerConfigure Automatically generate the mapper through the interface in the package -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Specified package -->
<property name="basePackage" value="com.hou.dao"></property>
</bean>
<!-- scanning 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 {
/**
* according to id Query user information
*/
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 {
// Inject 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 {
// Inject 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> Here is the user information found :<br>
id:${user.id}<br>
username:${user.username}
</body>
</html>
Running environment :tomcat v8.5
Run a screenshot : Manually add /findUserById?id=2( Query with a parameter , Successfully jump to success And display )
Console output : Above about SSL Warning of , We are testing , Don't mind . About SSL: You can use Baidu or reference :https://www.cnblogs.com/mysql-dba/p/7061300.html
Database table structure : The truth is , Consider the length of the field , I used it for convenience 255
Database table data :
At present, we are considering using ajax and json To interact with the front and back …
边栏推荐
猜你喜欢

mysql版本升级+数据迁移

Analyse des cinq causes profondes de l'échec du développement de produits

Hibernate entity class curd, transaction operation summary

idea 查看单元测试覆盖率

解析產品開發失敗的5個根本原因

Leetcode-1528- rearrange string - hash table - string

二叉排序树

Use Baidu map API to set an overlay (infowindow) in the map to customize the window content

后序线索二叉树

18亿像素火星全景超高清NASA放出,非常震撼
随机推荐
权限设计=功能权限+数据权限
说说单例模式!
Analysis and comprehensive summary of full type equivalent judgment in go
php进程间传递文件描述符
UE4 learning records create a role and control its movement
util. Collection and encapsulation of JS tool functions
Apache Doris1.0版本集群搭建、负载均衡与参数调优
idea 查看单元测试覆盖率
MySQL自定义函数实例
Talk about the copy on write mechanism of PHP variables or parameters
Download the latest V80 version of Google Chrome
Go language escape analysis complete record
mysql版本升级+数据迁移
Can I upload pictures without deploying the server?
Apache doris1.0 cluster setup, load balancing and parameter tuning
Analyse des cinq causes profondes de l'échec du développement de produits
为什么Integer的比较最好使用equals
中序线索二叉树
Jenkins 发布PHP项目代码
QT Chinese and English use different fonts respectively