Mybatis全注解开发

  1. dao(mapper)层CRUD的注解替代了Mapper.xml文件
    查询 :@Select
    添加 :@Insert
    修改 :@Update
    删除 :@Delete
    package com.itheima.dao;
    
    import com.itheima.domain.Book;
    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 java.util.List;
    
    public interface BookDao {
    
        @Select("select * from tbl_book")
        List<Book> selectAll();
    
    
        @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
        boolean addBook(Book book);
    
        @Delete("delete from tbl_book where id = #{id}")
        boolean deleteById(Integer id);
    
        @Update("update tbl_book set type=#{type},name=#{name},description=#{description} where id=#{id}")
        boolean updateById(Book book);
    
        @Select("select * from tbl_book where id=#{id}")
        Book selectById(Integer id);
    }
    

  2.  在com.QT.config层下设置JdbcConfig类,MybatisConfig类来替代Mybatis-config.xml文件
    package com.itheima.config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.PlatformTransactionManager;
    
    import javax.sql.DataSource;
    
    @PropertySource("classpath:jdbc.properties")
    public class JdbcConfig {
    
        @Value("${jdbc.driver}")
        private String driver;
    
        @Value("${jdbc.url}")
        private String url;
    
        @Value("${jdbc.username}")
        private String username;
    
        @Value("${jdbc.password}")
        private String password;
    
        @Bean
        public DataSource getDataSource(){
    
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
    
            return dataSource;
        }
    //开启事务对象
        @Bean
        public PlatformTransactionManager getPlatformTransactionManager(DataSource dataSource){
            DataSourceTransactionManager manager = new DataSourceTransactionManager();
            manager.setDataSource(dataSource);
            return manager;
    
        }
    
    }
    
    package com.itheima.config;
    
    
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.mapper.MapperScannerConfigurer;
    import org.springframework.context.annotation.Bean;
    
    import javax.sql.DataSource;
    
    
    public class MybatisConfig {
    
        @Bean
        public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    
            bean.setDataSource(dataSource);
    //给实体类起别名
            bean.setTypeAliasesPackage("com.itheima.domain");
    
            return bean;
        }
    
        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer() {
            MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    //扫描指定的层
            configurer.setBasePackage("com.itheima.dao");
            return configurer;
        }
    }
    
  3. 若要在方法或者类开启事务只需要在其上方加入注解@

    Transactional​​​​​​

    并且需要在springConfig上加注解@EnableTransactionManagement
     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值