mybatis基本用法


1、引入pom
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>

2、pom中开启驼峰命名转换
mybatis:
configuration:
map-underscore-to-camel-case: true

3、编写数据库访问接口
@Repository
public interface XXXDao {
@Select("<script>" +
"SELECT * FROM t_xx where valid = 1 " +
" <if test=\"des!= null and des !=''\"> and des like CONCAT('%',#{des,jdbcType=VARCHAR},'%')</if>\n" +
" order by id desc limit #{pageStart},#{pageSize}" +
"</script>")
List<ListXxVo> listXXX(XXInput input);

//插入并获取自增长主键
@Insert("INSERT INTO t_tree_node(parent_id,node_name,node_type) VALUES (#{parentId},#{nodeName},#{nodeType})")
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn = "id")
long addNode(AddNodeReq input);

@SelectProvider(type= XXSqlUtil.class, method = "countXXSql")
int countXX(Input input);

public String countXXSql(Input input){
return "SELECT COUNT(1) FROM t_device_card where valid = 1";
}
}

4、Application入口增加注解
@MapperScan("xxx.*.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
posted @ 2021-07-05 11:33  w20200618  阅读(46)  评论(0)    收藏  举报