Spring|ssm框架整合


文章目录

  • 创建数据库
  • 创建Maven工程
  • 编写实体类
  • 编写mapper
  • 编写service
  • 与页面交互的Controller
  • sql语句配置文件
  • spring 配置文件
  • spring-mvc配置文件
  • mybaits配置文件
  • web页面
    • save页面
    • accountList页面

创建数据库
create database ssm; create table account( id int primary key auto_increment, name varchar(100), money double(7,2) );

创建好是这样的
Spring|ssm框架整合
文章图片

里面的数据是后来加的
创建Maven工程 Spring|ssm框架整合
文章图片

Spring|ssm框架整合
文章图片

编写实体类
package com.tong.domain; public class Account {private Integer id; private String name; private Double money; @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; }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 Double getMoney() { return money; }public void setMoney(Double money) { this.money = money; }}

编写mapper 【Spring|ssm框架整合】mapper是实现最底层功能的类
mapper是提供给service来使用的,通过mapper的基础方法service通过使用基础方法得到一些高级的方法来让controller使用
controller是直接与页面交互的 controller通过使用service里面的方法来实现具体的功能
这里mapper里面是接口是因为我们可以通过mybaits来自动实现实现类
package com.tong.mapper; import com.tong.domain.Account; import java.util.List; public interface AccountMapper { public void save(Account account); public List findAll(); }

编写service 这次实现的方法比较简单所有没有实现额外的功能
接口在这
package com.tong.service; import com.tong.domain.Account; import java.util.List; public interface AccountService { public void save(Account account ); public List findAll(); }

实现在这
package com.tong.service.impl; import com.tong.domain.Account; import com.tong.mapper.AccountMapper; import com.tong.service.AccountService; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; import java.io.InputStream; import java.util.List; @Service("accountService") public class AccountServiceImpl implements AccountService {@Autowired private AccountMapper accountMapper; 通过 在spring容器中找到AccountMapper类来注入到这里面 @Override public void save(Account account) { accountMapper.save(account); }@Override public List findAll() { return accountMapper.findAll(); } }

与页面交互的Controller
package com.tong.controller; import com.tong.domain.Account; import com.tong.service.AccountService; 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.ResponseBody; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller 这个注解表明了这个是Controller @RequestMapping("/account") 这个表示这个下面的方法访问的地址前都要加上/account public class AccountController { @Autowired 默认按byType自动装配 public AccountService accountService; @RequestMapping(value = "https://www.it610.com/save",produces = "text/html; charset=UTF-8") 这个方法访问是 根目录+/account/save @ResponseBody 不返回地址 public String save(Account account){ System.out.println(account.toString()); accountService.save(account); 调用service里面的方法完成功能 return"保存成功了"; } @RequestMapping("/findAll") public ModelAndView findAll(){ List all = accountService.findAll(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("accountList",all); modelAndView.setViewName("accountList"); modelAndView里面model是封装数据 view是跳转到那个页面return modelAndView; }}

sql语句配置文件
这很重要 这确定了我们写的sql语句是要用来实现那个接口位置不能写错 insert into account values(#{id},#{name},#{money}) id="findAll" resultType="account"> select * from account

spring 配置文件
我们要排除controller是因为controller是直接与页面交互的 controller要被springMVC扫描 通过mybatis得到接口的实现类

spring-mvc配置文件

mybaits配置文件
这是加载映射文件 这个文件就是之前编写sql语句的xml配置文件 这样mybaits就能自动生成实现类了

web页面 save页面
Title 添加账户信息表单
账户名称:
账户金额:


accountList页面
Title 展示账户数据列表
账户id 账户名称 账户金额
${account.id} ${account.name} ${account.money}

    推荐阅读