mybatis快速入门

Posted weishao-lsv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis快速入门相关的知识,希望对你有一定的参考价值。

 

代码结构

技术分享图片

 

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快速入门的主要内容,如果未能解决你的问题,请参考以下文章

13.2 MyBatis Generator 快速入门(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》

MyBatis快速入门

MyBatis学习总结——MyBatis快速入门

MyBatis学习总结——MyBatis快速入门

mybatis介绍和mybatis快速入门

MyBatis学习总结——MyBatis快速入门