SSM整合
Posted wnwn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合相关的知识,希望对你有一定的参考价值。
基本概念
mybatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
spring mvc
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
SSM整合(以下实现登录功能)
步骤一:导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!--引入需要的ehcache插件--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>1.2.3</version> </dependency> <!--mybatis整合ehcache的jar--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
步骤二:JDBC.properites文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/smbms?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=123
步骤三:applicationContext.xml文件
<!--扫描注解--> <context:component-scan base-package="com.ssm"/> <!--添加MVC支持--> <mvc:annotation-driven/> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!--释放静态资源--> <!--<mvc:default-servlet-handler/>--> <!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--SqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--加载数据源--> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描Dao层--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.dao"/> </bean> <!--开启事务注解支持--> <tx:annotation-driven/> <!--事务管理器--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
步骤四:mybatis-config文件
<?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> <settings> <!--log4j配置引入--> <setting name="logImpl" value="LOG4J"/> </settings> <!--设置别名--> <typeAliases> <!--全局设置别名:默认以类名作为别名--> <package name="com.ssm.entity"/> </typeAliases> <!--mappers 代表小配置集合--> <mappers> <!--<mapper resource="org/mybatis/example/BlogMapper.xml"/>--> <package name="com.ssm.dao"/> </mappers> </configuration>
步骤五:web.xml文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <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> <!--强制使用UTF-8编码--> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!--中央调度器--> <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:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
步骤六:实体类
package com.ssm.entity; import java.io.Serializable; import java.util.Date; public class Smbms_User implements Serializable { private Integer id; private String usercode; private String username; private String userpassword; private Integer gender; private Date birthday; private String phone; private String address; private Integer userrole; private Integer createdby; private Date creationdate; private Integer modifyby; private Date modifydate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsercode() { return usercode; } public void setUsercode(String usercode) { this.usercode = usercode == null ? null : usercode.trim(); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword == null ? null : userpassword.trim(); } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone == null ? null : phone.trim(); } public String getAddress() { return address; } public void setAddress(String address) { this.address = address == null ? null : address.trim(); } public Integer getUserrole() { return userrole; } public void setUserrole(Integer userrole) { this.userrole = userrole; } public Integer getCreatedby() { return createdby; } public void setCreatedby(Integer createdby) { this.createdby = createdby; } public Date getCreationdate() { return creationdate; } public void setCreationdate(Date creationdate) { this.creationdate = creationdate; } public Integer getModifyby() { return modifyby; } public void setModifyby(Integer modifyby) { this.modifyby = modifyby; } public Date getModifydate() { return modifydate; } public void setModifydate(Date modifydate) { this.modifydate = modifydate; } }
步骤七:编写dao层
package com.ssm.dao; import com.ssm.entity.Smbms_User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Repository public interface Smbms_UserMapper { public Smbms_User login(@Param("user_code") String user_code, @Param("user_password") String user_password); }
步骤八:编写dao.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.ssm.dao.Smbms_UserMapper" > <select id="login" resultType="Smbms_User"> select * from smbms_user where userCode=#{user_code} and userPassword=#{user_password} </select> </mapper>
步骤九:编写service业务接口层
package com.ssm.service; import com.ssm.entity.Smbms_User; import org.apache.ibatis.annotations.Param; public interface Smbms_UserService { public Smbms_User login(String user_code,String user_password); }
步骤十:编写serviceImpl业务实现层
package com.ssm.service.impl; import com.ssm.dao.Smbms_UserMapper; import com.ssm.entity.Smbms_User; import com.ssm.service.Smbms_UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @Service("smbms_UserService") public class Smbms_UserServcieImpl implements Smbms_UserService { //植入Dao层对象 @Resource private Smbms_UserMapper smbms_userMapper; //事务 /* @Transactional(isolation=Isolation.DEFAULT)*/ @Override public Smbms_User login(String user_code, String user_password) { return smbms_userMapper.login(user_code,user_password); } }
步骤十一:编写控制器
package com.ssm.controller; import com.ssm.entity.Smbms_User; import com.ssm.service.Smbms_UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpSession; @Controller @RequestMapping("/user") public class Smbms_UserController { //植入Service层分对象 @Resource private Smbms_UserService smbms_userService; @RequestMapping("/login") public String doLogin(Smbms_User smbms_user, HttpSession session){ Smbms_User user = smbms_userService.login(smbms_user.getUsercode(), smbms_user.getUserpassword()); if(user!=null){ session.setAttribute("user",user); return "welcome"; } return "login"; } }
步骤十二:页面
login.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆</title> </head> <body> <div> <form action="/user/login" method="post"> 用户名:<input type="text" name="usercode"/> 密码:<input type="password" name="userpassword"/> <input type="submit" value="登陆"/> </form> </div> </body> </html>
welcome.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>欢迎</title> </head> <body> 欢迎光临:${user.username} </body> </html>
步骤十三:实现结果
以上是关于SSM整合的主要内容,如果未能解决你的问题,请参考以下文章
520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“