1. 程式人生 > >Mybatis 學習

Mybatis 學習

final evel manage led config XML public users delete

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 學習