ssm集成笔记_SSM(Spring+SpringMvc+Mybatis)整合笔记

1.使用开发工具

jdk1.8

eclipse

Tomcat7.0

MySql

2.创建数据库和表,由于重点是整合,所以数据库就随意加几条数据。

3c8e5168f663b8f41fc5cf61605a167d.png

42782e8d6e63e4aa9ebfce2e25d3137a.png

3.创建动态Web项目(推荐使用Maven可以用配置来导入jar包),导入jar包

b30c33ede4d640ff0132d616192ab900.png

c9bbb898eed8f94482d4ed83bae267da.png

2d4127a5c164b2770e05ba96583f24bb.png

不管了 全家桶一次导入 👻 有兴趣的可以自己去筛选一下。

4.SSM整合

4.1 创建config文件夹 在config文件夹下创建mybatis和spring的文件夹用来分别放mybatis和spring的配置文件,这步就是为了让你下次改可以快速找到,你要是一箩筐放src中也是可以滴。

7f49659acc1f3cf0465ee2c7baeeeb13.png

4.2加入SqlMapConfig.xml(Mybatis的用来创建工厂的“原材料” ,在没有spring的时候,数据库连接池是放在里面的 ,现在有了spirng,这个xml文件先为空就可以)

47914cbf8a60c2c64b03517e99a17b7a.png

/p>

"http://mybatis.org/dtd/mybatis-3-config.dtd">

4.2加入spring需要的applicationContext.xml文件,spring就是根据你的这个xml文件去帮你动态创建相应的对象,由于分层的缘故嗷,配置文件我们也给他分个层。

applicationContext-dao.xml 表示dao层

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

jdbc.properties

42270164ebf4dd37723373c8122b74f0.png

applicationContext-service.xml 由于现在还用不到,先配置个头的部分

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

4.3配置玩dao和service,就剩下ui层啦,也就是springmvc的配置 还是在spring文件下加springmvc.xml

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

4.4配置web.xml , 前面加了这个多配置文件,web项目可没有那么聪明知道这个是dao,这个是service的,所以我们要在web.xml中配置

ssm

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

contextConfigLocation

classpath:spring/applicationContext-*.xml

org.springframework.web.context.ContextLoaderListener

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

springmvc

/

4.5 还记得我们数据库写了两个User吗,我们的目标就是要显示这两个User,所以先加个jsp页面,用来显示。

89fc206dc15d8d33e7d6ed805a882f11.png

4.6.从数据库去找数据,从dao层开始,首先构建个pojo,pojo的字段名最好和数据库中的字段一样,这样mybatis可以自动映射

packagecom.Dy.pojo;public classUser {privateString Id;privateString username;privateString password;publicString getId() {returnId;

}public voidsetId(String id) {

Id=id;

}publicString getUsername() {returnusername;

}public voidsetUsername(String username) {this.username =username;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}

}

4.7.加入Mapper接口,从数据库找出这两条数据

packagecom.Dy.Mapper;importjava.util.List;importcom.Dy.pojo.User;public interfaceUserMapper {public ListfindAllUsers();

}

因为我们才用Mapper动态代理,所以加入UserMapper.xml 要和接口同名才行 这样不需要写接口的实现类,sping会帮我们创建一个具体的实现类

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from user

4.8. 配置动态扫描,动态扫描就是spring扫描你配置的包下,发现两个同名的,那么下次你用接口的时候,就自动帮你实例化一个对象,执行的代码就是xml的sql语句。在applicationContext-dao.xml中加上下面这句话

1aad73f28c058d77b6f0ae270f145fd6.png

4.9.Service层开发,写一个接口,在实现这个接口。

packagecom.Dy.Service;importjava.util.List;importcom.Dy.pojo.User;public interfaceUserService {public ListfindAllUsers();

}

packagecom.Dy.Service;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.Dy.Mapper.UserMapper;importcom.Dy.pojo.User;

@Servicepublic class UserServiceImpl implementsUserService {

@AutowiredprivateUserMapper userMapper;

@Overridepublic ListfindAllUsers() {returnuserMapper.findAllUsers();

}

}

由于我们使用的是注解开发,所以在allicationConext-service.xml加上扫描

b7927d28af7fa4da64a79e368a02dc7f.png

4.10 写Controller层

packagecom.Dy.Controller;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importcom.Dy.Service.UserService;importcom.Dy.pojo.User;

@Controllerpublic classUserController {

@AutowiredprivateUserService userService;

@RequestMapping("/Home")publicString findAllUser(Model model) {

List users =userService.findAllUsers();

model.addAttribute("users", users);return "user";

}

}

因为Controller用了注解开发,所以在spingmvc.xml中加上

13b317377d88665fe065ac7dab91337b.png

4.11启动项目

3c7d6ca825d6326d3d8d0431615fb2be.png

c662a0edecb3a5e94efb3aa23ef0f309.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值