1. Project structure:
2. Related JAR Package:
3. Related Configuration files:
(1) Web. XML configuration file:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<!--Configure the initial open page-
<!--<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Spring Container load-
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringConf.xml</param-value>
</context-param>
<!--SPRINGMVC Front Controller--
<servlet>
<servlet-name>MyDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--load Profile path--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC-servlet.xml</param-value>
</init-param>
<!--when starting a value greater than 0 indicates that this servlet is initialized when the container starts, the smaller the positive values the higher the priority-
<load-on-startup>1</load-on-startup>
</servlet>
<!--Spring MVC configuration file Ends--
<!--SPRINGMVC Intercept settings--
<servlet-mapping>
<servlet-name>MyDispatcher</servlet-name>
<!--intercept all requests by SPRINGMVC--
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Springmvc intercept setting ends-
<!--solve Chinese garbled problems--
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(2) springconf.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:context= "Http://www.springframework.org/schema/context"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-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/context
Http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!--configuring data sources-
<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/mydb?characterencoding=utf-8"/>
<property name= "username" value= "root"/>
<property name= "password" value= "123"/>
</bean>
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<property name= "configlocation" value= "Classpath:MyBatisConf.xml"/>
<!--<property name= "typealiasespackage" value= "Com.tiantian.ckeditor.model"
/>--
</bean>
<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >
<property name= "Mapperinterface"
Value= "Com.ssm.mapper.UserMapper"/>
<property name= "Sqlsessionfactory" ref= "Sqlsessionfactory"/>
</bean>
<!--automatic scanning of annotated beans--
<context:component-scan base-package= "Com.ssm.dao"/>
</beans>
(3) Springmvc-servlet.xml configuration file
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:util= "Http://www.springframework.org/schema/util" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:p= "http://www.springframework.org/schema/p" xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xsi:schemalocation= "
Http://www.springframework.org/schema/util
Http://www.springframework.org/schema/util/spring-util-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/beans
Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<mvc:annotation-driven/>
<!--convert classes marked with @controller annotations to beans--
<context:component-scan base-package= "Com.ssm.controller"/>
<!--resolution of the Model view name, that is, the model view name is added before and after
<bean
Class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix= "/web-inf/views/" p:suffix= ". jsp"/>
</beans>
(4) Mybatisconf.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 the alias of the mapping class--
<typeAliases>
<typealias alias= "User" type= "Com.ssm.model.User"/>
</typeAliases>
<!--Configure the path to the mapper file--
<mappers>
<mapper resource= "Mapper/usermapper.xml"/>
</mappers>
</configuration>
(5) Usermapper.xml configuration file
<?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.ssm.mapper.UserMapper" >
<!--query A single record--
<select id= "Selectuserbyname" parametertype= "String" resulttype= "User" >
SELECT * from user where name= #{name}
</select>
</mapper>
4.Java Code:
(1) Control layer Usercontroller.java
Package Com.ssm.controller;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Com.ssm.dao.UserDao;
Import Com.ssm.model.User;
@Controller
public class Usercontroller {
@Autowired
Private Userdao DAO;
@RequestMapping (value= "/login", method = Requestmethod.post)
public string Login (string name,string password) {
if (Name==null | | password==null) {
return "fail";
}
User user = Dao.finduserbyname (name);
if (User!=null && password.equals (User.getpassword ())) {
Return "Success";
}
return "fail";
}
}
(2) DAO Layer:
① interface
Package Com.ssm.dao;
Import Com.ssm.model.User;
/**
* DAO interface Layer
*/
Public interface Userdao {
/**
* Query user information according to user name
* @param name
* @return
*/
Public User finduserbyname (String name);
}
② Implementation Class
Package Com.ssm.dao;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Com.ssm.mapper.UserMapper;
Import Com.ssm.model.User;
/**
* DAO Implementation Layer
*/
@Component
public class Userdaoimpl implements userdao{
@Autowired
Private Usermapper Usermapper;
@Override
Public User finduserbyname (String name) {
User user = Usermapper.selectuserbyname (name);
return user;
}
}
(3) Mapping Relationship interface
Package com.ssm.mapper;
Import Com.ssm.model.User;
/**
* Mapper Mapping class
*/
Public interface Usermapper {
Public User selectuserbyname (String name);
}
(4) Entity class
Package Com.ssm.model;
/**
* User Mapping class
*/
public class User {
Private Integer ID;
private String name;
private String password;
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;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + ((name = = null)? 0:name.hashcode ());
result = Prime * result
+ ((password = = null)? 0:password.hashcode ());
return result;
}
@Override
public boolean equals (Object obj) {
if (this = = obj)
return true;
if (obj = = null)
return false;
if (GetClass ()! = Obj.getclass ())
return false;
User other = (user) obj;
if (name = = null) {
if (other.name! = null)
return false;
} else if (!name.equals (other.name))
return false;
if (password = = null) {
if (Other.password! = null)
return false;
} else if (!password.equals (Other.password))
return false;
return true;
}
}
+++++++++++++++++++++++++++++ Congratulations, you have completed the SSM Framework Setup demo, you can test the +++++++++++++++
SSM (spring+springmvc+mybatis) Framework Integration