Spring-Mybatis-SpringMVC三大框架整合
阅读原文时间:2023年07月11日阅读:2

我们直接切人正题,不多逼逼赖赖

第一步:依赖,一下的这些基本上是SSM整合的全部依赖


org.springframework spring-beans 5.1..RELEASE

org.springframework spring-context 5.1..RELEASE
log4j log4j 1.2.
commons-logging commons-logging 1.2

org.aspectj aspectjweaver 1.9.

org.springframework spring-jdbc 5.1..RELEASE

mysql mysql-connector-java 5.1.
org.mybatis mybatis 3.4.
org.mybatis.generator mybatis-generator-core 1.3.

net.sf.ehcache ehcache 1.2.

org.mybatis mybatis-ehcache 1.0.

org.mybatis mybatis-spring 1.3.

com.alibaba druid 1.1.
org.slf4j slf4j-api 1.7.

<dependency>  
  <groupId>org.slf4j</groupId>  
  <artifactId>slf4j-log4j12</artifactId>  
  <version>1.7.</version>  
  <scope>test</scope>  
</dependency>  
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->  
<dependency>  
  <groupId>com.alibaba</groupId>  
  <artifactId>fastjson</artifactId>  
  <version>1.2.</version>  
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
<dependency>  
  <groupId>org.springframework</groupId>  
  <artifactId>spring-webmvc</artifactId>  
  <version>5.1..RELEASE</version>  
</dependency>  
<dependency>  
  <groupId>org.springframework</groupId>  
  <artifactId>spring-web</artifactId>  
  <version>5.1..RELEASE</version>  
</dependency>  
<dependency>  
  <groupId>javaee</groupId>  
  <artifactId>javaee-api</artifactId>  
  <version></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.</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.</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.</version>  
</dependency>  
<dependency>  
  <groupId>jstl</groupId>  
  <artifactId>jstl</artifactId>  
  <version>1.2</version>  
</dependency>

第二步:com.ssm.entity层

public class Smbms_User implements Serializable {
private Integer id;

private String usercode;

private String username;

private String userpassword;

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();  
}

}

第三步:com.ssm.dao层

@Repository
public interface Smbms_UserMapper {

@Select("select \* from smbms\_user where userCode=#{user\_code} and userPassword=#{user\_password}")  
public Smbms\_User login(@Param("user\_code") String user\_code, @Param("user\_password") String user\_password);

}

第四步:com.ssm.dao层中的.xml文件



¨C40C ¨C41C

第五步:com.ssm.service层

public interface Smbms_UserService {

public Smbms\_User login(String user\_code, String user\_password);  

}

第六步:com.ssm.service.impl层

@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);
}
}

第七步:com.ssm.controller层

@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";  
}  

}

第八步:resourdes中的mybatis-config.xml文件



<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>

第九步:resourdes中的jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

第十步:resourdes中的applicationContext.xml文件


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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> ¨C67C ¨C68C ¨C69C ¨C70C

第十一步:WEB-INF下的web.xml文件


Archetype Created Web Application
characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF- forceEncoding true
characterEncodingFilter /*


springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1
springmvc /

第十二步:登录页面的搭建

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登陆

用户名: 密码:

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
欢迎 欢迎光临:${user.username}

以上都是对SSM整合的讲解,具体顺序可以自己搭配,希望对大家有帮助,如果有什么看不懂的地方,可以评论,我一定会第一时间帮助大家解答疑惑,谢谢