mybatis--使用接口注解的方式实现Helloword

首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下:

create database my;

use my;
create table user(
id int(10) auto_increment,
name varchar(64),
level varchar(256),
phone varchar(256),
primary key(id)
);

insert into user(id,name,level,phone) values(1,'c','a','1234555666');

  

其次,创建mybatis配置文件Configure.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>
    <typeAliases>
        <typeAlias alias="user" type="com.zk.models.user" />
    </typeAliases>

    <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/my" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!-- // power by http://www.yiibai.com 注释掉咯...
        <mapper resource="com/yiibai/mybatis/models/User.xml" />-->
    </mappers>
</configuration>

接着,创建实体类user.java,放在com.zk.models下,

package com.zk.models;

public class user {
private Integer id;
    
    private String name;
    
    private String level;
    
    private String phone;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "user [id=" + id + ", name=" + name + ", level=" + level
                + ", phone=" + phone + "]";
    }
}

再次,创建一个IUser接口,放置在com.zk.dao下,如下:

package com.zk.dao;

import org.apache.ibatis.annotations.Select;

import com.zk.models.user;

public interface IUser {
    /*注解方式实现*/
    @Select("select * from user where id=#{id}")
    public user getUserById(int id);
}

最后,实现一个main函数

package Main;
import java.io.Reader;
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 com.zk.dao.IUser;
import com.zk.models.user;


public class Main {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;
    static {
        try {
            reader = Resources.getResourceAsReader("config/Configure.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            sqlSessionFactory.getConfiguration().addMapper(IUser.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession() {
        return sqlSessionFactory;
    }

    public static void main(String[] args) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            IUser iuser = session.getMapper(IUser.class);
            user user = iuser.getUserById(1);
            System.out.println("名字:"+user.getName());
            System.out.println("所属部门:"+user.getLevel());
            System.out.println("主页:"+user.getPhone());
        } finally {
            session.close();
        }
    }
} 

程序结构如下:

 

转载于:https://www.cnblogs.com/longlyseul/p/11220176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值