1. Core configuration files

  • mybatis-config.xml
  • The MyBatis configuration file contains Settings and properties that will deeply affect MyBatis behavior

    Configuration (configuration) the properties (attributes) Settings (set) typeAliases type (alias) typeHandlers processor (type) the objectFactory (object factory) plugins (plug-in) Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments Environments

    1.1 Create Maven project

    Writing a tool class

    package com.jialidun.utils; //sqlSessionFactory--->sqlSession public class MybatisUtils{ private static SqlSessionFactory sqlSessionFactory; Static {try{// SqlSessionFactory object String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch(IOException e){ e.printStackTrace(); }} // Now that we have the SQLSessionFactory, as the name implies, we can get an instance of the SQLSession from it. // The SQLSession provides all the methods needed to execute SQL commands in the database. public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); }}

    Entity class

    package com.jialidun.pojo;
    
    @Data
    public class User{
    private int id;
    private String name;
    private String pwd;
    }

    interface

    package com.jialidun.dao; Public interface userDAO {list_user_list <User> getUserList(); User getUserById(int id); //insert User int addUser(User User); // updateUser int updateUser(User User); // deleteUser int deleteUser(int id); }

    Mapper.xml mapping file

    <? The XML version = "1.0" encoding = "utf-8"? > <! DOCTYPE mapper PUBLIC "- / / mybatis.org//DTD mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <! -- Namespace = Bound a corresponding DAO /Mapper interface --> < Mapper Namespace =" com.jialidun.dao.userdao "> <! ResultType ="com.jialidun.pojo.User"> select * from mybatis. User </select> from mybatis <select id="getUserById" parameterType="int" resultType="user"> select * from mybatis.user where id = #{id} </select> <! - properties in the object, > <insert id="addUser" parameterType="com.jialidun.pojo.User"> insert into mybatis. User (id,name, PWD) values(#{id},#{name},#{pwd}); </insert> <update id="updateUser" parameterType="com.jialidun.pojo.User"> update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}; </update> <delete id="deleteUser" parameterType="int"> delete from mybatis.user where id=#{id}; </delete> </mapper>

    test

    package com.jialidun.dao; Public class UserDaotest {@test public void Test (){ Get the SqlSession objects SqlSession SqlSession = MybatisUtils. GetSqlSession (); try{ UserDao userDao= sqlSession.getMapper(UserDao.class); List<User> userList = userDao.getUserList(); 2: / / way / / the List < User > userList. = sqlSession selectList (" com. Jialidun. Dao. UserDao. GetUserList "); for(User user:userList){ System.out.println(user); } }catch(Exception e){ e.printStackTrace(); }finally{// Close SQLSession SQLSession.close (); } } @Test public void getUserById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserDao.class); User user = mapper.getUserById(1); System.out,println(user); sqlSession.close(); } @Test public void addUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserDao.class); Int num = map.addUser (4, num, addUser); int num = addUser(4, num, addUser); If (num>0){system.out.println (" User added successfully! "); ); } // commit the transaction SQLSession.com MIT (); sqlSession.close(); } @Test public void updateUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserDao.class); Map. updateUser(new User(4," 666666","666666")); // commit the transaction SQLSession.com MIT (); sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserDao.class); mapper.deleteUser(4); // commit the transaction SQLSession.com MIT (); sqlSession.close(); }}

2. Environment Configuration (Encryments)

<? The XML version = "1.0" encoding = "utf-8"? > <! DOCTYPE configuration PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <! -- Configuration core configuration file --> <configuration> <! > <properties resource="db.properties"> <property name="username" value="root"/> <property name="password" value="1111"/> </properties> <! > < TypeAliases > < Package Name ="com.jialidun.pojo"/> </ TypeAliases > </ Environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/jialidun/dao/UserMapper/xml"/> </mappers> </configuration>

MyBatis can be configured to accommodate multiple environments but keep in mind that each SQLSessionFactory instance can only select one environment, although multiple environments can be configured. Learn how to configure multiple environments! MyBatis default transaction manager is JDBC, connection pool: POOLED

3. Properties

We can use the properties property to reference the configuration file

These properties can be configured externally and can be dynamically replaced. You can configure these properties either in a typical Java properties file or in a child of the properties element. 【 db. The properties 】

driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis? useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC%2B+8 username=root password=root

Introduced in the core configuration

<! > <properties resource="db.properties"> <property name="username" value="root"/> <property name="password" value="1111"/> </properties>
  • External files can be imported directly
  • You can add some property configuration to it
  • If two files have the same field, use the external configuration file first!

4. TypeAliases

  • A type alias can set an abbreviated name for a Java type
  • Fully qualified class name writing intended to reduce redundancy
<! > < TypeAlias > < TyoeAlias type=" com.jialidun.pojo.user "alias="User"/> </ TypeAliases >

You can also specify a package name. MyBatis will search for Java beans under the package name. For example, the default alias will be the class name of the class with lowercase first letter!

<! > < TypeAliases > < Package name="com.jialidun.pojo"/> </ TypeAliases >

If there are a lot of entity classes, it is recommended to use the second option. The first option allows you to customize the Alias, but the second option cannot be customized. If you have to change this option, you need to add an @Alias(” XXX “) annotation to the entity class

@Alias("user")
public class User{
  private int id;
  private String name;
  private String pwd;
}

5. Settings

These are extremely important tuning Settings in MyBatis, and they change the runtime behavior of MyBatis.



6. Other configurations

  • TypeHandlers (typeHandlers)
  • ObjectFactory (objectFactory)
  • MyBatis – Generator – Core MyBatis – Plus General Mapper

    7. Mappers

    MapperRegistry: To register and bind our Mapper files

    <mappers>
    <mapper resource="com/jialidun/dao/UserMapper/xml"/>
    </mappers>

    Method 2:

    <mappers>
    <mapper class="com.jialidun.dao.UserMapper"/>
    </mappers>

    Option three: use scan packets for injection binding

    <mappers>
    <package name="com.jialidun.dao"/>
    </mappers>

    Note:

  • The interface and his Mapper configuration file must have the same name
  • The interface and its Mapper configuration file must be under the unified package

Import the entity class name outside of the database configuration file to ensure that the UserMapper interface and UserMapper.xml are changed to be the same! And under the same package!