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

What are the steps for Mybatis_day06:Mybatis to integrate Spring?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the steps for Mybatis_day06:Mybatis to integrate Spring". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

MyBatis-Spring will help you integrate MyBatis code seamlessly into Spring. It will allow MyBatis to participate in Spring's transaction management, create mappers mapper and SqlSession and inject them into bean, and convert Mybatis's exceptions to Spring's DataAccessException. In the end, it is possible to make the application code independent of MyBatis,Spring or MyBatis-Spring.

Mybatis integrates spring

Integration thinking

The SqlSessionFactory object should be placed in the spring container as a singleton.

In the traditional way of developing dao, you should get the sqlsession object from the spring container.

In the form of a Mapper proxy, you should get the mapper proxy object directly from the spring container.

Database connection and database connection pool transaction management are handed over to the spring container to complete.

Integrate the required jar packages

Spring's jar package

Mybatis's jar package

Integration package for Spring+mybatis.

Mysql's database-driven jar package.

The jar package for the database connection pool.

Steps for integration

Step 1: create a java project.

Step 2: import the jar package. (the jar package mentioned above)

Step 3: mybatis configuration file sqlmapConfig.xml

Step 4: write the configuration file for Spring

1. Database connection and connection pool

2. Transaction management (can not be configured for the time being)

3. SqlsessionFactory object, configured in the spring container

4. The mapeer proxy object or the dao implementation class is configured into the spring container.

Step 5: write dao or mapper files

Step 6: test.

SqlMapConfig.xml applicationContext.xml

Db.properties

Jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8 jdbc.username=root jdbc.password=root

The development of Dao

There are three ways to implement dao:

1. The development mode of traditional dao

2. Develop by using mapper agent.

3. Configure the mapper proxy using the scan package.

The development mode of traditional dao

Interface + implementation class to complete. The dao implementation class needs to inherit the SqlsessionDaoSupport class

Dao implementation class

Public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {@ Override public User findUserById (int id) throws Exception {SqlSession session = getSqlSession (); User user = session.selectOne ("test.findUserById", id); / / cannot close SqlSession and let the spring container complete / / session.close (); return user;} @ Override public void insertUser (User user) throws Exception {SqlSession session = getSqlSession (); session.insert ("test.insertUser", user); session.commit (); / / session.close ();}}

Configure dao

Configure the dao implementation class into the spring container

Test method initialization: private ApplicationContext applicationContext; @ Before public void setUp () throws Exception {String configLocation = "classpath:spring/ApplicationContext.xml"; / / initialize the spring runtime environment applicationContext = new ClassPathXmlApplicationContext (configLocation);} Test: @ Test public void testFindUserById () throws Exception {UserDao userDao = (UserDao) applicationContext.getBean ("userDao"); User user = userDao.findUserById (1); System.out.println (user);}

Developing dao in the form of 1.Mapper Agent

a. Develop mapper interface

Develop mapper files

Configure mapper proxy

* * Test method public class UserMapperTest {private ApplicationContext applicationContext; @ Before public void setUp () throws Exception {applicationContext = new ClassPathXmlApplicationContext ("classpath:spring/applicationContext.xml"); * * @ Test public void testGetUserById () {UserMapper userMapper = applicationContext.getBean (UserMapper.class); User user = userMapper.getUserById (1); System.out.println (user);}}

Scan packet configuration mapper

The id of each mapper proxy object is the class name, with the first letter lowercase

This is the end of the content of "what are the steps for Mybatis_day06:Mybatis to integrate Spring". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report