zoukankan      html  css  js  c++  java
  • springboot与通用mapper的整合

    找到springboot工程下的pom.xml文件,导入如下的依赖jar包

    <!--配置通用Mapper start-->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>2.1.5</version>
            </dependency>
            <!--通用Mapper end-->

    配置UserDao接口,继承通用mapper,注意泛型

    @Repository
    public interface UserDao extends Mapper<User> {
     
    }

    Service的实现层

    @Service("userService")
    public calss UserServiceImpl implements UserService{
        @Autowired
        private UserDao userDao;
    
        @Override
        public List<User> list(){
        return userDao.selectALL();
        }
    
    }
        

    User实体类配置通用文件mapper的主键和主键返回策略

    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
        private  String name;
        private  Integer age;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
    }

    配置入口类Application

    import tk.mybatis.spring.annotation.MapperScan;//把MapperScan的导入路径换成tk.mybatis.spring.annotation.MapperScan
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    @SpringBootApplication
    @EnableTransactionManagement //开启书屋管理注解模式 最新的版本可以省略
    @MapperScan("com.xz.springboot.mapper") //扫描该包下所有的接口并为该接口生成实现类
    public class Springboot01Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot01Application.class, args);
        }
    
    }

     在application.properties中

    #开启mybatis驼峰命名自动转换
    mybatis.configuration.map-underscore-to-camel-case=true
  • 相关阅读:
    设计模式学习--Abstarct Factory
    WPF使用Canvas绘制可变矩形
    离线安装 Android 4.0 SDK
    引用了System.Configuration命名空间,却找不到ConfigurationManager类
    LIBXML2库使用指南2
    Quartz任务调度实践
    FastJson 自定义Serialize、Parser
    maven安装与创建多模块项目
    Zookeeper源码调试环境踩坑记录
    Apache Dubbo时间轮HashedWheelTimer算法的实现原理
  • 原文地址:https://www.cnblogs.com/sitian2050/p/11824825.html
Copyright © 2011-2022 走看看