MyBatis(1)--MyBatis入门示例

1.MyBatis下载

mysql驱动jar包必须与mysql数据库版本相对应

2.第一个示例:

1>.数据库sql创建:

create database mybatis;
use mybatis;
CREATE TABLE t_role(id INT, role_name VARCHAR(20), note VARCHAR(20));
INSERT INTO t_role VALUES(1,'Tom', 'teacher');
INSERT INTO t_role VALUES(2,'Kobe', 'player');

2>.创建数据库表对应的JavaBean:

package com.anlw;

public class Role {
    private Integer id;
    private String roleName;
    private String note;
    public Role() {
        super();
    }
    public Role(Integer id, String roleName, String note) {
        super();
        this.id = id;
        this.roleName = roleName;
        this.note = note;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    @Override
    public String toString() {
        return "Role [id=" + id + ", roleName=" + roleName + ", note=" + note + "]";
    }
}

3>.定义该表的查询接口:

package com.anlw;

public interface RoleMapper {

    Role getRole(Integer id);

}

4>.定义该表的查询接口的映射器(RoleMapper.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">
<mapper namespace="com.anlw.RoleMapper">
    <select id="getRole" resultType="com.anlw.Role" parameterType="int">
        select id,role_name roleName,note from t_role where id = #{id}
    </select>
</mapper>

5>.定义构建SqlSessionFactory的配置文件(mybatis-config.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>
    <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" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="RoleMapper.xml" />
    </mappers>
</configuration>

6>.构建SqlSessionFactory,测试Main方法:

package com.anlw;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        Role role = roleMapper.getRole(2);
        System.out.println(role.toString());
    }
}

3.源码下载:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值