• mybatis无mapper.xml用法


    在datasource配置类上加上

    @MapperScan("cn.x.x.dao")
    @Configuration

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>cn.x</groupId>
        <artifactId>xxxtest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-java8</artifactId>
                <version>4.2.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-spring</artifactId>
                <version>4.2.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>4.2.0</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>3.11.0</version>
            </dependency>
            
            <dependency>
                <groupId>com.squareup.okio</groupId>
                <artifactId>okio</artifactId>
                <version>1.14.0</version>
            </dependency>
            
            <dependency>
                <groupId>com.google.code.findbugs</groupId>
                <artifactId>jsr305</artifactId>
                <version>3.0.2</version>
            </dependency>
            
            <dependency>
                <groupId>org.conscrypt</groupId>
                <artifactId>conscrypt-openjdk-uber</artifactId>
                <version>1.1.4</version>
            </dependency>
            
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.49</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.12</version>
            </dependency>
            
             <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.12</version>
            </dependency> 
            
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    pom
    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://1.1.1.1:3306/databasename?useSSL=false
        username: root
        password: root
        filters: stat
        maxActive: 20
        initialSize: 5
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        testWhileIdle: true
        validationQuery: select 1
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
    appliaction.yml
    package cn.x.x.config;
    
    import javax.sql.DataSource;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    
    @MapperScan("cn.x.x.dao")
    @Configuration
    public class DataSourceConfig {
    
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource dataSource(){
            return new DruidDataSource();
        }
        
        @Bean
        public SqlSessionFactory sqlSessionFactory(){
            SqlSessionFactory sqlSessionFactory = null;
            try{
                SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
                sqlSessionFactoryBean.setDataSource(dataSource());
                sqlSessionFactory = sqlSessionFactoryBean.getObject();
            }catch (Exception e){
                e.printStackTrace();
            }
            return sqlSessionFactory;
        }
    
        @Bean
        public ServletRegistrationBean druidServlet(){
            ServletRegistrationBean reg = new ServletRegistrationBean();
            reg.setServlet(new StatViewServlet());
            reg.addUrlMappings("/druid/*");
            reg.addInitParameter("loginUsername","admin");
            reg.addInitParameter("loginPassword","admin123");
            reg.addInitParameter("logSlowSql","true");
            return reg;
        }
    
        @Bean
        public FilterRegistrationBean filterRegistrationBean(){
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(new WebStatFilter());
            filterRegistrationBean.addUrlPatterns("/*");
            filterRegistrationBean.addInitParameter("profileEnable","true");
            return filterRegistrationBean;
        }
    }
    DataSourceConfig.java
    package cn.x.x.dao;
    
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    
    public interface RegUtil {
        
        @Update("update `databasename`.`t_user` set`user_name` = '${user_name}',`idcard` = '${idcard_no}',`gender` = '男',`race` = '汉',`birthdate` = '1990-09-27 00:00:00',`address` = `宛平路47号',`valid_date` = '2017.12.11-2037.12.11',`id_expired_time` = '2037-12-11 00:00:00'WHERE`id` = '${reg_user_id}'")
        public boolean updateUser(@Param("user_name")String user_name,@Param("idcard_no")String idcard_no,@Param("reg_user_id")String reg_user_id);
        
        @Insert("insert into databasename.t_user_bankcard(user_id,issue_bank,issue_bank_abbr,card_type,account_no,phone_no,idcard) values('${reg_user_id}','民生银行','CMBC','2','${bankcard_no}','${mobile}','${idcard_no}')" )
        public boolean insertBankCard(@Param("reg_user_id")String reg_user_id,@Param("bankcard_no")String bankcard_no,@Param("mobile")String mobile,@Param("idcard_no")String idcard_no);
        
        
        @Select("select count(1) from databasename.t_user where idcard = ${idcard} ")
        int getIdcardCount(@Param("idcard")String idcard);
        
    }
    dao
  • 相关阅读:
    localStorage cache
    webpack的学习过程
    npm install --save/--save-dev的区别
    .gitignore常见问题
    jQuery的优点与缺点
    JSONP是什么
    Node.js-Usage & Example
    【转】Swig Getting Started
    【转】使用Spring MVC统一异常处理实战
    C++-Qt【5】-QT的QString,char*,QByteArray转化以及中文乱码的问题
  • 原文地址:https://www.cnblogs.com/xiaodebing/p/10024456.html
Copyright © 2020-2023  润新知