1. 程式人生 > >mybatis快速入門

mybatis快速入門

source nbsp statement ade man domain gets version mes

代碼結構

技術分享圖片

pom.xml

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
    </dependencies>

conf.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://10.37.148.37:3306/metadata?characterEncoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="cn/domain/mapper.xml" />
    </mappers>

</configuration>

mapper.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="cn.domain.mapper">
    
    <select id="findDeteleIds" resultType="cn.domain.VO">
        select id 
        from database_basic 
        where isorigin
=#{isorigin} and parent_id=0 </select> <select id="findUpdateIds" resultType="cn.domain.VO"> select id from database_basic where isorigin=#{isorigin} and parent_id=#{parentId} </select> <select id="updateById"> update database_basic <set> parent_id = 0 </set> where id=#{id,jdbcType=BIGINT} </select> <select id="deleteById" resultType="cn.domain.VO"> delete from database_basic where id=#{id} </select> </mapper>

VO

public class VO {
    private Long id;
    private Long parent_id;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getParent_id() {
        return parent_id;
    }
    public void setParent_id(Long parent_id) {
        this.parent_id = parent_id;
    }
}

TestCase

public class TestCase {
    
    @Test
    public void testUser(){
        String resource = "conf.xml"; 
        InputStream is = TestCase.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        
        
        String isorigin = "2";
        
        //要刪除的ids
        List<Long> delList = new ArrayList<Long>();
        String findDeteleIds = getStatement("findDeteleIds");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("isorigin", isorigin);
        List<VO> list = session.selectList(findDeteleIds, map);
        for(VO u:list){
            delList.add(u.getId());
        }
        
        //要修改的ids
        List<Long> updateList = new ArrayList<Long>();
        for(Long id:delList){
            String findUpdateIds = getStatement("findUpdateIds");
            Map<String,Object> upMap = new HashMap<String,Object>();
            upMap.put("isorigin", isorigin);
            upMap.put("parentId", id);
            List<VO> tmpList = session.selectList(findUpdateIds, upMap);
            for(VO u:tmpList){
                updateList.add(u.getId());
            }
        }
        
        
        //執行修改
        for(Long id:updateList){
            System.out.println("修改id--->"+id);
            String updateById = getStatement("updateById");
            Map<String,Object> upMapId = new HashMap<String,Object>();
            upMapId.put("id", id);
            session.update(updateById, upMapId);
            session.commit();
        }
        
        //執行刪除
        for(Long id:delList){
            System.out.println("刪除id--->"+id);
            String deleteById = getStatement("deleteById");
            Map<String,Object> delMapId = new HashMap<String,Object>();
            delMapId.put("id", id);
            session.update(deleteById, delMapId);
            session.commit();
        }
        
    }
    
    private String getStatement(String index){
        return "cn.domain.mapper."+index;
    }
}

mybatis快速入門