zoukankan      html  css  js  c++  java
  • MyBatis-Spring整合之方式3

    通过注解整合

    1.在UserDao同级目录下新建接口UserMapper,内容如下:

    public interface UserMapper {
        @Select("select * from user")
        public List<User> selectUser();
    }
    

    2.新建service包,改写UserDao和其实现类为UserService类和其实现类,如下:

    public interface UserService {
        public List<User> selectUser();
    }
    
    
    
    public class UserServiceImp implements UserService {
        private UserMapper userMapper;
        @Override
        public List<User> selectUser() {
            return userMapper.selectUser();
        }
    
        public void setUserMapper(UserMapper userMapper) {
            this.userMapper = userMapper;
        }
    }
    

    3.beans.xml新增bean标签、修改原UserDao标签,如下:

    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.lxy.dao.UserMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    <bean id="userService" class="com.lxy.service.imp.UserServiceImp">
        <property name="userMapper" ref="userMapper"/>
    </bean>
    

    4.修改test为新的类型,如下:

    public class test {
        public static void main(String[] args) throws IOException {
            ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
            UserService userService= (UserService) context.getBean("userService");
            System.out.println(userService.selectUser().size());
        }
    }
    
  • 相关阅读:
    Php排序
    php导出excel表
    yii的多表查询
    MySQL中自定义排序
    jquery中的obj.attr()和obj.data
    PhpStorm快捷键
    记2019年目标之一没有996的大数据分析BI实战历程
    2018年传统公司技术部门技术变迁和2019展望
    mpvue微信小程序开发随笔
    docker toolbox的redis 配置主从及哨兵模式保证高可用
  • 原文地址:https://www.cnblogs.com/inkqx/p/12316360.html
Copyright © 2011-2022 走看看