Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring+SpringMVC+MyBatis+Maven framework integration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article records the integration of the Spring+SpringMVC+MyBatis+Maven framework, mainly recording the following points

1. Jar package that needs to be introduced into Maven

II. Configuration separation of Spring and SpringMVC

III. The integration of Spring and MyBatis

1. Jar package that needs to be introduced into Maven

The default reader of this article has mastered the use of Maven, and the Maven configuration snippet is as follows

Xml code

Org.springframework

Spring-webmvc

${springframework.version}

Org.springframework

Spring-jdbc

${springframework.version}

Org.mybatis

Mybatis

${mybatis.version}

Org.mybatis

Mybatis-spring

${mybatis-spring.version}

Com.oracle

Ojdbc14

${oracle14.version}

C3p0

C3p0

${c3p0.version}

II. Configuration separation of Spring and SpringMVC

1. It is necessary to explain the order of execution configured in web.xml:

Listener > filter > servlet, while the same configuration fragment is executed from top to bottom.

2. The configuration section of web.xml. The following configuration information puts the configuration of Spring and SpringMVC to applicationContext*.xml and springmvc-servlet.xml, respectively.

Xml code

Org.springframework.web.context.ContextLoaderListener

ContextConfigLocation

Classpath*:applicationContext*.xml

Springmvc

Org.springframework.web.servlet.DispatcherServlet

ContextConfigLocation

Classpath:springmvc-servlet.xml

Springmvc

* .do

EncodingFilter

Org.springframework.web.filter.CharacterEncodingFilter

Encoding

Utf-8

ForceEncoding

True

EncodingFilter

/ *

3. The configuration segment of springmvc is as follows: springmvc-servlet.xml

Xml code

4. Only one spring configuration file, applicationContext.xml, is used in this example, as follows:

Xml code

5. About the xml header in the spring configuration file:

Xml code

6. At this point, you can use annotations in the code to configure the bean of spring, that is, the following code completes dependency injection:

Java code

@ Autowired

Private UserService userService

III. The integration of Spring and MyBatis

1. MyBatis is mainly used to implement a more flexible dao layer by using Mapper interface + writing sql in Mapper.xml, which remains unchanged after integration with spring.

The global configuration file for mybatis is SqlMapConfig.xml, which can also be another name.

2. Before consolidation, the connection information of the database is configured in SqlMapConfig.xml, and the scanning of Mapper is also configured in SqlMapConfig.xml.

The most troublesome thing is that there is a lot of redundancy in the code that completes the crud operation, which is in the form shown below:

Java code

/ / complete a new operation

InputStream is = Resources.getResourceAsStream ("SqlMapConfig.xml")

SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder () .build (is)

SqlSession session = sessionFactory.openSession ()

UserMapper userMapper = session.getMapper (UserMapper.class)

UserMapper.save (user)

Session.commit ()

Session.close ()

3. The purpose of integration with spring is to submit and close the creation of SqlSessionFactory, SqlSession, UserMapper and SqlSession to the spring container for management.

To achieve the effect of calling only one line of code, that is

Java code

UserMapper.save (user)

4. After integration, the connection information of the database and the scanned configuration fragments of Mapper are directly moved to applicationContext.xml to complete the injection of SqlSessionFactory, SqlSession and UserMapper.

Configuration snippet of mybatis in applicationContext.xml:

Xml code

5. There are only a few lines of configuration information left in the original SqlMapConfig.xml file

Xml code

6. Take the UserMapper.xml configuration fragment as an example to describe the concise programming mode after integration.

Xml code

Select sys_guid () from dual

Insert into t_user (id, name, password) values (# {id}, # {name}, # {password})

The method declaration in the UserMapper interface is as follows:

Java code

Public interface UserMapper {

Public void save (User user)

}

Call part of the code and inject it directly into UserMapper

Java code

@ Autowired

Private UserMapper userMapper

Public void save (User user) {

UserMapper.save (user)

}

Get [download address] [free support update]

The three major databases mysql oracle sqlsever are more professional, more powerful and suitable for different user groups.

[newly recorded video tutorials for this system, hand-in-hand teaching to develop a module to quickly master the system]

An integrated code generator [positive and negative two-way (single table, master table, parts list, tree table, development tool) + quickly build forms

Freemaker template technology, 0 code need not be written, generate a complete module, with pages, table building sql scripts, processing classes, service and other complete modules

B integrate Alibaba database connection pool druid

Database connection pool Alibaba's druid. Druid has obvious advantages in monitoring, scalability, stability and performance.

C integrated security rights framework shiro

Shiro is a framework implemented in Java language, which provides authentication and authorization through an easy-to-use API, which is more secure and reliable.

D Integrated ehcache distributed cache

Is a pure Java in-process cache framework, with fast, lean and other characteristics, widely used open source Java distributed cache.

E integrated Wechat interface development; F picture crawler technology; G SQL editor, support complex sql statements, generate reports, can export excel

H websocket timely communication technology; (instant chat, timely internal message and voice reminder, real-time online management, websocket timely page refresh)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report