Spring, Spring MVC, and MyBatis Interview Questions and Answers
This article compiles common interview questions and concise answers about Spring, Spring MVC, and MyBatis, covering core concepts such as IoC, AOP, transaction management, controller behavior, annotation usage, design patterns, MyBatis configuration, mapping techniques, pagination, and integration with Ajax.
1. Spring Interview Questions
Q1: What role does Spring play in an SSM architecture? Spring is a lightweight framework that acts as a Bean factory, managing bean lifecycles and integrating with other frameworks. Its two core features are IoC/DI (injecting DAO into service, service into controller) and AOP.
Q2: How does Spring handle transactions? There are two approaches: programmatic transaction management (high flexibility but hard to maintain) and declarative transaction management (separates business code from transaction code using annotations or XML).
Q3: What is the purpose of IoC in a project? IoC resolves dependencies between objects by configuring bean relationships via XML or annotations, reducing coupling.
Q4: What typical contents are found in a Spring configuration file? Enabling transaction annotation support, defining a transaction manager, enabling component scanning, configuring the database, setting up the SQL session factory, aliases, mapper files, and eliminating the need to write DAO implementations.
Q5: Common Spring annotations include registration annotations (@Controller, @Service, @Component), injection annotations (@Autowired, @Resource), request mapping (@RequestMapping), and response handling (@ResponseBody).
Q6: Three DI methods are constructor injection, setter injection, and interface injection. Example constructor injection code:
<constructor-arg index="0" type="java.lang.String" value="宝马"/>Example setter injection code: <property name="id" value="1111"/> Q7: Design patterns used by Spring include Factory pattern (bean creation), Singleton pattern (default bean scope), and Proxy pattern (used in AOP).
Q8: Implementation principles of IoC and AOP IoC uses reflection to instantiate and inject objects; AOP relies on dynamic proxies.
2. Spring MVC Interview Questions
Q1: Are Spring MVC controllers singleton? Yes; this can cause thread‑safety issues in multi‑threaded environments. The solution is to avoid mutable fields and not use synchronization inside controllers.
Q2: Controller annotation is @Controller, indicating the class acts as a controller.
Q3: Purpose of @RequestMapping on a class It maps a URL pattern to the class (or specific handler methods) for request handling.
Q4: Binding multiple request parameters to an object Declare the object as a method parameter; Spring MVC automatically populates its fields.
Q5: Possible return types of controller methods String, ModelAndView, List, Set, etc.; commonly String for view names or Ajax responses, and List for collections.
Q6: Forward vs redirect Forward: return "hello"; Redirect: return "redirect:hello.jsp"; Q7: Interaction between Spring MVC and Ajax Use Jackson to convert Java objects to JSON, configure the mapper, and annotate methods with @ResponseBody to return objects directly.
Q8: Spring MVC workflow diagram
Q9: Differences between Struts2 and Spring MVC Entry point (filter vs servlet), development style (class‑based vs method‑based), and request handling (OGNL value stack vs parameter resolver with ModelAndView).
3. MyBatis Interview Questions
Q1: Ibatis vs MyBatis Ibatis was discontinued in 2010 and handed over to Google, later renamed MyBatis, which is its actively maintained successor.
Q2: Interface binding in MyBatis MyBatis binds DAO interfaces to XML mapper files, automatically generating implementations and simplifying data access.
Q3: When to use annotations vs XML Use annotations for simple SQL statements; use XML for more complex mappings.
Q4: Core processing class SqlSession.
Q5: Handling mismatched column and property names Define explicit
<result column="title" property="title" javaType="java.lang.String"/>mappings.
Q6: Benefits of MyBatis Separates SQL from Java code, abstracts JDBC, auto‑maps result sets to JavaBeans, allows flexible SQL, supports @Param for parameters.
Q7: One‑to‑many mapping example
<collection property="topicComment" column="id" ofType="com.tmf.bbs.pojo.Comment" select="selectComment"/>Q8: One‑to‑one mapping example
<association property="topicType" select="selectType" column="topics_type_id" javaType="com.tmf.bbs.pojo.Type"/>Q9: Difference between ${} and #{} in MyBatis ${} performs simple string replacement without type conversion; #{} creates a prepared statement placeholder (?), preventing SQL injection.
Q10: Retrieve last generated primary key select last_insert_id() Q11: Pagination methods Use RowBounds object or write physical pagination SQL.
Q12: MyBatis working principle
Steps: Build SqlSessionFactory from mybatis‑config.xml, open a SqlSession, obtain Mapper, execute mapped SQL, perform CRUD and transaction commit, then close the SqlSession.
Conclusion The author invites feedback, encourages sharing, and provides a QR code for more Java content.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
