Rough translation understanding of SSM framework (pure theory)

Posted by PunjabHaker on Wed, 08 Dec 2021 07:15:04 +0100

1. Development tools and brief description of technologies used

1.1 framework description

Framework: Spring+Spring MVC+MyBatis

1.2 development tools

Development tools: JDK, Tomcat, IntelliJ, idea, spring, spring MVC, MyBatis,MySQL

2. Develop technical route

2.1 basic knowledge and application of spring

2.1.1 Spring foundation

1.IOC (Inversion of Control): instead of creating objects by yourself, you use the IOC container provided by the spring framework to manage them. You can directly obtain objects from the IOC container, and the control is handed over by the application to the IOC container.
IOC realizes the decoupling between dependency objects and associates object classes through IOC container. (Understanding: I used to cook meals by myself. Now I eat and send them directly to you outside meituan.)
2.AOP(Aspect Oriented Programming) aspect oriented programming
3.DI (Dependency Inject): the attribute of an object, which has been injected with relevant values, can be used directly. The program relies on the container to create and inject the external resources it needs
4. The core containers of the framework are BeanFactory and ApplicationContext. The methods include setter injection and construction method injection
5. Use of Bean. Write the Bean file and the class corresponding to the file together in applicationContext.xml (Understanding: create an object instance)

2.1.2 Spring Bean

1.Bean instances have 6 domains
singleton: always point to the same object (default)
Prototype: when spring obtains the bean defined by the prototype, it will create a new bean instance
Request: an http request will return the same instance of the bean. Different requests will generate new. The bean is only valid in the http request
Session: same as above, valid only in http session
application: create an instance for each servletContext, which is only valid in the web related applicationContext
Websocket: create an instance for each websocket, which is only valid in the web related applicationContext

2. Take prototype scope as an example

<bean id="scope" class="com.ssm.xx.xx.xx" scope="prototype"/>

Where id can also be name, class is your instance file path, and scope is the bean scope

3.bean assembly mode (bean dependency injection mode): XML assembly, Annotation assembly, automatic assembly

4. Assembly through Annotation: try to master common annotations (@ Component,@Repository,@Service,@Controller,@Autowired,@Resource,@Qualifier)

5.@Component, which describes the Bean in Spring. It can act at any level, just annotate it on the corresponding class
@Repository, used for data access layer (DAO layer), identifies the business layer class as a Bean in Spring
@Service, acting on the business layer (service layer)
@Controller, acting on the control layer (controller)
@Autowired is used to label the Bean's attribute variables, attribute setter () methods and construction methods, and cooperate with the corresponding annotation processor to complete the automatic assembly of the Bean.
@Resource, similar to Autowired, is assembled according to the instance name of the Bean
@Qualifier, used with @ Autowired annotation

6.<context:component-scan base-package="com.ssm.xxx"/>
Direct scanning eliminates the need for matching bean id and class in xml

7. Personal understanding of Annotation: with annotations, there is only one step left to configure bean s. What files use what annotations, and when to use @ Resource depends on the situation

2.1.3 what does spring AOP mean

1.AOP, aspect oriented programming, is mainly to modify the code. It is too troublesome to modify all relevant methods. The solution is to extract the repeated codes scattered in various methods, and apply these codes to the places to be executed when compiling.
If the specific content is omitted, you can refer to the understanding of others

2.2 MyBatis related knowledge

2.2.1 about database

1.spring jdbc
Configure the data source, database driver, URL to connect to the database, user name and password in applicationContext.xml
The so-called four vajras of dataSource refer to the driver name, data source address, user name and user password

2. To operate the package to be imported from the database (mysql package, jdbc package, spring transaction processing package, 5 packages of spring Architecture)

3. First create an instance class – > write a method class (Dao) with parameters – > a class that implements the method of the interface
4. Methods of adding, deleting and modifying query. Examples of adding methods:

public int addUser(User user){
 String sql="insert  into user(username,password) value(?,?)";
 Object[] params=new Object[]{
    user.getUsername(),
    user.getPassword()
  };
  int num=this.jdbdTemplate.update(sql,obj);
  return num;
}
//How to update users
public int updateUser(User user){
String sql="update user det username=?,password=? where id=?";
Object[] params=new Object[]{
user.getUsername(),
user.getPassword(),
user.getId()
};
int num=this.jdbdTemplate.update(sql,params);
return num;
}
//How to delete a user
public int deleteUser(User user){
 String sql="delete from user where id=?";
 int num =this.jdbcTemplate.update(sql,id);
 return num;
}

