mybatis之抛弃XML,拥抱注解

  • @Select 查询SQL注解
  • @Update 修改SQL注解
  • @Insert 插入数据SQL注解
  • @Delete 删除SQL注解
  • @Results 结果集字段与属性映射
  • @Result 单个字段与属性映射
  • @ResultMap 根据ID关联XML里面
  • @SelectProvider 查询高级注解-mybatis3
  • @InsertProvider 插入高级注解-mybatis3
  • @UpdateProvider 修改高级注解-mybatis3
  • @DeleteProvider 删除高级注解-mybatis3

1、pom引入jar

<dependencies>
        <dependency> 
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency> 
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

2、application.properties配置mybtis相关参数

#配置项:驼峰转换
mybatis.map-underscore-to-camel-case=true

3、在数据层接口类中增加注解

@Mapper
public interface DaoMapper {}

4、简单基础注解

@Results({
     @Result(property = "userId", column = "USER_ID"),
     @Result(property = "username", column = "USERNAME")
})
@Select("select * from tb_user")
List getUserList();

5、高级注解

@SelectProvider(type = UserSqlProvider.class, method = "getUserListByName")
List<User> getUserListByName(@Param("name") String name);
public class UserSqlProvider {
    public String getUserListByName(@Param("name") String name){
     return "select * from tb_user where username like #{name}";
    }
}

你可能感兴趣的:(mybtis)