Mybatis的使用

Mybatis的使用

Mybatis简介

定义言简意赅:

mybatis是java的持久层框架

mybatis可以隐藏jdbc繁杂的API

只需关注sql语句

解决实体和数据库映射问题

依赖引入
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.27</version>
</dependency>
<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.22</version>
      <scope>provided</scope>
</dependency>
核心配置
<?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">
    	<!--使用jdbc事务管理,目前由mybatis来管理-->
            <transactionManager type="JDBC"/><!--mysql数据库连接的各种信息-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mp?
useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;allowMultiQuerie
s=true&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UserMapper.xml"/><!--配置映射文件-->
    </mappers>
</configuration>
映射文件
<?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.learn.mapper.UserMapper"><!--配置映射接口-->
    <!--List<User> findAll();-->
    <select id="findAll" resultType="com.learn.pojo.User">
        select * from tb_user
    </select>
</mapper>
映射接口
import com.learn.pojo.User;
import java.util.List;

public interface UserMapper {
    List<User> findAll();
}
实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String user_Name;
    private String password;
    private String name;
    private Integer age;
    private String email;
}
测试类
import com.learn.mapper.UserMapper;
import com.learn.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.InputStream;
import java.util.List;

public class MybaitsTest {
    @Test
    public void testFindAll() throws Exception{
        String url = "mybatis-config.xml";
        //加载配置文件
        InputStream inputStream = Resources.getResourceAsStream(url);
        //创建SqlSessionFactory对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession
        SqlSession sqlSession = build.openSession();
        //使用SqlSession对象获取映射接口对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

文件目录

Mybatis的使用_第1张图片

小结

请多多指教!!

你可能感兴趣的:(JavaLearn,java,数据库,junit,mybatis)