MyBatis注解开发

MyBatis注解开发

注解开发

步骤

  • 1.创建接口和编写操作方法

  • 2.在核心配置文件中配置映射关系

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!--MyBitis的DTD约束,定义xml标签约束,使开发者按照定义书写-->
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <!--configuration 根标签-->
    <configuration>
        <!--引入数据库配置文件-->
        <properties resource="jdbc.properties" />
        <!--配置LOG4J-->
        <settings>
            <setting name="logImpl" value="log4j"/>
        </settings>
        <!--起别名-->
        <typeAliases>
            <package name="com.cmy.bean" />
        </typeAliases>
        <!--environments配置数据库环境,环境可以有很多个,而default属性则是指定某个环境-->
        <environments default="mysql1">
            <!--    environment数据库环境,id属性:唯一标识    -->
            <environment id="mysql1">
                <!--transactionManager事务管理,type 采用JDBC默认的事务管理-->
                <transactionManager type="JDBC" />
                <!--dataSource数据库源信息 type属性 连接池-->
                <dataSource type="POOLED">
                    <!--property 获取数据库连接的配置信息-->
                    <property name="driver" value="${driver}" />
                    <property name="url" value="${url}" />
                    <property name="username" value="${username}" />
                    <property name="password" value="${password}" />
                </dataSource>
            </environment>
        </environments>
        <!--mappers配置映射关系-->
        <mappers>
            <package name="com.cmy.mapper"/>
        </mappers>
    </configuration>
    
    

    使用<package>标签配置映射关系,指定对应接口所在包的映射关系

  • 3.编写测试类

查询操作

  • 1.创建接口和查询方法

    package com.cmy.mapper;
    
    import com.cmy.bean.Book;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    /**
     * @author chenmingyong
     */
    public interface BookMapper {
    	/**
    	 * 查询全部图书信息
    	 * @return 结果集
    	 */
    	@Select("SELECT * FROM BOOK")
    	public abstract List<Book> selectAll();
    }
    
  • 2.在核心配置文件中配置映射关系

  • 3.测试文件

    InputStream is = null;
    SqlSession sqlSession = null;
    try {
        is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = sqlSessionFactory.openSession(true);
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book> list = mapper.selectAll();
        for (Book book : list) {
            System.out.println(book);
        }
    }
    catch (Exception e){
        e.printStackTrace();
    }
    finally {
        if(sqlSession != null){
            sqlSession.close();
        }
        if(is != null){
            try {
                is.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

增加操作

  • 1.创建接口和增加方法

    package com.cmy.mapper;
    
    import com.cmy.bean.Book;
    import org.apache.ibatis.annotations.Insert;
    
    import java.util.List;
    
    /**
     * @author chenmingyong
     */
    public interface BookMapper {
    	/**
    	 * 插入一条数据
    	 * @param book 图书类实例对象
    	 * @return 返回影响行数
    	 */
    	@Insert("INSERT INTO book VALUES (#{book_id},#{book_name},#{book_author},#{book_publisher})")
    	public abstract Integer insert(Book book);
    }
    
  • 2.在核心配置文件中配置映射关系

  • 3.测试文件

    InputStream is = null;
    SqlSession sqlSession = null;
    try {
        is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = sqlSessionFactory.openSession(true);
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Book book = new Book(4, "测试", "测试", "测试");
        Integer result = mapper.insert(book);
        System.out.println(result);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    finally {
        if(sqlSession != null){
            sqlSession.close();
        }
        if(is != null){
            try {
                is.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

修改操作

  • 1.创建接口和修改方法

    package com.cmy.mapper;
    
    import com.cmy.bean.Book;
    import org.apache.ibatis.annotations.Update;
    
    import java.util.List;
    
    /**
     * @author chenmingyong
     */
    public interface BookMapper {
    	/**
    	 * 更新一条数据
    	 * @param book 图书类实例对象
    	 * @return 返回影响行数
    	 */
    	@Update("UPDATE book SET book_name=#{book_name},book_author=#{book_author},book_publisher=#{book_publisher}  WHERE book_id=#{book_id}")
    	public abstract Integer update(Book book);
    }
    
  • 2.在核心配置文件中配置映射关系

  • 3.测试文件

    InputStream is = null;
    SqlSession sqlSession = null;
    try {
        is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = sqlSessionFactory.openSession(true);
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Book book = new Book(4, "测试11", "测试11", "测试11");
        Integer result = mapper.update(book);
        System.out.println(result);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    finally {
        if(sqlSession != null){
            sqlSession.close();
        }
        if(is != null){
            try {
                is.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

删除操作

  • 1.创建接口和删除方法

    package com.cmy.mapper;
    
    import com.cmy.bean.Book;
    import org.apache.ibatis.annotations.Delete;
    
    import java.util.List;
    
    /**
     * @author chenmingyong
     */
    public interface BookMapper {
    	/**
    	 * 删除一条数据
    	 * @param book_id 图书id
    	 * @return 返回影响行数
    	 */
    	@Delete("DELETE FROM book WHERE book_id=#{book_id}")
    	public abstract Integer delete(Integer book_id);
    }
    
  • 2.在核心配置文件中配置映射关系

  • 3.测试文件

    InputStream is = null;
    SqlSession sqlSession = null;
    try {
        is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = sqlSessionFactory.openSession(true);
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Integer result = mapper.delete(6);
        System.out.println(result);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    finally {
        if(sqlSession != null){
            sqlSession.close();
        }
        if(is != null){
            try {
                is.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    
上一篇:SSM整合案例


下一篇:MyBatis 逆向工程