Mybatis-01

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyj4495673/article/details/82770570
mybatis简介
  • 支持普通sql查询,存储过程,高级映射的持久层框架
  • 减少了jdbc代码和手工设置(可以参考jdbc链接数据库进行比较)
  • 可以使用简单的xml或注解用于配置原始映射,简单化
应用图解

在这里插入图片描述

建立简单链接
  • 新建javaProject项目,在src下新建lib文件夹,添加mybatis的jar包

  • 连接数据库需要用到mysql的驱动包,将mysql的jar包也添加到lib文件下
    在这里插入图片描述

  • 创建简单数据库表

CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE users( id PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),age INT);
INSERT INTO users(NAME,age)VALUE('Tom',12);
INSERT INTO users(NAME,age)VALUE('jack',11);
  • 根据数据库建立实体类
    private int id;
    private String name;
    private int age;
//配置conf.xml文件用来链接数据库
//后期可以根据 <properties resource="db.properties"/>对下面配置进行优化
   <!--
        development : 开发模式
        work : 工作模式,发布以后
     -->
    <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?characterEncoding=utf-8" />
                 <!--根据自己数据库的用户名和密码配置-->
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
  1. 添加配置文件的映射文件Mapper.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">
<!--确定映射文件的namespace是唯一的包名+文件名-->
<mapper namespace="com.atguigu.day03_mybaits.test1.userMapper">
    <!--
        根据id查询得到一个user对象
     -->
    <!--parameterType类型 resultType返回类型得到user对象,根据id不同返回不同结果-->
    <select id="selectUser" parameterType="int" resultType="com.atguigu.day03_mybatis.test1.User">
        select * from users where id=#{id}
    </select>
</mapper>
  1. 添加了mapper.xml文件后要去conf.xml里注册一下
 <mappers>
        <!--去找mapper的文件位置-->
        <mapper resource="com/atguigu/day03_mybatis/test1/userMapper.xml"/>
    </mappers>
  1. 建立test类来测试是否连接成功
public static  void main(String[] args)throws IOException{
        String resource = "conf.xml";
        
        //不知道来源,先考虑sessionfactory,通过上sessionfactory来input一个is
        InputStream is =  Test.class.getClassLoader().getResourceAsStream(resource);
        
        //先写sessionfactory对象出来,build传一个inputstream,iniputstream是为了加载配置文件的
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        
        //打开一个session
        SqlSession session = factory.openSession();
        
        //执行一个查询,找到映射文件里的某一个标签
        String statement = "com.atguigu.day03_mybaits.test1.userMapper"+".selectUser";
        
        //查询一个对象,返回一个为一个的user对象这里要关联源码
        User user= session.selectOne(statement,2);
        System.out.println(user.getName());
        System.out.println(user.getAge());
    }

打印结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lyj4495673/article/details/82770570