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

Basic understanding of mybatis

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

Share

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

1. Main classes

1.1 SqlSessionFactoryBuilder

Used to create a SqlSessionFactory, either through a configuration file or code.

The main method

SqlSessionFactory build (InputStream inputStream) SqlSessionFactory build (InputStream inputStream, String environment) SqlSessionFactory build (InputStream inputStream, Properties properties) SqlSessionFactory build (InputStream inputStream, String env, Properties props) SqlSessionFactory build (Configuration config)

Created exampl

String resource = "org/mybatis/builder/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder (); SqlSessionFactory factory = builder.build (inputStream); DataSource dataSource = BaseDataTest.createBlogDataSource (); TransactionFactory transactionFactory = new JdbcTransactionFactory (); Environment environment = new Environment ("development", transactionFactory, dataSource); Configuration configuration = new Configuration (environment); configuration.setLazyLoadingEnabled (true); configuration.setEnhancementEnabled (true); configuration.getTypeAliasRegistry (). RegisterAlias (Blog.class); configuration.getTypeAliasRegistry (). RegisterAlias (Post.class) Configuration.getTypeAliasRegistry (). RegisterAlias (Author.class); configuration.addMapper (BoundBlogMapper.class); configuration.addMapper (BoundAuthorMapper.class); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder (); SqlSessionFactory factory = builder.build (configuration)

1.2 SqlSessionFactory

Used to create SqlSession

Main methods

SqlSession openSession () SqlSession openSession (boolean autoCommit) SqlSession openSession (Connection connection) SqlSession openSession (TransactionIsolationLevel level) SqlSession openSession (ExecutorType execType,TransactionIsolationLevel level) SqlSession openSession (ExecutorType execType) SqlSession openSession (ExecutorType execType, boolean autoCommit) SqlSession openSession (ExecutorType execType, Connection connection) Configuration getConfiguration

1.3 SqlSession

Persistent layer operation session

Common methods

T selectOne (String statement) List selectList (String statement) Map selectMap (String statement, String mapKey) int insert (String statement) int update (String statement) int delete (String statement)

Transaction operation method

Void commit () void commit (boolean force) void rollback () void rollback (boolean force)

2. Configuration file

Note:

Configuration file elements and JAVA manipulation methods can correspond to each other; configuration file elements can be independent, such as property files and annotations.

For details, refer to the official website address: http://www.mybatis.org/mybatis-3/zh/configuration.html#environments

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

Database

Wechat

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

12
Report