Mybatis入门(一)——第一个Mybatis程序

Mybatis

第一个Mybatis程序

思路:搭建环境–>导入Mybatis–>编写代码–>测试!

1、搭建环境

搭建数据库

create datebase `mybatis`;

use `mybatis`;

create table `user`(
    `id` int(20) not null,
    `name` varchar(30) default null,
    `pwd` varchar(30) default null
)engine=innodb default charset = utf8;

insert into `user`(`id`,`name`,`pwd`) values
(1,"于世梁","123456"),
(2,"张三","128893456"),
(3,"李四","1234676"),

新建项目

  1. 新建一个普通的Maven项目

  2. 删除src目录

  3. 导入Maven依赖

     <!--导入依赖-->
        <dependencies>
            <!--mysql驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.28</version>
            </dependency>
            <!--mybatis-->
            <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.9</version>
            </dependency>
            <!--junit-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
    

2、创建一个模块

  • 编写mybatis的核心配置文件
<?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.cj.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>
</configuration>
  • 编写mybatis的核心工具类
public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {

        try {
            //使用Mybatis工具类的第一步:获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //既然有了sqlSessionFactory,顾名思义,我们就可以从中获得SqlSession的实例。
    //SqlSession完全包含了面向数据库执行sql命令所需要的方法。
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

3、编写代码

  • 实体类
package com.ysl.pojo;

public class User {
    
    private int id;
    private String name;
    private String pwd;

    public User(){
        
    }
    
    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

  • Dao接口

    public interface UserDao {
        List<User> getUserList();
    }
    
  • 接口实现类由原来的impl转换为配置文文件

<?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=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.ysl.dao.UserDao">
    <!--select查询语句-->
    <select id="getUserList" resultType="com.ysl.pojo.User">
        select * from mybatis.user
    </select>
</mapper>

4、测试

注意点

org.apache.ibatis.binding.BindingException: Type interface com.ysl.dao.UserDao is not known to the MapperRegistry.

MapperRegistry是什么

核心配置文件中注册mappers

  • junit测试
@Test
public void test(){

    //获取sqlSession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //执行SQL
    UserDao mapper = sqlSession.getMapper(UserDao.class);
    List<User> userList = mapper.getUserList();

    for (User user : userList) {
        System.out.println(user);
    }

    //关闭sqlSession
    sqlSession.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值