美文网首页
mybatis中,如何用java注解方式实现sql语句查询?

mybatis中,如何用java注解方式实现sql语句查询?

作者: 我不是刺猬_ | 来源:发表于2019-10-22 20:28 被阅读0次

    简介: mylbatis除了xml方式实现sql语句动态查询外,用java注解的方式也可以实现,即将sql作为注解参数,在主类中,直接调用接口方法,即相当于调用了查询语句。

    1. 总体步骤:
    1)建立sql语句注解接口。
    2)mybatis-config.xml添加<mapper class = "注解类路径">
    3)main方法中,根据sqlSession传入注解类.class,拿到注解类引用,调用响应sql语句。

    1)建立注解类

    package com.how2java.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    
    import com.how2java.pojo.Category;
    
    public interface CategoryMapper {
        @Insert("insert into Category_ (name) values(#{name})")
        public int add(Category category);
        
        @Select("select * from Category_ where id = #{id}")
        public Category select(int id);
        
        @Delete("delete from Category_ where id = #{id}")
        public void delete(int id);
        
        @Update("update Category_ set name = #{name} where id = #{id}")
        public Category update(Category category);
        
        public List<Category> list();
    }
    

    2)配置文件添加注解类映射

    <mappers>
            <mapper resource = "com/how2java/pojo/Category.xml"/>
            <mapper class = "com.how2java.mapper.CategoryMapper"/> 
        </mappers>
    

    3)main方法进行测试

    package com.how2java.test;
    
    import java.io.InputStream;
    import java.util.List;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.how2java.mapper.CategoryMapper;
    import com.how2java.pojo.Category;
    
    public class TestMybatis2 {
        public static void main(String[] args) throws Exception{
            String source = "mybatis-config.xml";
            InputStream stream = Resources.getResourceAsStream(source);
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(stream);
            SqlSession session = sessionFactory.openSession();
            CategoryMapper mapper = session.getMapper(CategoryMapper.class);
            
            update(mapper);
            select(mapper);
            delete(mapper);
            add(mapper);
            
            session.commit();
            session.close();
        }
        
        private static void update(CategoryMapper mapper) {
            Category category = mapper.select(3);  //注解类接口引用直接调用注解方法,相当于执行sql语句。
            System.out.println(category.getName());
            category.setName("newname");
            mapper.update(category);
            listAll(mapper);
            
        }
        
        private static void select(CategoryMapper mapper) {
            Category c = mapper.select(4);
            System.out.println(c.getId() + c.getName());
        }
        
        private static void delete(CategoryMapper mapper) {
            mapper.delete(2);
            listAll(mapper);
        }
        
        private static void add(CategoryMapper mapper) {
            Category c = new Category();
            c.setName("new name");
            mapper.add(c);
            
            listAll(mapper);
        }
        
        private static void listAll(CategoryMapper mapper) {
            List<Category> list= mapper.list();
            for(Category c: list) {
                System.out.println(c.getId() + c.getName());
            }
        }   
    }
    
    

    相关文章

      网友评论

          本文标题:mybatis中,如何用java注解方式实现sql语句查询?

          本文链接:https://www.haomeiwen.com/subject/odscvctx.html