关闭

MyBatis(1)--MyBatis入门示例

标签: mybatisjava
65人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41583次
    • 积分:625
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:0篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论