mybatis创建第一个程序

1,Mybatis介绍

Mybatis就是帮助程序员将数据存入数据库中 , 以及从数据库中取数据 .是一个半自动化的ORM框架 (Object Relationship Mapping) -->对象关系映射

2,创建第一个Mybatis程序

思路流程:创建mysql数据库–>创建Idea普通的Maven项目–>pom.xml中导入相关Jar包(mybatis,mysql-connector-java,junit)–>编写mybatis-config.xml文件–>编写MyBatis工具类–>创建实体类–>编写Mapper接口类–>编写Mapper.xml配置文件–>编写测试类(Junit测试)–>运行测试
详细步骤:
1,创建mysql数据库

CREATE DATABASE mybatis_test
USE mybatis_test

CREATE TABLE student(
id INT PRIMARY KEY,
sname VARCHAR(10) NOT NULL,
ssex VARCHAR(2) DEFAULT '男'
)DEFAULT CHARSET=utf8 ENGINE=INNODB


INSERT INTO student(id,sname,ssex)
VALUES(1,'张三','男'),
(2,'小红','女')

INSERT INTO student(id,sname)
VALUES(3,'李四'),
(4,'王五')

2,创建Idea普通的Maven项目,导入相关jar包

<!--    导入相关jar包-->
<dependencies>
<!--    mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.5</version>
    </dependency>
<!--    mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
<!--    junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
</dependencies>

3,编写mybatis-config.xml文件(置于resources目录下,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.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/wj/dao/Mapper.xml"/>
    </mappers>
</configuration>

4,编写MyBatis工具类MybatisUtils

ublic 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();
           }
       }
//    获取sqlSession连接
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

5,创建数据库实体类

public class Student {
    private int id;     //ID
    private String sname;            //姓名
    private String ssex;             //性别
//    有参函数,无参函数构造

    public Student() {
    }

    public Student(int id, String sname, String ssex) {
        this.id = id;
        this.sname = sname;
        this.ssex = ssex;
    }
//    getter和setter方法

    public int getId() {
        return id;
    }

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

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }
//    toString()

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", sname='" + sname + '\'' +
                ", ssex='" + ssex + '\'' +
                '}';
    }
}

6,编写Mapper接口类

public interface StudentDao {
    List<Student> selectStudent();
}

7,编写Mapper.xml配置文件

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的mapper接口-->
<mapper namespace="com.wj.dao.StudentDao">
    <select id="selectStudent" resultType="com.wj.pojo.Student">
select * from student
</select>

8,编写测试类(Junit测试)

 @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List<Student> students = mapper.selectUser();
        for (Student student : students) {
            System.out.println(student);
        }
        sqlSession.close();
    }

9,展示结果
在这里插入图片描述
至此,Mybatis的第一个项目就算彻底完成了.
遇到的错误:
1,Type interface com.wj.dao.UserDao is not known to the MapperRegistry.
该错误是由于配置文件缺失配置导致的,在mybatis中添加如下的配置即可:

<mappers>
        <mapper resource="com/wj/dao/Mapper.xml"/>
    </mappers>

2,在项目路径下:target\classes\ 中报错路径下,对应的xml文件不存在,解决方法如下:
需要在pom.xml中加入以下代码:

 <!--在build中配置resources,来防止我们资源导出失败的问题-->
    <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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值