您当前的位置: 首页 >  彭世瑜 Java

Java编程:MyBatis读取数据库数据

彭世瑜 发布时间:2019-01-19 10:42:02 ,浏览量:1

1、文件结构

在这里插入图片描述 一共涉及3个文件夹8个文件,心累… 注意config文件夹和lib文件夹需要设置为资源文件夹,不然找不到文件

2、依赖下载

放入lib文件夹 mybatis: https://github.com/mybatis/mybatis-3/releases mysql-connector: https://dev.mysql.com/downloads/connector/j/

3、数据准备

demo的数据库下有一个names 的数据表,字段类型和数据如下

mysql> desc names;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(10)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  | UNI | NULL    |                |
| age   | int(10)      | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

mysql> select * from names;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  2 | 大红   |   27 |
|  3 | 大壮   |   24 |
|  4 | 秀英   |   25 |
+----+--------+------+
4、Person类编写

其实就是一个普通的java类 Person.class


public class Person {
    private Integer id;
    private  String name;
    private Integer age;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    public String toString(){
        return "[Person] id: "+ id + " name: " + name + " age :"+ age;
    }
}

5、配置映射器

方式一 文件配置映射器: 映射接口 PersonMapper.class

public interface PersonMapper {
    public Person getPersonById(Integer id);
}

映射文件 PersonMapper.xml






    
    select id,name, age from names where id = #{id}
  

方式二 注解配置映射器: PersonMapperAnnoation.class

import org.apache.ibatis.annotations.Select;

public interface PersonMapperAnnoation {
        @Select("select id, name, age from names where id = #{id}")
        public Person getPersonById(Integer id);
}

这样看来,方式2更为简洁和清晰

6、数据库配置

填入数据库的地址,账号,密码 配置映射器,可以使用两种方式resourceclass




    
        
            
            
            
            
                
                
                
                
            
        
    
    
    
        
        
    

7、代码测试
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 java.io.IOException;
import java.io.InputStream;

public class mybatisDemo {
    public static void main(String[] args) throws IOException{

        //1.根据MyBatis的配置文件,即mybatis-config.xml创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //2.获取session实例开启会话,其能直接执行*已经映射的SQL语句*
        SqlSession session = sqlSessionFactory.openSession();
        try {
        	// 方式1 查询
            //3.获取接口的实现类对象
            PersonMapper personMapper = session.getMapper(PersonMapper.class);
            //4.执行查询操作
            Person person = personMapper.getPersonById(2);
            System.out.println(person);
			
			// 方式2 查询
            //3.获取接口的实现类对象
            PersonMapperAnnoation personMapperAnnoation = session.getMapper(PersonMapperAnnoation.class);
            //4.执行查询操作
            Person person1 = personMapperAnnoation.getPersonById(2);
            System.out.println(person1);

        } finally {
            //4.关闭会话session
            session.close();
        }
    }

    //根据MyBatis的配置文件,即mybatis-config.xml创建SqlSessionFactory
    public static SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }

}

/*
两种方式都能查到数据
[Person] id: 2 name: 大红 age :27
[Person] id: 2 name: 大红 age :27
*/

参考 (二)MyBatis学习笔记-HelloWorld

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 1浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0989s