浅浅请留步

给我一个键盘,我能敲出整个世界!

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.源码下载:

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/an_2016/article/details/77487975
文章标签: mybatis java
个人分类: MyBatis
想对作者说点什么? 我来说一句

MyBatis经典入门实例

2010年08月02日 2KB 下载

Spring Mybatis 示例

2011年08月14日 9.56MB 下载

testMybatis

2017年06月29日 2.32MB 下载

mybatis helloword

2014年10月27日 1.07MB 下载

没有更多推荐了,返回首页

不良信息举报

MyBatis(1)--MyBatis入门示例

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