springboot整合mybatis

引入依赖
<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.0.1.RELEASEversion>
    <relativePath/> 
parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>

    
    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
        <version>1.1.1version>
    dependency>

    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
    dependency>
dependencies>
配置application.properties文件
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

#配置mybatis的信息
#spring集成Mybatis环境
# pojo别名扫描包
mybatis.type-aliases-package=com.itcast.domain
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
配置UserMapper.xml
 
<mapper namespace="com.itcast.mapper.UserMapper">
    <select id="queryUserList" resultType="user">
        select * from user
    select>
mapper>
接口
@Mapper
public interface UserMapper {
    public List<User> queryUserList();
}
实体类
public class User {
    private Integer id;
    private String username;
    private String password;
    private String name;}
controller
@Controller
public class MyBatisController {

    @Resource
    UserMapper userMapper;

    @RequestMapping("/queryUser")
    @ResponseBody
    public List<User> selAll(){
        return userMapper.queryUserList();
    }
}
引导类
@SpringBootApplication
public class SpringbootMybatisApplication {

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

}

你可能感兴趣的:(SpringBoot)