SSM backend development minutes

Table of contents

introduction

technology stack

Knowledge points

        Interface

       Static class

accomplish

        Spring

        MyBatis

       SpringMVC

       SpringBoot

question


introduction

        Recently, I need to complete the development of the backend of a management system. During the summer training of my sophomore year, I learned the backend framework, including springBoot, mybatis, spring, springMVC, etc. However, the study period was very short. I only tested the simple examples in the teacher's notes. The understanding of the Dao layer, Service layer, and Controller layer in the backend was not deep enough. This time, I used the actual project to test the previously learned backend knowledge. To consolidate and improve, the following is a review of the basics of each back-end framework. There may be inappropriate places. If some content is consistent with your question, but I didn't explain it clearly, please leave a message and I will try my best to solve it for you. ,study together.

technology stack

        Spring boot: Focus on spring development and reduce configuration

        Mybatis: persistence layer, lightweight semi-automated ORM framework

        SpringMVC: A lightweight web framework that implements MVC based on JAVA.

Knowledge points

        Interface

        In JAVA, an interface is a public abstract class, which only has the characteristics of methods and no implementation of methods. It can be implemented by multiple classes, and these implementations can have different behaviors. Provides a solution to JAVA's inability to implement multiple inheritance. An interface can be implemented in multiple places

       Static class

        Static means static or global. What is modified by static can be a variable, a class or a block of code. There is no such thing as a global variable in Java. Member variables or member methods modified by static are independent of any object of the class. It does not depend on a specific instance of the class, is shared by all instances, and can be accessed when a class is created.

accomplish

        Spring

        ​​​​A lightweight control inversion and aspect-oriented programming framework.

        Try the traditional way of implementing a simple function, create an interface for the function implementation in the Dao layer, create an implementation class of the interface, and then create an interface in the Service layer and create its implementation class. When you need to use it, you need to instantiate the implementation class of the Service layer. When you need to perform another operation, you immediately need to instantiate another implementation class. This method is very cumbersome.

// userDao的实现类
package dao.Impl;
import dao.userDao;
public class userDaoImpl implements userDao{
    @Override
    public void getUser() {
        System.out.println("取得用户");
    }
}
// userDao的接口
package dao;

public interface userDao {
    public void getUser();
}
// userService的实现类
package service.Impl;

import dao.Impl.userDaoImpl;
import dao.userDao;
import service.userService;

public class userServiceImpl implements userService {
    private userDao userDao = new userDaoImpl();
    @Override
    public void getUser() {
        userDao.getUser();
    }
}
// userService的接口
package service;

public interface userService {
    public void getUser();
}

        If we try to leave an interface in the Service implementation class instead of specifically implementing a function, it will bring greater convenience.

// 在service实现类中设置一个接口
package service.Impl;

import dao.Impl.userDaoImpl;
import dao.userDao;
import service.userService;

public class userServiceImpl implements userService {
//    该方式过度的耦合,使用set方式流出一个接口
//    private userDao userDao = new userDaoImpl();
    private userDao userDao;

    public void setUserDao(userDao userDao){
        this.userDao = userDao;
    }
    @Override
    public void getUser() {
        userDao.getUser();
    }
}
// 当需要使用到某个功能的时候,直接将Dao层的实现类传入之前留出的接口
import dao.Impl.userDaoImpl;
import dao.Impl.userDaoMysqlImpl;
import service.Impl.userServiceImpl;
import service.userService;

public class Test {
    @org.junit.Test
    public void test(){
        // 先执行userDaoMysqlImpl
        userServiceImpl userService = new userServiceImpl();
        userService.setUserDao(new userDaoMysqlImpl());
        userService.getUser();

        // 此时又想执行另外一个功能
        userService.setUserDao(new userDaoImpl());
        userService.getUser();
    }
}

        Through set injection, it effectively solves the problem that when using different functions of the data layer, new implementation classes of different data layers are needed in the Service layer. However, there is still a problem that as long as the data layer is used, a new implementation class must be added in the service layer. When the data layer changes, the service layer also needs to change, so the implementation class of the service-initiated new data layer is adjusted to provide the Service layer from the outside. This is the idea of ​​Inversion of Control (IOC). Inversion of control creates and initializes the class objects of the Dao layer and the Service layer through a bean container. Although there are class objects of the Dao layer and the Service layer in the container, the program cannot be executed yet because the Service layer relies on the Dao layer to provide services. Therefore, it is necessary to establish a dependency relationship between the Service layer and the Dao layer in the bean container, which is dependency injection (DI).

// 添加spring依赖
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>

// 编写beans.xml文件
<bean id="hello" class="pojo.Hello" >
    <property name="name" value="spring"/>
</bean>

// 测试
@org.junit.Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    Hello hello = (Hello) context.getBean("hello");

    System.out.println(hello.toString());

    hello.show();

}

        ​ ​ ​ Let’s use inversion of control and dependency injection at the same time

<!--    依赖注入  -->
        <bean id="userDao" class="dao.Impl.userDaoImpl"/>
<!--    将userDaoService与userDao绑定   -->
        <bean id="userDaoService" class="service.Impl.userServiceImpl">
            <property name="userDao" ref="userDao"/>
        </bean>
// userServiceImpl通过set留出功能实现的接口 
package service.Impl;

import dao.Impl.userDaoImpl;
import dao.userDao;
import service.userService;

public class userServiceImpl implements userService {
//    该方式过度的耦合,使用set方式流出一个接口
//    private userDao userDao = new userDaoImpl();
    private userDao userDao;

