赞
踩
- <dependencies>
- <!--mybatis坐标-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.2.2</version>
- </dependency>
- <!--mysql-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.29</version>
- </dependency>
-
- <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>
- </dependencies>
-
- #数据源
- spring:
- datasource:
- username: root
- password: 123456
- url: jdbc:mysql://localhost:3307/testdb?serverTimezone=GMT
- driver-class-name: com.mysql.cj.jdbc.Driver
-
- public class Account {
- private int aid;
- private String aname;
- private int amoney;
- }
@Mapper//注册注入一个mapper
@MapperScan(basePackages ="com.ztt.mybatis_springboot.mapper")//注册注入多个mapper(以包为单位) )
注意:两者只能写一个
- //@Mapper//注册注入一个mapper
- public interface AccountMapper {
- @Select("select * from account")
- public List<Account> findAll();
- }
- @SpringBootTest
- class MybatisSpringbootApplicationTests {
-
- @Autowired(required = false)
- AccountMapper accountMapper;
-
- @Test
- void contextLoads() {
- List<Account> all=accountMapper.findAll();
- for (int i = 0; i <all.size() ; i++) {
- Account account=all.get(i);
- System.out.println(account);
-
- }
- }
-
- }
public List<Account> find();
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
-
- <mapper namespace="com.ztt.mybatis_springboot.mapper.AccountMapper">
- <select id="find" resultType="com.ztt.mybatis_springboot.pojo.Account">
- select * from account;
- </select>
- </mapper>
- mybatis:
- mapper-locations: mappers/*.xml
- @Test
- void contextLoads1() {
- List<Account> all=accountMapper.find();
- for (int i = 0; i <all.size() ; i++) {
- Account account=all.get(i);
- System.out.println(account);
-
- }
- }
MyBatis最佳搭档,只做增强不做改变,为简化开发、提高效率而生。
详细信息请看官方文档https://www.wpsshop.cn/w/笔触狂放9/article/detail/1011741
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。