mybatis 3.4.1 Step By Step

1.下载mybatis3.4.1

3.引入mybatis3.4.1必须的jar包

mybatis-3.4.1.jar

4.完整的配置文件和代码


mybatis 3.4.1 Step By Step
 

4.1 创建数据库和表,并写入数据

CREATE DATABASE mybatis;

USE mybatis;

CREATE TABLE user (

  id int(11) NOT NULL AUTO_INCREMENT,

  userName varchar(50) DEFAULT NULL,

  userAge int(11) DEFAULT NULL,

  userAddress varchar(200) DEFAULT NULL,

  PRIMARY KEY (`id`)

)

INSERT INTO user VALUES ('1', 'mybatis练习', '30', '山东济南');

4.2 mybatis-conf

<?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.checker.mybatis.model.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://127.0.0.1:3306/mybatis" />
            <property name="username" value="root"/>
            <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/checker/mybatis/model/UserMapper.xml"/>
    </mappers>
</configuration>

4.3 User.java

package com.checker.mybatis.model;

public class User {
    private int id;
    private String userName;
    private String userAge;
    private String userAddress;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserAge() {
        return userAge;
    }
    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }
    public String getUserAddress() {
        return userAddress;
    }
    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }
}

4.4 UserMapper.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.checker.mybatis.models.UserMapper">
    <select id="selectUserByID" parameterType="int" resultType="User">
        select * from `user` where id = #{id}
    </select>
</mapper>

4.5 Test.java

package com.checker.mybatis.test;
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.checker.mybatis.model.User;

public class Test {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader; 

    static{
        try{
            reader    = Resources.getResourceAsReader("mybatis-conf.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }
    
    public static void main(String[] args) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
	        User user = (User) session.selectOne("com.checker.mybatis.models.UserMapper.selectUserByID",new Integer(1));
	        System.out.println(user.getUserAddress());
	        System.out.println(user.getUserName());
        } finally {
	        session.close();
        }
    }
}

 以上信息参考 www.yihaomen.com/article/java/303.htm

5.程序运行的结果


mybatis 3.4.1 Step By Step
 

相关推荐