这边只在SpringBoot下进行简单查询的测试,接下来会博客会介绍增删改的操作。
数据库表结构如下:
开始测试:
1、新建工程(trymp_springboot)并把项目结构建立好
2、导入pom.xml的依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <!--测试的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--mysql驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency> <!--日志依赖--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <!--Druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency> </dependencies>
3、日志文件(log4j.properties)配置如下:
log4j.rootLogger=DEBUG,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.Al.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
4、SpringBoot核心配置文件(application.yml),如下:
server: port: 8080 spring: datasource: druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///chw?CharacterEncoding=utf8 username: root password: root
5、Mapper接口内容如下:
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.jieku.pojo.User; public interface UserMapper extends BaseMapper<User> { }
6、实体类内容如下:
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; @Data @NoArgsConstructor @AllArgsConstructor @ToString @TableName("tb_user") public class User { @TableId(type = IdType.AUTO) // 需要指定,否则无法新增后拿到回调的id,以及进行删除等操作 private Integer uid; private String uname; private Integer age; public User(String uname, Integer age) { this.uname = uname; this.age = age; } }
7、启动类内容如下:
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.jieku.mapper") // 设置mapper接口的扫描路径 public class MPApplication { public static void main(String[] args) { SpringApplication.run(MPApplication.class); } }
8、测试类内容如下:
import com.jieku.mapper.UserMapper; import com.jieku.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest // SpringBoot的测试 public class TestMPSpringboot { @Autowired private UserMapper userMapper; @Test public void test01(){ // 1.进行查询 List<User> users = userMapper.selectList(null); // 2.遍历打印输出 for (User user : users) { System.out.println(user); } } }
9、运行结果如下:
至此,简单查询测试结束!