其他分享
首页 > 其他分享> > Mybatis入门

Mybatis入门

作者:互联网

一、入门

(1)、Mybatis的项目结构

在这里插入图片描述

(2)、配置文件

1、properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/exercisedb?characterEncoding=utf-8
jdbc.username=root
jdbc.password=admin57

2、xml文件

<?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>
       <properties resource="db.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="com/bjsxt/Mapper/UserMapper.xml"/>
    </mappers>

  </configuration>

(3)、Mapper层

1、UserMapper

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

2、UserMapper.xml

<?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">
<mapper namespace="com.bjsxt.Mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.bjsxt.entity.User">
        <!--@Table user-->
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="userpassword" column="userpassword" jdbcType="VARCHAR"/>
    </resultMap>
    
    <select id="selectAll" resultMap="BaseResultMap">
        select * from exercisedb.user
    </select>

</mapper>

(4)、实体类 User

package com.bjsxt.entity;

import java.io.Serializable;

/**
 * (User)实体类
 *
 * @author makejava
 * @since 2021-10-11 21:56:25
 */
public class User implements Serializable {
    private static final long serialVersionUID = -50885993925997685L;
    private Integer id;
    private String username;
    private String userpassword;
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUserpassword() {
        return userpassword;
    }
    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", userpassword='" + userpassword + '\'' +
                '}';
    }
}

(5)、测试类

package com.bjsxt;

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

import com.bjsxt.Mapper.UserMapper;
import com.bjsxt.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

public class Test {
    public static void main(String[] args) throws Exception {
        String resource = "SqlMapperClient.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE);
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<User> users = userMapper.selectAll();
            users.stream().forEach(System.out::println);
        } finally {
            sqlSession.close();
        }
    }
}

(6)、运行结果

在这里插入图片描述

标签:username,String,userpassword,id,Mybatis,import,public,入门
来源: https://blog.csdn.net/wxn3213/article/details/120727225