欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis3注解的动态SQL

程序员文章站 2022-07-13 16:10:05
...
1. MyBatis3注解的动态SQL

@InsertProvider
@UpdateProvider
@DeleteProvider
@SelectProvider

新建MyBatisPro04项目


package com.andrew.mappers;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import com.andrew.model.Student;
public class StudentDynaSqlProvider {
    public String insertStudent(final Student student) {
        return new SQL() {
            {
                INSERT_INTO("t_student");
                if (student.getName() != null) {
                    VALUES("name", "#{name}");
                }
                if (student.getAge() != null) {
                    VALUES("age", "#{age}");
                }
            }
        }.toString();
    }
    public String updateStudent(final Student student) {
        return new SQL() {
            {
                UPDATE("t_student");
                if (student.getName() != null) {
                    SET("name=#{name}");
                }
                if (student.getAge() != null) {
                    SET("age=#{age}");
                }
                WHERE("id=#{id}");
            }
        }.toString();
    }
    public String deleteStudent(){
        return new SQL() {
            {
                DELETE_FROM("t_student");
                WHERE("id=#{id}");
            }
        }.toString();
    }
    public String getStudentById() {
        return new SQL() {
            {
                SELECT("*");
                FROM("t_student");
                WHERE("id=#{id}");
            }
        }.toString();
    }
    public String findStudents(final Map<String,Object> map) {
        return new SQL() {
            {
                SELECT("*");
                FROM("t_student");
                StringBuffer sb = new StringBuffer();
                if (map.get("name") != null) {
                    sb.append(" and name like '"+map.get("name")+"'");
                }
                if (map.get("age") != null) {
                    sb.append(" and age="+map.get("age"));
                }
                if (!sb.toString().equals("")) {
                    WHERE(sb.toString().replaceFirst("and", ""));            
                }
            }
        }.toString();
    }
}


package com.andrew.mappers;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import com.andrew.model.Student;
public interface StudentMapper {
    @InsertProvider(type=StudentDynaSqlProvider.class,method="insertStudent")
    public int insertStudent(Student student);
    @UpdateProvider(type=StudentDynaSqlProvider.class,method="updateStudent")
    public int updateStudent(Student student);
    @DeleteProvider(type=StudentDynaSqlProvider.class,method="deleteStudent")
    public int deleteStudent(int id);
    @SelectProvider(type=StudentDynaSqlProvider.class,method="getStudentById")
    public Student getStudentById(Integer id);
    @SelectProvider(type=StudentDynaSqlProvider.class,method="findStudents")
    public List<Student> findStudents(Map<String,Object> map);
}


package com.andrew.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.andrew.mappers.StudentMapper;
import com.andrew.model.Student;
import com.andrew.util.SqlSessionFactoryUtil;
public class StudentTest {
    private SqlSession sqlSession = null;
    private StudentMapper studentMapper = null;
    @Before
    public void setUp() throws Exception {
        sqlSession = SqlSessionFactoryUtil.openSession();
        studentMapper = sqlSession.getMapper(StudentMapper.class);
    }
    @After
    public void tearDown() throws Exception {
        sqlSession.close();
    }
    @Test
    public void testInsert() {
        Student student = new Student("琪琪",11);
        studentMapper.insertStudent(student);
        sqlSession.commit();
    }
    @Test
    public void testUpdate() {
        Student student = new Student(17, "琪琪2", 12);
        studentMapper.updateStudent(student);
        sqlSession.commit();
    }
    @Test
    public void testDelete() {
        studentMapper.deleteStudent(17);
        sqlSession.commit();
    }
    @Test
    public void testGetById() {
        Student student = studentMapper.getStudentById(14);
        System.out.println(student);
    }
    @Test
    public void testFindStudents() {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name", "%赵%");
        map.put("age", 22);
        List<Student> studentList = studentMapper.findStudents(map);
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}
相关标签: mybatis