大家好,又见面了,我是你们的朋友全栈君。
我们整合SSM框架时,大部分都是基于注解+XML配置方式。只因为结合这两种方法能够实现同样的效果,而且会更加的轻松。所以在此推荐朋友们用注解+XML配置的方式,基于注解+XML配置方式会另写一篇。但是有朋友和我说,怎么用纯XML方式整合SSM呢?我做了一个入门的整理,如果不足,请多多指教。
本文是基于XML配置方式整合SSM框架,由于本人不太推荐这种方式。
首先可以看一下完整的目录结构
整合步骤如下:
1、我建的是maven工程,首先导入pom文件
代码语言:javascript代码运行次数:0运行复制
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
代码语言:javascript代码运行次数:0运行复制 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
代码语言:javascript代码运行次数:0运行复制jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=1111114、MyBatis配置,我们习惯叫SqlMapConfig.xml
代码语言:javascript代码运行次数:0运行复制
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5、配置映射文件 UserMapper.xml(由于SqlMapConfig.xml中使用的是包扫描,所以路径需要与实体类路径相同)
代码语言:javascript代码运行次数:0运行复制
select * from users
6、配置Spring的配置文件 applicationContext.xml
代码语言:javascript代码运行次数:0运行复制
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.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 ">
代码语言:javascript代码运行次数:0运行复制
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/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">
接下来就是我们最快乐的写代码时光了:
实体类:User.java
代码语言:javascript代码运行次数:0运行复制public class User {
private Integer id;
private String name;
private String password;
private String phone;
//省略setter/getter方法
}dao层(mapper):UserMapper.java
代码语言:javascript代码运行次数:0运行复制public interface UserMapper {
List
}service层:UserService.java
代码语言:javascript代码运行次数:0运行复制public interface UserService {
List
}UserServiceImpl.java
代码语言:javascript代码运行次数:0运行复制public class UserServiceImpl implements UserService {
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public List
return userMapper.findAll();
}
}web层UserController.java
代码语言:javascript代码运行次数:0运行复制@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
@ResponseBody
public List
List
return userList;
}
}由于没有写页面,就用工具测试一下效果吧:
这个整合适合入门小白,里面注释比较多,看懂应该都没有什么问题。而且还有一些比如文件上传也给提供了,可以自己在需要的时候使用。在CSDN提供一下源码下载https://download.csdn.net/download/nbl153/11245465,但是CSDN下载需要积分,没积分的朋友看这https://pan.baidu.com/s/1gV8NKeYFDcfWztIQtKVc3w 提取码:uhoh
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143330.html原文链接:https://javaforall.cn