    public void setUserDao(userDao userDao){
        this.userDao = userDao;
    }
    @Override
    public void getUser() {
        userDao.getUser();
    }
}
// 测试
    @org.junit.Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        userServiceImpl userService = (userServiceImpl)context.getBean("userDaoService");
        userService.getUser();
    }
        MyBatis

         Mybatis is a framework for the persistence layer. Recall that the traditional way of operating data is as follows: configuring the connection to the database, using SQL statements to perform specific operations, and encapsulating the results using objects to undertake. These operations all need to be completed manually. Mybatis can greatly simplify these operations. You can feel the convenience of mybatis through the following two pictures facing each other.

        Writing entity classes

public class User {
    private long id;
    private String username;
    private String password;
    private String name;
    private String phone;
    private String head_url;
    private long dept_id;
    private long post_id;
    private String description;
    private int status;
    private Date create_time;
    private Date update_time;
    private int is_deleted;
    public User (){

    }
    public User(long id, String username, String password, String name, String phone, String head_url, long dept_id, long post_id, String description, int status, Date create_time, Date update_time, int is_deleted) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.phone = phone;
        this.head_url = head_url;
        this.dept_id = dept_id;
        this.post_id = post_id;
        this.description = description;
        this.status = status;
        this.create_time = create_time;
        this.update_time = update_time;
        this.is_deleted = is_deleted;
    }

    public long getId() {
        return id;
    }

    public void setId(long 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;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getHead_url() {
        return head_url;
    }

    public void setHead_url(String head_url) {
        this.head_url = head_url;
    }

    public long getDept_id() {
        return dept_id;
    }

    public void setDept_id(long dept_id) {
        this.dept_id = dept_id;
    }

    public long getPost_id() {
        return post_id;
    }

    public void setPost_id(long post_id) {
        this.post_id = post_id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public void setCreate_time(Date create_time) {
        this.create_time = create_time;
    }

    public Date getUpdate_time() {
        return update_time;
    }

    public void setUpdate_time(Date update_time) {
        this.update_time = update_time;
    }

    public int getIs_deleted() {
        return is_deleted;
    }

    public void setIs_deleted(int is_deleted) {
        this.is_deleted = is_deleted;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", head_url='" + head_url + '\'' +
                ", dept_id=" + dept_id +
                ", post_id=" + post_id +
                ", description='" + description + '\'' +
                ", status=" + status +
                ", create_time=" + create_time +
                ", update_time=" + update_time +
                ", is_deleted=" + is_deleted +
                '}';
    }
}

        Writing Dao interface

public interface userDao {
    List<User> getUserList();
}

        write the specific implementation of mapper 

<mapper namespace="dao.userDao">
    <select id="getUserList" resultType="pojo.User">
        select * from user;
    </select>
</mapper>

        Register in the mybatis-config.xml core configuration file

    <mappers>
        <mapper resource="dao/mapper/userDaoMapper.xml"></mapper>
    </mappers>

         Create a sqlSessionFacrory tool class

public class MybatisUtils {
    // 定义一个工厂
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            // 使用mybatis获取sqlsessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

         test

    public void test4(){
        // 获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 执行sql
        userDao mapper = sqlSession.getMapper(userDao.class);
        List<User> userList = mapper.getUserList();
        userList.forEach(System.out::println);
        // 关闭sqlSession
        sqlSession.clearCache();
    }
       SpringMVC

        ​​ Can’t start using the built-in tomcat, we will solve it later

       SpringBoot

        Springboot requires only a minimal amount of configuration to run a web program.

<properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

        Main startup

@SpringBootApplication
public class SpringBoot01Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringBoot01Application.class, args);
    }
}

        Control class

@RestController   //  @Controller  + @ResponseBody
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/h1")
    public String hello(){
        return "Hello~ SpringBoor,你用起来真的太香了";
    }


}

question

        ​ ​ 1. Idea connects to the Alibaba Cloud mysql database and uses idea to automatically download the database connection driver. The download fails. The host uses a proxy, but idea has not been set up, so it is still very slow. Use auto detect proxy to solve the problem. I guess after using this setting, idea will automatically look for the host's proxy.

        ​ ​ 2. Idea uses kotlin. The version of the delivered back-end code idea is version 23. The default kotlin is version 1.9x, and it is also set to 1.9 in maven. My idea is version 21, which supports up to version 1.6. I tried to lower the configuration of the kotlin version in maven, but found that it did not work. It showed that mybatis had dependencies on version 1.9 of kotlin, so I tried to install the idea23 version and installed it again. maven configuration. It may also be necessary to configure the idea agent. Although the maven warehouse of Alibaba Cloud is installed in maven, the speed is still very slow. After hanging for 2 hours, the download was finally completed.

         3. Configure mybatis and it will appear:

                (1)org.apache.ibatis.binding.BindingException: Type interface dao.userDao is not known to the MapperRegistry.

                This problem occurs probably because userDaoMapper.xml is not registered in mybatis-config.xml

                (2) Caused by: java.io.IOException: Could not find resource com/lxyk/dao/UserMapper.xml. This error is likely to have two reasons. One is that the path of the Pojo class or Dao class is incorrect, and the other is that the path of the Pojo class or Dao class is incorrect. One reason is that the following code is not added to pom.xml.

    <!--在build中配置resources  来防止我们资源导出失败的问题-->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>

        4. Use Spring Boot to start the simplest web, configure the startup class and the control class that needs to be accessed, but the Whitelabel Error Page keeps appearing. After checking the information, I found that the startup class and the control class are not in the same package. If @ComponentScan is not used, There will be a situation where the startup class cannot find the control class. The correct organization method is shown below. The two classes need to be under the same main package.

        You can also use @ComponentScan("control class path") in the startup class

Guess you like

Origin blog.csdn.net/2201_75875170/article/details/133901505