Spring+MyBatis实例详解

1.项目结构:

                Spring+MyBatis实例详解_第1张图片

2.项目的Maven依赖:


        4.3.21.RELEASE
    

    
        
            org.projectlombok
            lombok
            1.18.4
        
        
            org.mybatis
            mybatis-spring
            1.3.2
        
        
            org.springframework
            spring-tx
            ${spring-version}
        
        
            org.springframework
            spring-jdbc
            ${spring-version}
        
        
            org.springframework
            spring-core
            ${spring-version}
        
        
            org.springframework
            spring-context
            ${spring-version}
        
        
            org.springframework
            spring-context-support
            ${spring-version}
        
        
            org.springframework
            spring-aop
            ${spring-version}
        
        
            org.springframework
            spring-beans
            ${spring-version}
        
        
            org.springframework
            spring-web
            ${spring-version}
        
        
            org.springframework
            spring-webmvc
            ${spring-version}
        
        
            org.springframework
            spring-test
            test
            ${spring-version}
        
        
            com.alibaba
            fastjson
            1.2.54
        
        
            org.mybatis
            mybatis
            3.4.6
        
        
            org.postgresql
            postgresql
            42.2.5
        
        
            junit
            junit
            4.12
            test
        
    

3.Java类:
   3.1 Student.java

package com.lance.mybatis.demo.entity;

import lombok.Data;
import org.springframework.stereotype.Component;

@Component
@Data
public class Student {
    private String id;
    private String name;
    private byte age;
    private String sex;

}

3.2 StudentMapper.java

package com.lance.mybatis.demo.mapper;

import com.lance.mybatis.demo.entity.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentMapper {

    void addStudent(Student s);

    void deleteStudentById(@Param("id") String id);

    int updateStudent(Student s);

    List findStudent(@Param("limit") int limit, @Param("offset") int offset);

    Student findById(@Param("id") String id);

    List findByIds(@Param("ids") List ids);
}

4.配置文件:

4.1 mybatis-config.xml




  
    
    
    
    
    
    
    
    
    
    
    
    
    
  

  
    
  


4.2 StudentMapper.xml




    
        update student set name=#{name},age=#{age},sex=#{sex} where id=#{id}
    

    
        
        
        
        
    


    

    

    
        insert into student(id,name,age,sex) VALUES (#{id},#{name},#{age},#{sex})
    

    
        delete from student where id = #{id}
    

  


4.3 applicationContext.xml



    
        
        
        
        
    

    
        
        
    
    
        
        
    


5.单元测试
StudentMapperTest.java

package com.lance.mybatis.demo.mapper;


import com.alibaba.fastjson.JSON;
import com.lance.mybatis.demo.entity.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class StudentMapperTest {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void getStudent() {
        List students = studentMapper.findStudent(2,1);
        System.out.println(JSON.toJSONString(students));
    }

    @Test
    public void addStudent() {
        Student student = new Student();
        student.setId("20181204");
        student.setName("wanger");
        student.setAge((byte) 20);
        student.setSex("男");

        studentMapper.addStudent(student);
    }

    @Test
    public void deleteStudentById() {

        studentMapper.deleteStudentById("20181201");

    }


    @Test
    public void findById() {

        Student student = studentMapper.findById("20181201");
        System.out.println(JSON.toJSONString(student));
    }

    @Test
    public void updateStudent() {
        Student student = new Student();
        student.setId("20181201");
        student.setName("xiaoming");
        student.setAge((byte) 20);
        student.setSex("男");

        studentMapper.updateStudent(student);
    }

   @Test
    public void findByIds() {
        List ids = new ArrayList();
        ids.add("20181203");
        ids.add("20181204");

        List students = studentMapper.findByIds(ids);
        System.out.println(JSON.toJSONString(students));
    }

}

你可能感兴趣的:(spring,Mybatis)