首页 > 代码库 > Mybatis 学习

Mybatis 学习

1. Mybatis的重要的配置文件 mybatis-config.xml 和 实体类的配置文件

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>
    <properties resource="jdbc.properties"/>
    <typeAliases>
        <package name="andy.com.model"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
    <!-- 将mapper文件加入到配置文件中 -->
    <mappers>
        <mapper class="andy.com.mapper.UserMapper"/>
    </mappers>
</configuration>

 

2. 实体类配置文件 User.xml , 对sql的映射,该实体类的配置文件一般和实体类放在一起,

<?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="andy.com.model.User">
<!-- <mapper namespace="andy.com.mapper.UserMapper"> -->  这个是另外一只方式,需要建一个UserMapper的接口
    <insert id="add" parameterType="User">
        insert into users (username,password,nickname,type)
            value(#{username},#{password},#{nickname},#{type})
    </insert>
    
    <update id="update" parameterType="User">
        update users set password=#{password},nickname=#{nickname},type=#{type} where id=#{id}
    </update>
    
    <delete id="delete" parameterType="int">
        delete from users where id=#{id}
    </delete>
    
    <select id="load" parameterType="int" resultType="User">
        select * from users where id=#{id}
    </select>
    
    <select id="list" resultType="User">
        select * from users
    </select>
</mapper>

 

3. Mybatsi  util 公共类, 完成对session的获取

package andy.com.util;

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 MyBatisUtil {
    private static SqlSessionFactory factory;
    static {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static SqlSession createSession() {
        return factory.openSession();
    }
    
    public static void closeSession(SqlSession session) {
        if(session!=null) session.close();
    }
}

4. 测试Mybatis

package andy.com.test;


import andy.com.model.User;
import andy.com.util.MyBatisUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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 org.junit.Test;

public class TestMyBatis {

    @Test
    public void testDelete() {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory f = new SqlSessionFactoryBuilder().build(is);
            SqlSession session = f.openSession();
            session.delete(User.class.getName()+".delete",1);
            session.commit();
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void testAdd() {
        SqlSession session = null;
        try {
            session = MyBatisUtil.createSession();
            User u = new User();
            u.setNickname("andy");
            u.setPassword("password");
            u.setType(1);
            u.setUsername("dawang");
            session.insert(User.class.getName()+".add", u);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            MyBatisUtil.closeSession(session);
        }
    }
    
    @Test
    public void testLoad() {
        SqlSession session = null;
        try{
            session = MyBatisUtil.createSession();
            User u = (User)session.selectOne(User.class.getName()+".load", 1);
            System.out.println(u.getNickname());
        } finally {
            MyBatisUtil.closeSession(session);
        }
    }
    
    @Test
    public void testList() {
        SqlSession session = null;
        try{
            session = MyBatisUtil.createSession();
            List<User> us = session.selectList(User.class.getName()+".list", null);
            System.out.println(us.size());
        } finally {
            MyBatisUtil.closeSession(session);
        }
    }
    
}

 

Mybatis 学习