SSM environment construction

Keywords: Maven Mybatis Tomcat xml

SSM environment construction


SSM(Spring+SpringMVC+MyBatis), the framework of web project

First of all, ensure that the Maven configuration is completed and that the image source is replaced by domestic Alibaba. See maven configuration

Create project

Menu bar file - > New - > Project - > Maven - > Maven - > Archetype - > webapp

Project naming

Select the maven installation path and other configuration file paths, remember the right side, and finally add archetypeCatalog=internal to increase performance.


Initial directory structure

Load successful

With maven version management, we don't need to manually import the jar package ourselves. In the pom.xml file, configure the relevant version to load and auto enable to download.

General directory structure

Specific file configuration

UserInfoMapper.xml configuration file implementation class

namespace="com.zhongruan.dao.IUserInfoDao" is the interface of the class to be implemented

id write method name

resultType is the return value

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.IUserInfoDao" >
    <select id="findAll" resultType="com.zhongruan.bean.UserInfo">
        select * from userInfo
    </select>
</mapper>

IUserInfoService is the interface

public interface IUserInfoService {
    public List<UserInfo> findAll();
}

UserInfoServiceImpl is the specific implementation result

@Autowired, spring will help new automatically, and you don't need to do it yourself.

@Service("userInfoServiceImpl") //Put it in a container
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    IUserInfoDao userInfoDao;

    @Override
    public List<UserInfo> findAll() {
        return userInfoDao.findAll();
    }

}

UserInfoController class

@Controller								//Notification is a control layer
@RequestMapping("user")					 //1 level path
public class UserInfoController {
    @Autowired
    IUserInfoService userInfoService;//=new UserInfoServiceImpl();

    @RequestMapping("findAll.do")		//2 level path
    public ModelAndView findAll(){		//Jump method
        List<UserInfo> users=userInfoService.findAll();
        ModelAndView mv=new ModelAndView();
        mv.addObject("users",users);
        mv.setViewName("allUser");      //Add allUser.jsp
        return mv;

    }

//    @Autowired
}

UserInfo class bean

package com.zhongruan.bean;

public class UserInfo {
    private int id;
    private String username;
    private String password;

    public UserInfo(){

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

Run Tomcat

If it is configured, click the operation of triangle shape directly. If it is not configured, click Edit Configurations in the upper right corner.

Choose the tomcat installation path

Deployment - > + - > artifact - > SSM: War expanded, where I have added SSM: War expanded

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<html>
<body>
<h2>Hello World!</h2>
<a href="${pageContext.request.contextPath}/user/findAll.do">Click jump</a>
</body>
</html>

Operation result

home page

Click to query all users

Posted by pyro3k on Tue, 29 Oct 2019 14:53:15 -0700