1:首先创建一个新的spring boot项目
2: 引入必要的依赖
3:配置数据源 ,在resources文件夹,新建的spring boot的项目下就有一个application.properties文件,本人喜欢yml结尾的,所以修改 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.projectlombok lombok true com.baomidou mybatis-plus-boot-starter 3.0.1 org.springframework.boot spring-boot-starter-web com.microsoft.sqlserver mssql-jdbc runtime org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus 3.0.2
application.properties文件名为 application.yml,最好修改一下, 配置文件内容就是配置一下数据源
spring: datasource: driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://localhost:1433;databaseName=Test username: sa password: root server: port: 8081 4: 项目整体结构如下
5:创建一个实体类 people
6:创建一个PeopleMapper类
7:在启动类上加上注解
@MapperScan(括号里面就是PeopleMapper类的路径)
8: 接下来就可以开始测试了
@RunWith(SpringRunner.class) @SpringBootTest public class MpApplicationTests { @Resource private PeopleMapper userMapper; //添加一个用户 @Test public void contextLoads() { People p = new People(); p.setName("b123"); p.setAge("1234"); userMapper.insert(p); //得到返回的主键ID int id = p.getId(); } //根据用户ID删除记录 @Test public void delete(){ userMapper.deleteById(5); } //根据主键修改 @Test public void update(){ People p = new People(); p.setName("bao"); p.setAge("18"); p.setId(1); userMapper.updateById(p); } //根据不同的条件查询 @Test public void select() { People p = new People(); // name 和 age 都是数据库People表的字段 //该查询语句==》查询 select * from People where name = 'b' and age between 124 and 1235; //当然后面还是可以继续添加条件 Listlist = userMapper.selectList(new QueryWrapper ().eq("name","b"). between("age",124,1235)); for(People people:list){ System.out.println(people.getAge()+"====="+people.getName()); } } }