In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Mybatis global configuration and mapping relationship how to achieve, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
I. contents of the configuration file
Mybatis.xml is the global configuration file for Mybatis.
The global configuration file requires a constraint file in the header.
The top-level structure of the profile is as follows:
Configuration (configuration) properties-- property: load external configuration file For example, database connection information Settings-- global configuration parameters: for example, log configuration typeAliases-- type alias typeHandlers-- type processor objectFactory-- object factory Plugins-- plug-in: for example paging plug-in Environments-- environment collection property object environment (environment variable) transactionManager (transaction manager) dataSource (data source) Mappers-- mapper: register the mapping file 1.1, Proerties
Properties can be configured externally and can be replaced dynamically. We can either set it in the child elements of the properties element (such as the properties node in Datasource) or configure it in the java properties file.
There are four parameter data connected to the database in the data source, which are usually placed in a special property file, and the global configuration file of mybatis reads the data directly from the property file.
1. Create a jdbc.properties under the resources directory
Jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8&serverTimezone=Asia/Shanghaijdbc.username=rootjdbc.password=123@qwe
2. Introduce configuration files into mybatis.xml
3. Use the values in the properties file
1.2.Setting setting
Extremely important tuning settings in Mybatis that change the running behavior of Mybatis, such as logging.
1.3.Type alias typeAliases
You can set an abbreviated alias for the java type for xml configuration only in order to reduce the redundancy of fully qualified class name writing.
Some common types of aliases are already supported in MyBatis, as follows:
Type of alias mapping _ bytebyte_longlong_shortshort_intint_integerint_doubledouble_floatfloat_booleanbooleanstringStringbyteBytelongLongshortShortintIntegerintegerIntegerdoubleDoublefloatFloatbooleanBooleandateDatedecimalBigDecimalbigdecimalBigDecimalobjectObjectmapMaphashmapHashMaplistListarraylistArrayListcollectionCollectioniteratorIterator
Custom aliases are also supported:
1.4.Mapper Mappers
1.4.1. Use resource references relative to the classpath
Syntax:
Use resources relative to the classpath to find files from the classpath path
For example:
1.4.2. Use the mapper interface to implement the fully qualified class name
Syntax:
Requirement: the interface and mapping file have the same name as the package
1.4.3. Register all in-packet mapper interfaces as mappers
Recommend
Syntax:
Specify all mapper interfaces under the package
For example:
Note: this method requires that the Mapper interface name is the same as the mapper mapping file name and is in the same directory
1.5 、 dataSource
Access to database in Mybatis supports connection pooling technology, and uses its own connection pooling technology. Configure it in the mybatis.xml file to create the corresponding type of data source DataSource based on the type attribute.
There are three categories of Mybatis data sources:
UNPOOLED: do not use connection pool data sources
POOLED: using connection pooling data sources
JNDI: a data source implemented using JNDI
The first two data sources implement the javax.sql.Datasource interface.
1.6. Transaction
Manually commit transactions by default:
The Mybatis framework encapsulates JDBC, so the control mode of Mybatis transaction itself is also using the commit () and rollback () methods of the JDBC connection object, and the setAutoCommit () of the connection object sets the transaction commit mode to manual or automatic.
TransactionManager specifies the transaction manager used by Mybatis. Support: JDBC and MANAGED
The JDBC transaction management mechanism is committed through the commit () method of the Connection object and rolled back by the rollback () method. By default, Mybatis will turn off auto-commit. If you look at the log, you can see that either commit or rollback needs to be set manually.
MANAGED: the container manages the entire lifecycle of the transaction (such as the Spring container)
The openSession () method of SqlSessionFactory is overloaded, and you can set the autocommit mode.
If sqlSession = SqlSessionFactory.openSession (true)
The parameter is set to true, and there is no need to execute sqlSession.commit () to perform the addition, deletion and modification again, and the transaction will be committed automatically.
II. Relational mapping in Mybatis
The table structure is as follows:
2.1. One-to-one mapping
Add the entity class Player:
Public class Player {private Integer playerId; private String playerName; private Integer playerNum; private Integer teamId; private Team team1; public Player () {} public Player (Integer playerId, String playerName, Integer playerNum, Integer teamId) {this.playerId = playerId; this.playerName = playerName; this.playerNum = playerNum; this.teamId = teamId } @ Override public String toString () {return "Player {" + "playerId=" + playerId + ", playerName='" + playerName +'\'+ ", playerNum=" + playerNum + ", teamId=" + teamId + ", team1=" + team1 +'}' } public Integer getPlayerId () {return playerId;} public void setPlayerId (Integer playerId) {this.playerId = playerId;} public String getPlayerName () {return playerName;} public void setPlayerName (String playerName) {this.playerName = playerName;} public Integer getPlayerNum () {return playerNum;} public void setPlayerNum (Integer playerNum) {this.playerNum = playerNum } public Integer getTeamId () {return teamId;} public void setTeamId (Integer teamId) {this.teamId = teamId;} public Team getTeam1 () {return team1;} public void setTeam1 (Team team1) {this.team1 = team1;}}
Mapper interface:
Public interface PlayerMapper {Player queryById (int playerId); Player queryById1 (int playerId); Player queryById2 (int playerId); Player queryById3 (int playerId);}
Mapper mapping file:
Select * from player where playerId=# {playerId} select * from player an inner join team b on a.teamId=b.teamId where a.playerId=# {playerId} select * from player an inner join team b on a.teamId=b.teamId where a.playerId=# {playerId} select * from player where playerId=# {playerId}
Test class:
Public class PlayerMapperTest {private PlayerMapper mapper=MybatisUtil.getSqlSession () .getMapper (PlayerMapper.class); @ Test public void test1 () {Player player = mapper.queryById1 (1); System.out.println (player);} @ Test public void test2 () {Player player = mapper.queryById2 (1); System.out.println (player);} @ Test public void test3 () {Player player = mapper.queryById3 (1) System.out.println (player);}} 2.2, one to many mapping
Modify the entity class Team.java:
Public class Team {/ * team ID * / private Integer teamId; / * team name * / private String teamName; / * team address * / private String location; / * creation time * / private Date createTime; / * team assembly * / private List playerList ...}
Add method in TeamMapper API:
Team queryById1 (int teamId); Team queryById2 (int teamId)
Add method in PlayerMapper API:
List queryByTeamId (int teamId)
Add a mapping to TeamMapper.xml:
Add a mapping to PlayerMapper.xml:
Select * from player where teamId=# {teamId}
Add test methods to the test class:
@ Test public void test13 () {TeamMapper mapper = sqlSession.getMapper (TeamMapper.class); Team team = mapper.queryById1 (1025); System.out.println (team);} @ Test public void test14 () {TeamMapper mapper = sqlSession.getMapper (TeamMapper.class); Team team = mapper.queryById2 (1025); System.out.println (team) } Thank you for reading this article carefully. I hope the article "how to realize the global configuration and mapping of Mybatis" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.