mybatis简单的开发过程(数据库查询)

mybatis简单的开发过程(数据库查询)

以下是一个简单的mybaits开发过程以及遇到的问题:

1.基本环境的搭建

先导入相关的包或者依赖
junit,mybatis,mysql,spring,aop,mybatis-spring等等

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
    </dependencies>

    <!-- 在build中配置resources, 以防止我们资源导出失败的问题 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

2.回忆mybatis
编写实体类 编写核心配置文件 编写接口 编写mapper.xml 测试

链接数据库 创建表User表
创建User的java实体类与之对应

package com.hui.pojo;

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}

编写配置文件mybatis-config.xml

  <properties resource="db.properties"></properties>
    <typeAliases>
        <package name="com.hui.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 class="com.hui.dao.UserMapper"></mapper>
    </mappers>
  

编写dao接口

package com.hui.dao;

import com.hui.pojo.User;

import java.util.List;

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

编写其对应的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hui.dao.UserMapper">
    <select id="selectUser" resultType="user">
        select * from mybatis.user
    </select>
    
</mapper>

编写测试类进行测试

import com.hui.dao.UserMapper;
import com.hui.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest {
    @Test
    public void test() throws IOException {
        String resource= "mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

结果查询正常
User(id=1, name=妲己, pwd=376563)
User(id=3, name=李华, pwd=456)
User(id=4, name=甄姬, pwd=1234566342)
User(id=5, name=玄女, pwd=23412353)
User(id=10, name=虞姬, pwd=23412353)

遇见的问题:

org.apache.ibatis.binding.BindingException: Type interface com.hui.dao.UserMapper is not known to the MapperRegistry.
由于没有进行sqlMapper.xml文件的注册 也就是说我们每一个sqlMapper.xml文件都需要到核心配置文件中进行注册

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

注意利用class进行注册要保证 接口和映射文件的同包同名

资源文件导出失败的问题
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.hui.dao.UserMapper.selectUser
我们需要配置maven 的资源导出 我们在pom.xml中加入以下的代码防止资源导出的失败

    <!-- 在build中配置resources, 以防止我们资源导出失败的问题 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值