5. You can use Test to write, add, delete and query Test files to operate the database. You should inject JDBC template into userDao in advance
Example: test the example of adding and querying data

@Test
public void addUserTest(){
//load configuration
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//Get userDao instance
UserDao userDao=(UserDao)applicationContext.getBean("userDao");
//Create user instance
User user= new User();
//Set user instance property value
user.setUsername("zhangsan");
user.setPassword("123");
//Add user
int num=userDao.addUser(user);
if(num>0){
system.out.println("ok!");
}
else{
System.out.println("false");
}
}

6. In userdao, first create a method to query a single user and all users – > write a specific query method in Dao interface – > test class write test query single – > query all

//Create method name
//Find users by id
public User findUserById(int id);
//Find all
public List<User> findAllUser();
//Specific search methods
//Find users by id
public User findUserById(int id){
String sql="select * from user where id=?";
RowMapper<User> rowMapper= new BeanPropertyRowMapper<User>(User.class);
return this.jdbcTemplate.queryForObject(sql,rowMapper,id);
}
//Find all
public List<User>findAllUser(){
String sql="select * from user";
RowMapper<User> rowMapper= new BeanPropertyRowMapper<User>(User.class);
return this.jdbcTemplate.queryForObject(sql,rowMapper);
}
//The test class looks up data according to the id
@Test
public void findUserByIdTest(){
ApplicationContext applicationContext=new ClassPathXmlApplication("applicationContext.xml"):
//Find data by id
UserDao userDao=(UserDao)applicationContext. getBean("userDao");
User user =userDao.findUserById(22):
System.out.println(user);
}
//Full check user test class
@Test
public void findAllUserTest(){
ApplicationContext applicationContext=new ClassPathXmlApplication("applicationContext.xml"):
//Find data by id
UserDao userDao=(UserDao)applicationContext. getBean("userDao");
//Query all user data
List<User>list=userDao.findAllUser();
//All user data information needs to be output
for(User user:list){
System.out.println(user);
  }
}

2.2.2 Spring transaction processing (developed by mainstream annotation)

1. Programming transaction management: transaction management realized by writing code, including defining the beginning of fifteen, committing fifteen after normal execution and rolling back things in case of exception.

2. Declarative transaction management: the transaction management realized through AOP technology is written separately as a "aspect" code, and the "aspect" code of transaction management is implanted into the business target class through AOP technology.

3. Commonly used named transaction management, as long as the related transaction rules are named in the configuration file. (ready made, just use it)
4. Declarative transaction management is based on XML and Annotation.

5. Annotation based transaction management method: register the transaction annotation driver in the spring container – > add the annotation @ Transactional on the spring Bean or Bean class method that needs to use the transaction

//Register transaction annotation driver
<tx:annotation-driven transaction-managers="transactionManager"/>

2.2.3 personal knowledge of MyBatis

1.MyBatis is a persistence layer framework that supports SQL query, storage and high-level mapping. XML or annotation configuration maps interfaces and POJO s in Java into records in the database. (object oriented operations – > mybatis – > SQL operations)

2. Create the User persistence class under the POJO package, the name attribute and the getter/setter method – > create the mapping file UserMapper.xml file (operation database) under the mapper package – > configure the mybatis-config.xml file (jdbc and mapper location) – > write the test file

3. Write the addition, deletion, modification and query operation statements through the UserMapper file, and write the specific methods in the test file

2.2.4 dynamic SQL (personal opinions on adding, deleting, modifying and querying)

1. < if >: judgment statement, used for single condition branch judgment

2. < choose > (< when >, < otherwise >): used for multi conditional branch judgment, similar to switch case default

3. < where > < trim > < set > is used for SQL assembly, special characters and other problems

4. < foreach > circular statements are often used in enumeration conditions such as in statements (the most used)

//The use of foreach element, which is usually used when building IN conditional statements
<select id="findUserByIds" parameterType="List" reaultType="com.ssm.po.User">
  select * from t_user where id in 
<foreach item="id" index="index" collection="list" open="("separator=","close=")">
  #{id}
</foreach>
</select>

5. < bind > is used in fuzzy query SQL

2.3 spring MVC related knowledge (omitted)

2.3.1 what is MVC

2.3.2 how to bind data

2.3.3 json data interaction / RESTful support

2.3.4 interceptor

Topics: Java Spring Back-end