SSM框架初解(我是新手)

SSM框架结构

controller:获得参数,返回响应
dao:和数据库之间交互
entity:存储与数据库相关的类 例如:User类 Blog类
service:完成业务逻辑
webapp:新建jsp文件

程序:

项目结构:

在这里插入图片描述
userList.jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action = "index">
        姓名:<input type="text" name="account" value = "${user.account}"/>
        年龄:<input type="text" name="pwd" value = "${user.pwd}"/>
        <input type = "submit" value="提交"/>
    </form>
</body>
</html>

UserController.java代码:

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("userController")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("index")
    public String index(Model model, HttpServletRequest request) {

        User user = userService.findById(1);
        model.addAttribute("user", user);
        return "user/userList";
    }
}

UserService.java代码:

package com.qcby.service;

import com.qcby.entity.User;

public interface UserService {
    User findById(Integer id);
}

UserServiceImpl.java代码:

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findById(Integer id) {
        User user = userMapper.selectByPrimaryKey(String.valueOf((long) id));
        return user;
    }
}

通过 点击m(mybatis反向生成命令) 输入mybatis-generator:generate
在这里插入图片描述
会逆向生成dao(UserMapper) entity(User) mapping(UserMapper.xml)文件,Blog也会生成。
在这里插入图片描述
UserMapper.java代码:

public interface UserMapper {
    int deleteByPrimaryKey(String id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(String id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}
运行流程:
  1. 运行tomcat服务器后,输入网址http://localhost:8080/testssm/userController/index
    进入界面,在这里插入图片描述
  2. 点击提交后,通过form表单action=“index”进入到UserController.java文件
  3. 调用index方法,通过userService.findById(1),访问到UserService.java文件中
  4. 调用findById方法,通过这个接口跳到UserServiceImpl.java文件
  5. 运行findById方法会调用userMapper.java文件中的User selectByPrimaryKey(string id) 方法
  6. 这个方法会调用UserMapper.xml文件中的id为selectByPrimaryKey的方法,
<mapper namespace="com.qcby.dao.UserMapper">
  <resultMap id="BaseResultMap" type="com.qcby.entity.User">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="account" jdbcType="VARCHAR" property="account" />
    <result column="pwd" jdbcType="VARCHAR" property="pwd" />
  </resultMap>
  <sql id="Base_Column_List">
    id, account, pwd
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=VARCHAR}
  </select>
  1. 读取到数据库数据后return到UserServiceImpl.java文件中赋给对象user
  2. 再return user到UserController.java文件中赋值给user对象
  3. 再将user添加到model对象中
  4. 再return “user/userList”返回到userList.jsp中通过el表达式
${user.account}    ${user.pwd}

取到数据库数据并显示。

browser->UserController->UserService->UserServiceImpl->UserMapper->UserMapper.xml->UserServiceImpl->UserController->userList.jsp->browser
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值