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

How to get started with bind in Mybatis and supporting JavaAPI from multiple data sources

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

Share

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

This article shows you how to quickly get started with bind in Mybatis and multiple data sources to support JavaAPI. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Bind// test bindList testBind (@ Param ("name") String name); select * from person where name like # {bidname} import dao.PersonMapper;import entity.Person;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream;import java.util.* / * * @ author found more highlights about official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and queries, unworldly affectation * / public class TestMain03 {public static void main (String [] args) throws Exception {String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream) Try (SqlSession session = sqlSessionFactory.openSession ()) {/ / get Mapper through sesson this Mapper can program Mybatis proxy Mapper PersonMapper mapper = session.getMapper (PersonMapper.class); List list = mapper.testBind ("Xiao Qiang"); Optional.ofNullable (list) .orElse (new ArrayList ()) .forEach (item-> {System.out.println (item);}) }}}

Bind allows you to use OGNL expressions to create a variable (for example, bidname) and then bind it to the current context

II. Multi-database support

I made a mistake for a long time. I wasted some time.

2.1 pom.xml

The version of the jar package I use is 3.4.5

4.0.0 org.example testDB 1.0-SNAPSHOT 8 8 org.mybatis mybatis 3.4.5 mysql mysql-connector-java 5.1.47 2.2 mybatis-config.xml

DatabaseIdProvider I used the default configuration without customization, the next day to write an example of a custom implementation class

Interface PersonMapperpackage dao;/** * @ author found more exciting official account: Muzi's round-the-clock programming shares the perception and learning of a programmer living at the bottom of the Internet doing additions, deletions, modifications and queries * @ create 2021-08-30 21:54 * / public interface PersonMapper {/ / the test returns the current time String testDb () } 2.4 xml PersonMapper.xml select CONCAT ("mysql-- >", # {_ databaseId}, "-->", now ()) from dual select "oracle-- >" | # {_ databaseId} from dual 2.5 Test import dao.PersonMapper;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession Import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream / * * @ author found more highlights about official account: Muzi's round-the-clock programming * A programmer living at the bottom of the Internet, doing additions, deletions, changes and queries, unworldly affectation * @ create 2021-09-02 21:42 * / public class TestMain {public static void main (String [] args) throws Exception {String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); SqlSession session = sqlSessionFactory.openSession (); / / get Mapper through sesson this Mapper will program Mybatis's agent Mapper PersonMapper mapper = session.getMapper (PersonMapper.class); String type = mapper.testDb (); System.out.println ("Database Type:" + type);}}

You can see that the driver package of Mysql is introduced into my pom, so the result here must be Mysql. If multiple packages are introduced, then the first matching of databaseIdProvider will be used by default, and multiple drivers will be introduced to write demo in the next article.

Output result:

Introduce multi-drive performance results

This is the end of this article on getting started with Mybatis bind in one hour and supporting Java API from multiple data sources. For more related Mybatis bind multiple data sources supporting Java API content, please search for previous articles or continue to browse the relevant articles below. I hope you will support it in the future!

The above content is how to quickly get started with bind in Mybatis and multiple data sources support JavaAPI. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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