首页 > 其他 > 详细

mybatis无接口直接调用xml的helloworld

时间:2015-04-08 23:26:57      阅读:585      评论:0      收藏:0      [点我收藏+]

1、首先在库里面创建一张表,sql语句如下:

CREATE TABLE `users` (
   `id` INT(11) NOT NULL AUTO_INCREMENT,
   `name` VARCHAR(20) COLLATE utf8_bin DEFAULT NULL,
   `age` INT(11) DEFAULT NULL,
   PRIMARY KEY (`id`)
 );
insert  into `users`(`id`,`name`,`age`) values (1,‘tome‘,12),(2,‘jack‘,11);

2、新建一个普通的java项目,将mybatis-3.2.8.jar、mysql-jdbc.jar添加到项目中。
3、新建与表名对应的实体类,具体代码如下:

public class Users {

    private Integer id;
    private String name;
    private Integer age;

    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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

4、编写操作数据库的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="IUserMapper">
    <select id="findById" parameterType="int" resultType="com.evan.pojo.Users">
        select *
        from users where id=#{id}
    </select>
</mapper>    

5、编写mybatis配置文件,具体如下:

<?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>
    <!--数据源配置 这块用 mysql数据库 -->
    <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/mybatishelloworld" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 -->
        <mapper resource="com/evan/dao/IUserMapper.xml" />
    </mappers>
</configuration> 

6、测试类内容如下:

package com.evan;

import java.io.IOException;

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.evan.pojo.Users;

public class MybatisHelloWorldTest {

    public static void main(String[] args) throws IOException {
        String resource = "mybatisConfig.xml";

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsReader(resource));

        SqlSession session = sessionFactory.openSession();

        Users user = session.selectOne("IUserMapper.findById", 1);

        System.out.println(user);
    }

}

7、项目目录截图如下:
技术分享
8、项目源码下载地址(免积分):http://download.csdn.net/download/zl544434558/8575601

9、mybatis的需要声明接口的helloworld博文地址:
http://blog.csdn.net/zl544434558/article/details/12620941

mybatis无接口直接调用xml的helloworld

原文:http://blog.csdn.net/zl544434558/article/details/44947301

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!