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

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

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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下,如下:

1
2
3
4
5
6
7
8
9
10
11
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函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
        }
    }

程序结构如下:

 

posted @   leagueandlegends  阅读(203)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示