Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)

文章目录

    • 一、导入依赖
    • 二、编写User实体类
    • 三、编写持久层UserMapper接口
    • 四、编写UserMapper映射文件
    • 四、编写mybatis-config.xml配置文件
    • 四、编写jdbc.properties 修改优化mybatis-config.xml配置文件
    • 六、编写UserMapperTests测试类
    • 七、demo_user数据库表模板
    • 八、mybatis入门案例总结
      • 1. mybatis入门案例步骤:
      • 2. mybatis常见错误:
        • 2.1 BindingException
          • 2.1.1 Type interface xxx is not known to the MapperRegistry
          • 2.1.2 Invalid bound statement (not found)


一、导入依赖

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.tedugroupId>
    <artifactId>mybatis_demo01artifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.26version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.6version>
        dependency>

        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.20version>
            <scope>providedscope>
        dependency>
    dependencies>
project>

二、编写User实体类

User
Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第1张图片

@Data
@Accessors(chain = true)
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}

三、编写持久层UserMapper接口

UserMapper
Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第2张图片

public interface UserMapper {
    List<User> getAllUser();   
}

四、编写UserMapper映射文件

UserMapper.xml
Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第3张图片


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cy.mapper.UserMapper">
    <select id="getAllUser" resultType="cn.cy.pojo.User">
        select * from demo_user
    select>
mapper>

四、编写mybatis-config.xml配置文件

mybatis-config.xml
Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第4张图片

 

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.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/demo_admin?serverTimezone=UTC&rewriteBatchedStatements=true&characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            dataSource>
        environment>
    environments>
    
    <mappers>
        <mapper resource="mappers/UserMapper.xml"/>
    mappers>
configuration>

四、编写jdbc.properties 修改优化mybatis-config.xml配置文件

jdbc.properties
Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第5张图片

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo_admin?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

mybatis-config.xml
Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第6张图片


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    
    <properties resource="jdbc.properties">properties>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>
    
    <mappers>
        <mapper resource="mappers/UserMapper.xml"/>
    mappers>
configuration>

六、编写UserMapperTests测试类

UserMapperTests
Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第7张图片

public class UserMapperTests {

    @Test
    public void testGetAllUser() throws IOException {
        /*创建SqlSessionFactory*/
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /*从SqlSessionFactory中获取sqlSession*/
        SqlSession sqlSession = sqlSessionFactory.openSession();
        /*获取mapper接口,执行接口方法*/
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getAllUser();
        // [User(id=1, name=zs, age=18, sex=男), User(id=2, name=ls, age=20, sex=男), User(id=3, name=xiaohong, age=18, sex=女)]
        System.out.println(userList);
        sqlSession.close();
    }
}

七、demo_user数据库表模板

Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第8张图片

八、mybatis入门案例总结

1. mybatis入门案例步骤:

  • 导入依赖
    • MySQL
    • Mybatis
    • Juint
    • lombok
  • 编写实体类,实体类属性需要与数据库表字段一一对应
  • 编写Mapper接口,添加接口方法
  • 编写Mapper映射文件
  • 编写mybatis-config.xml配置文件
    • 配置数据源信息
    • 配置映射关系
  • 测试类测试
    • 读取配置文件,创建SqlSessionFactory工厂对象
    • 获取SqlSession会话对象,开启数据库连接
    • 获取持久层接口实现类
    • 调用方法,获取返回值结果
    • 关闭SqlSession链接

2. mybatis常见错误:

2.1 BindingException

2.1.1 Type interface xxx is not known to the MapperRegistry

实际报错信息:xxx接口的类型在Mapper注册中未知

org.apache.ibatis.binding.BindingException: Type interface
cn.cy.mapper.UserMapper is not known to the MapperRegistry.

解决方案:

检查 xml映射文件中 namespace 命名是否与想要执行的接口一致

Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第9张图片

2.1.2 Invalid bound statement (not found)

实际报错信息:无效的绑定语句(未找到)

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.cy.mapper.UserMapper.getAllUser

解决方案:

检查 xml映射文件中 id 的值是否与想要执行的接口中方法一致

Mybatis入门(二)Mybatis入门案例(Maven - mybatis原生)_第10张图片

你可能感兴趣的:(#,Mybatis,mybatis,maven,java)