Mybatis快速入门

2022-03-06 10:23:29 浏览数 (1)

1. MyBatis开发步骤

  • MyBatis官网地址:http://www.mybatis.org/mybatis-3/ 在这里插入图片描述在这里插入图片描述
  1. 添加MyBatis的坐标
代码语言:html复制
<dependencies>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
	<dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>1.2.17</version>
	</dependency>

</dependencies>
  1. 创建user数据表 在这里插入图片描述在这里插入图片描述
  2. 编写User实体类 public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" "id=" id ", username='" username ''' ", password='" password ''' '}'; } }
  3. 编写映射文件UserMapper.xml
代码语言:html复制
<?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="userMapper">

    <select id="findAll" resultType="com.xdr630.domain.User">
        select * from user
    </select>
    
</mapper>
  1. 编写核心文件SqlMapConfig.xml
代码语言:html复制
<?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"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载配置映射文件-->
    <mappers>
        <mapper resource="com/xdr630/mapper/UserMapper.xml"></mapper>
    </mappers>
</configuration>
  1. 编写测试类
代码语言:java复制
public class MybatisTest {

    @Test
    public void test() throws IOException {
        //获取核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
        //获得sqlsession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace   id
        List<User> userList = sqlSession.selectList("userMapper.findAll");
        //打印数据
        System.out.println(userList);
        //释放资源
        sqlSession.close();
    }


}
在这里插入图片描述在这里插入图片描述

2. Mybatis 映射文件概述

在这里插入图片描述在这里插入图片描述

0 人点赞