Mybatis usage steps (IDEA version)

1. Create project

Create a maven general project (no prototype is required). Select Maven project and continue to next

2.Pom.xml to create the parent project and add dependencies

For simple projects, you can directly take your own POM XML as the parent project, just add dependencies directly.

Here are the recommended methods:

  1. Right click project and create a new module as the parent

  2. In build POM XML add the following code:

    <modules>
            <module>parent</module>
            <module>module-a</module>
            <module>module-b</module>
            <module>module-c</module>
        </modules>
    
  3. POM in sub module Add the following code to XML to share the dependency of parent (note to modify it to its own attribute):

 <parent>
        <groupId>com.itranswarp.learnjava</groupId>
        <artifactId>parent</artifactId>
        <version>1.0</version>
        <relativePath>../parent/pom.xml</relativePath>
    </parent>

Add the following dependencies:

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>

After adding the dependency, find the Maven menu on the right and select POM. In the subdirectory XML, double-click install to complete the package guide.

After importing the package, you should be able to find the corresponding jar package in the External libraries and display the corresponding package during import.

If you don't know how to fill in the dependent information, you can go to this website to search: https://search.maven.org/

3. Add mybatis config XML file

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;
                useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    </mappers>
</configuration>

4. Write mybatis tool class

To build SqlSessionFactory from XML, you can write tool classes to encapsulate and obtain sqlSession objects:

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream  inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  static SqlSession getSqlSession(){
        return  sqlSessionFactory.openSession();
    }
}

5. Coding

  • Entity class pojo

  • dao interface -- change the habit to Mapper interface

public interface UserMapper {
     List<User> getUserList();
}
  • dao implementation class Impl -- change to map XML file, which is also the difference between mybatis and jdbc
<?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">
<mapper namespace="com.linkup.study.dao.UserMapper">
    <select id="getUserList" resultType="com.linkup.study.pojo.User">
        select * from user
    </select>
</mapper>
  • Write test method
public class UserMappterTest {
    @Test
    public void getUserList(){
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        List<User>list=userMapper.getUserList();
        for (User user:list
             ) {
            System.out.println(user);
        }
        session.close();

    }

}

problem

1.mybatis could not resolve type alias' user 'during web startup

For the path problem, the fully qualified name should be written instead of the short name:

<select id="getAll" resultType="User">
     Change to
 <select id="getUserList" resultType="com.linkup.study.pojo.User">

2.Error querying database. Cause: Communications link failure

No, SSL For website certificate authentication, you need to put mybatis config Change useSSL=true in XML to false

useSSL=false, which can't be seen from the error prompt. It plays a very important role. When using JDBC to connect to your database, your JDBC version is incompatible with MySQL version. MySQL version is higher. Add "useSSL =" true "after the connection statement to connect to the database. Later version. But if it is a compatible version, an error will occur.

Original link: https://blog.csdn.net/ponxbin/article/details/80608040

After further inspection, it is found that although the driver with 5.1 version is relied on, the driver with 8.0 is actually used for connection:

If you change it back to 5.1, you will find that it is incompatible. It is recommended to use the latest 8

--------

3. Map not found XML file

It may be that the storage location is in the module and is not specified in the module. It needs to be filtered. Add the following code to the root directory:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

4. Write map During the sql statement of the XML file, it is found that the corresponding database information is not prompted:

Setting SQL dialect to MySql.

Keywords: Mybatis

Added by lifeless on Tue, 25 Jan 2022 23:50:38 +0200