SSH2 framework

In recent years SpringMVC development is relatively rapid, his advantage has slowly manifested, and even many companies require SpringMVC alternative SSH framework, in the end there are those SpringMVC difference does it make?

What SpringMVC framework?

 

Spring Framework is highly configurable, multiple view techniques, e.g. JavaServer Pages (JSP) technology, Velocity, Tiles, iText, and POI. Spring MVC framework does not know the view of the use, so you can customize the selected view. Spring MVC separation controller, model objects, as well as dispatcher role handler object, such separation to make them easier to customize.

SpringMVC is a typical textbook mvc framework, rather than struts, etc. are not entirely based on variations or mvc framework system for beginners or people who want to know the mvc I think spring is the best, its implementation is a textbook! The second and tapestry as it is a pure servlet system, this is it and tapestry compared struts do not have advantages. Frame and the code itself, and it seems easy to understand.

 

What SSH2 framework?

 

SSH into the system framework of responsibilities from the four layers: presentation layer, business logic, data persistence layer and domain layer modules to help developers build a clear structure in the short term, be good reusability, easy to maintain Web applications . Using Struts as a whole infrastructure of the system, responsible for the separation of MVC, the model part of the Struts framework, control of the business to jump, use Hibernate framework provides support for persistence layer, Spring do management, management struts and hibernate. Specifically: make some models based on the needs of object-oriented analysis methods, these models will be implemented as a basic Java objects and then write basic DAO (Data Access Objects) interfaces, and give DAO Hibernate implementation, the use of Hibernate framework to achieve the DAO classes to implement the conversion between Java classes and access to the database, and finally by the Spring do management, management struts and hibernate.

 

SpringMVC and SSH comparison:

struts more comprehensive package of features, the use of relatively unique. springmvc closer to the native servlet, high flexibility. But also because the controller is a single springmvc embodiment, and no large number of filters, better performance than struts2.

Another point is that Spring has its own JdbcTemplate, used to perform the original ecology of SQL statements.

Such as:

. 1
2
. 3
. 4
. 5
@Transactional
public Integer the Click (String ID) {
    jdbcTemplate.update ( "the UPDATE SET COUNT = COUNT + Content. 1 WHERE ID =?", ID);
    return jdbcTemplate.queryForInt ( "SELECT COUNT from WHERE ID = Content ? ", ID);
}
the SSH is used to operate the database Hibernate mapping data, the operation is the object.

Such as:

. 1
2
. 3
. 4
public void the Click (User) {
    // Update (User);
    Save (User);
}
the Hibernate originally JDBC-based package, so compared to the original Eco SQL execution efficiency certainly faster than the operation target.

Note: If you want to use Spring to achieve Hibernate function, you can confidant package

Such as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    public T save(T entity){
        Field[] declaredFields = entityClass.getDeclaredFields();
        String pk="";
        for(Field f : declaredFields){
            boolean annotationPresent = f.isAnnotationPresent(Id.class);
            if(annotationPresent){
                pk = f.getName();
                break;
            }
        }
        if(StringUtils.isBlank(pk)){
            Method[] declaredMethods = entityClass.getDeclaredMethods();
            for (Method method : declaredMethods) {
                boolean annotationPresent = method.isAnnotationPresent(Id.class);
                if(annotationPresent){
                    String field = method.getName().replace("get", "");
                    //首字母转小写
                    pk = field.replace(field.charAt(0), (char)(field.charAt(0)+32));
                    break;
                }
            }
        }
        Map<String, Object> map = EntityToMapUtil.convertEntityToMap(entity);
        Object pkvalue = map.get(pk);
        if(pkvalue!=null&&StringUtils.isNotBlank(pkvalue.toString())){
            update(map,pk);
            return entity;
        }
        SimpleJdbcInsert simpleJdbcInsert=new SimpleJdbcInsert(jdbcTemplate);
        KeyHolder keyHolder = simpleJdbcInsert.withTableName(table())
        //.usingColumns(new String[keySet.size()])
        .usingGeneratedKeyColumns(pk)
        .executeAndReturnKeyHolder(map);
        try {
            BeanUtils.setProperty(entity, pk, keyHolder.getKeys().values());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return entity;
    }
     
    public void insert(Map map,String... pks){
        Set<String> columns = map.keySet();
        for(String pk : pks){
            if(columns.contains(pk))
                columns.remove(pk);
        }
        SimpleJdbcInsert simpleJdbcInsert=new SimpleJdbcInsert(jdbcTemplate);
        Keyholder = simpleJdbcInsert.withTableName a KeyHolder (Table ())
        .usingColumns (columns.toArray (new new String [columns.size ()]))
        .usingGeneratedKeyColumns (PKS)
        .executeAndReturnKeyHolder (Map);
    }  
the original is used to obtain the reflection properties of the object , then the corresponding database field, and finally spelled SQl statement, the feeling is not very big impact on efficiency.

 

We are now the company is using to do SpringMVC based framework, but does not integrate Hibernate. The latest version also supports SpringMVC RestFull, you can return the corresponding JSON or XML directly.

Such as code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("public")
public class CommonController {
    @Autowired
    private ContentService contentService;
    @RequestMapping("ViewClick")
    public String clicks(HttpServletRequest request,@RequestParam String id){
        int clicks=0;
        synchronized (this) {
            clicks= contentService.click(id);
        }
        return "document.write('"+clicks+"')";at the front desk to call (this is just a small function of my blog currently used)@ RestController // key notes1}
    }



1
阅读(<script src="../public/ViewClick?id=${content.id}"></script>)

----------------
Disclaimer: This article is CSDN blogger "webin 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/swebin/article/details/52583718

Guess you like

Origin www.cnblogs.com/qiu18359243869/p/12602912.html