码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis快速入门

时间:2017-12-30 20:25:43      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:let   iso   artifact   upd   pass   代码结构   bat   session   property   

 

代码结构

技术分享图片

 

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快速入门

标签:let   iso   artifact   upd   pass   代码结构   bat   session   property   

原文地址:https://www.cnblogs.com/weishao-lsv/p/8150965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!