SSM framework instance (user login function) * tomcat and jdk version compatibility issues need to be considered
Step 1: Create a new web project First, import the jar package needed to build SSM to WebContent\WEB-INF\lib.
Step 2: Create the required class packages controller, mapper, pojo, service, serviceImpl;
1. Put the control layer java class in the controller package:
Login.java
@Controller//Annotation
public class Login {
//@Autowired can annotate member variables, methods and constructors to complete the work of autowiring
@Autowired//Annotation, note that this is an interface, not a specific implementation class
public LoginService service;
@RequestMapping("/login")//The mapping address can be freely defined
private String login(String userName,String passWord){
User user=service.login(userName, passWord);
if(user!=null){
return "success";//Return the file name of jsp, no prefix and suffix are required, just configure the prefix and suffix in springmvc.xml
}
return "fail";
}
}
2. Put the dao layer java interface and configuration file in the mapper package:
The interface name is the same as the corresponding configuration file name, and the id in the configuration file corresponds to the method name in the interface
The namespace of the configuration file is the path to the corresponding interface
LoginMapper.java
public interface LoginMapper {
//login
public User login(String userName,String passWord);
}
LoginMapper.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.yd.mapper.LoginMapper">
<!-- login verification
resultType return type, #{0} indicates the first parameter passed in
-->
<select id="login" resultType="User">
select * from user where userName=#{0} and passWord=#{1}
</select>
</mapper>
3. Leave in pojo package javabean:
User.java
public class User {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
4. Put the business layer interface in the service package, same as mapper
LoginService.java
public interface LoginService {
//login
public User login(String userName,String passWord);
}
5. The implementation class of the corresponding interface of the business layer is placed in the serviceImpl package
LoginServiceImpl.java
public class LoginServiceImpl implements LoginService{
@Autowired
public LoginMapper mapper;
@Override
public User login(String userName, String passWord) {
User user=mapper.login(userName, passWord);
return user;
}
}
Step 3: Create a view layer, create a folder view in WebContent\WEB-INF to store jsp pages (cannot be accessed directly through a browser)
1.fail.jsp
2.success.jsp
Create login.jsp under WebContent (directly accessible through browser)
Note: The action of the form form must be consistent with the @RequestMapping value of the corresponding method in the corresponding controller of the control layer.
The name of the control is the same as the formal parameter of the corresponding method.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>login</title>
</head>
<body>
<h1 align="center">Login</h1>
<br>
<form action="login" method="post">
<table width="200" height="100" bgcolor="#000fff" align="center" border="1">
<tr>
<td align="right">account</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td align="right">password</td>
<td><input type="password" name="passWord"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
Step 4: Create a data source folder and place SSM related configuration files
1. sqlMapConfig.xml configuration file
<?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>
<!-- configure alias -->
<typeAliases>
<!-- Batch Scan-->
<package name="com.yd.pojo"/>
</typeAliases>
<!-- Configure mapper Since the integration package of spring and mybatis is used for mapper scanning, no configuration is required here. Must follow: mapper.xml and mapper.java files have the same name and are in the same directory -->
</configuration>
2. applicationContext-dao.xml configuration file
<?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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Load the content of the db.properties file, the key naming of db.properties must have certain special rules-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- configure data source -->
<!-- The function of destroy-method="close" is to put the connection back into the data pool when the database connection is not in use, which is convenient for the next call. -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${mysql.driverClassName}"></property>
<property name="jdbcUrl" value="${mysql.url}"></property>
<property name="user" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
<property name="acquireIncrement" value="5"></property> <!-- When the connection in the connection pool is used up, the number of new connections created by C3P0 at one time is 2 -->
<property name="initialPoolSize" value="10"></property> <!-- The number of connections created during initialization, which must be between minPoolSize and maxPoolSize -->
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<!-- maximum idle time, connections that exceed the idle time will be discarded
[Note: The default connection duration of mysql is 8 hours (28800) [you can add wait_timeout=30 (unit seconds) in my.ini to set the connection timeout], the timeout for setting c3p0 here must be < 28800]
-->
<property name="maxIdleTime" value="300"></property>
<property name="idleConnectionTestPeriod" value="60"></property> <!-- Check the connection pool for idle connections every 60 seconds-->
<property name="maxStatements" value="20"></property> <!-- The standard parameters of jdbc are used to control the number of PreparedStatements loaded in the data source, but since the pre-cached Statements belong to a single Connection rather than the entire connection-- >
</bean>
<!-- Configure SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
<!-- Load the global configuration file of mybatis-->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Configure mapper scanner -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Scanned package, if you want to scan multiple packages, use half-width, separated -->
<property name="basePackage" value="com.yd.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
3.applicationContext-service.xml configuration file
<?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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- configure service -->
<bean id="loginService" class="com.yd.serviceImpl.LoginServiceImpl"></bean>
</beans>
4.applicationContext-transaction.xml configuration file
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--Thing Manager
For database transaction control of mybatis operations, spring uses jdbc control
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- configure data source -->
<!--Configured in applicationContext-dao.xml-->
<property name="dataSource" ref="dataSource"></property>
</bean>
5. springmvc.xml configuration file
<?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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!--Scan Controller -->
<context:component-scan base-package="com.yd.controller"></context:component-scan>
<!-- mvc annotation driver: use mvc:annotation-driven to replace the configuration of the above two annotation mappers and mappers
The mvc annotation driver will load the binding methods of many parameters by default, such as json conversion, and the json parser will be loaded by default
Actual development uses mvc annotation driver -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- View resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix/WEB-INF/jsp-->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- suffix -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Step 5: Configure the web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Load Spring container -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spirngmvc front controller -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Specify the location of the springmvc configuration file through initialization parameters -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Step 6: Deploy the project to tomcat, start the service, and link to the login page through http://localhost:8080/First/login.jsp.