五、SpringBoot整合mybaties

1.所需依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
<!--            <version>2.3.1.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
<!--            <version>5.1.46</version>-->
        </dependency>
<!--        整合mybaties启动器-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

2.编写Mapper:

@Mapper//要求mybaties版本3.4以上
public interface IUserMapper {

    @Select("select * from user where name like '%${value}%'")
    public List<User> findUserByName(String name);
}

另外service及其实现类,以及Controller添加如下代码:

//service添加
List<User> findUserByName(String name);
//impl添加
    @Autowired
    private IUserMapper iUserMapper;
    @Override
    public List<User> findUserByName(String name) {
        return iUserMapper.findUserByName(name);
    }    
//controller添加
    @RequestMapping("/findUserByName/{name}")//restful编程风格
    public List<User> findUserByName(@PathVariable("name") String name){
        List<User> users = userService.findUserByName(name);
        System.out.println(users);
        return users;
    }

3.启动项目测试:

 

posted @ 2019-05-25 02:49  指尖下的世界  阅读(500)  评论(0编辑  收藏  举报
/* 看板娘 */ /*炸泡*/
/* 鼠标点击求赞文字特效 */