springboot整合mybatis出現的一些問題


springboot整合mybatis非常非常的簡單,簡直簡單到發指。但是也有一些坑,這里我會詳細的指出會遇到什么問題,並且這些配置的作用

 

整合mybatis,無疑需要mapper文件,實體類,dao層,數據庫連接池。。。。。也就沒了。

先放配置application.yml

spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
        
        name: test
        url: jdbc:mysql://localhost:3306/mama-bike?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
        username: root
        password: root

mybatis:
  #告訴spring你的mapper的位置。
  mapper-locations: classpath:com/coder520/mamabike/**/**.xml
  #告訴spring你的實體類的位置
  type-aliases-package: classpath:com.coder520.mamabike.**.entity

logging:
  config: classpath:logback.xml
View Code

dao層接口      //就簡單的寫一個方法

public interface UserMapper {

    int insert(User record);

   
}
View Code

mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.coder520.mamabike.user.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.coder520.mamabike.user.entity.User" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="nickname" property="nickname" jdbcType="VARCHAR" />
    <result column="enable_flag" property="enableFlag" jdbcType="TINYINT" />
    <result column="verify_flag" property="verifyFlag" jdbcType="TINYINT" />
    <result column="head_img" property="headImg" jdbcType="VARCHAR" />
    <result column="mobile" property="mobile" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, nickname, enable_flag, verify_flag, head_img, mobile
  </sql>
  <insert id="insert" parameterType="com.coder520.mamabike.user.entity.User" >
    insert into user (id, nickname, enable_flag, 
      verify_flag, head_img, mobile
      )
    values (#{id,jdbcType=BIGINT}, #{nickname,jdbcType=VARCHAR}, #{enableFlag,jdbcType=TINYINT}, 
      #{verifyFlag,jdbcType=TINYINT}, #{headImg,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}
      )
  </insert>
</mapper>
View Code

main方法

@SpringBootApplication
@ComponentScan(basePackages={"com.coder520.mamabike"})
@MapperScan(basePackages="com.demo.user.mapper")
public class MamaBikeApplication {

    public static void main(String[] args) {
        SpringApplication.run(MamaBikeApplication.class, args);
    }
}
View Code

 

需要注意的是,dao層接口spring怎么會知道呢?這里就需要@MapperScan(basePackages="com.demo.user.mapper") 這個注解來指定mapper接口的位置。用@ComponentScan(basePackages={"com.coder520.mamabike"})這個注解來讓spring掃描我們指定包下的注解。

如果我們不用@MapperScan這個注解的話,也可以在接口類的上方加上@Mapper這個注解也可以。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM