springboot初体验之mybatis

来源:互联网 发布:周杰伦的床边故事知乎 编辑:程序博客网 时间:2024/05/16 07:34

注解的形式配置

  1. 配置pom.xml
  2. 编写数据源(application.properties)
  3. 编写实体类User
  4. 编写mapper
  5. 测试

1.完整pom.xml如下

    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>        <!--Mybatis-->        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.3.1</version>        </dependency>        <!--mybatis-->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.4.5</version>        </dependency>        <!--配置热部署-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <version>1.5.8.RELEASE</version>            <optional>true</optional>        </dependency>        <!--Junit测试-->        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>        </dependency>        <!--mysql-->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>    </dependencies>

2.application.properties配置

spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://localhost:3306/BookManageuseUnicode=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456

springboot会自动加载spring.datasource.*相关配置,数据源会自动注入要SqlSessionFactory

在启动类中加入@MapperScan,扫描Mapper包

@SpringBootApplication@MapperScan("com.jiang.mapper")public class SpringbootdemoApplication {    public static void main(String[] args) {        SpringApplication.run(SpringbootdemoApplication.class, args);    }}

3.编写User

public class User {    private String userName;    private String password;    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public User() {    }    public User(String userName, String password) {        this.userName = userName;        this.password = password;    } }

4.编写Mapper

@Mapperpublic interface UserMapper {    @Select("SELECT * FROM User")    @Results({            @Result(property = "username",column = "username",javaType = User.class),            @Result(property = "password",column = "password"),    })    List<User> getAllUser(); }

@Select注解中编写sql语句
@Result修饰返回结果集,关联实体类数据与数据库字段一一对应,如果实体类与数据库字段一致
,那么就不需要了


5.测试

@RestController@RequestMapping("/user")public class UserController {    @Autowired    private UserMapper userMapper;    @RequestMapping("/getAllUser")    public List<User> getAllUser(){        List<User> users = userMapper.getAllUser();        return users;    }}

打开浏览器访问:localhost:8080/user/getAllUser
在浏览器中就会返回JSON数据