链接数据库,简单的crud不用自己写,话不多说直接上步骤
第一步,自己准备一个数据库,创建一个简答的springboot项目,相信大家都会就不用我演示了
首先配置ymal文件
server:port: 8081
spring:datasource:url: jdbc:mysql:driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 123456mybatis-plus:mapper-locations: classpath*:/mapper
第二步创建实体导报
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>provided</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>3.4.3.1</version></dependency><!-- https:<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.25</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.4.1</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3.1</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>2.5.1</version></dependency><!-- https:<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency><!-- https:<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.76</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
第3步创建实体
&#64;Data
&#64;TableName("ye_chuan_jiang")
public class YeChuanJiang implements Serializable {private Integer id;private String name;private Integer age;
}
第4步创建service
public interface YeChuanJiangService extends IService<YeChuanJiang> {
}
第5步实现层impl
&#64;Service
public class YeChuanJiangServiceImpl extends ServiceImpl<YechuanjiangMapper, YeChuanJiang> implements YeChuanJiangService {}
第6步创建mapper
&#64;Mapper
public interface YechuanjiangMapper extends BaseMapper<YeChuanJiang> {
}
第7创建ampper的xml
看配置文件应该知道我是在创建的mapper的xml
具体内容&#xff1a;
<?xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace&#61;"com.example.springcloud_mode.mapper.YechuanjiangMapper">
</mapper>
接下来就是controller层了
&#64;RestController
&#64;RequestMapping("/ye")
public class YeChuanJiangController {&#64;Resourceprivate YeChuanJiangService yechuanjiangService;&#64;GetMapping("/get")public YeChuanJiang get(){YeChuanJiang byId &#61; yechuanjiangService.getById(1);return byId;}&#64;GetMapping("/add")public YeChuanJiang add(){YeChuanJiang yeChuanJiang &#61; new YeChuanJiang();yeChuanJiang.setAge(27);yeChuanJiang.setName("xioaye");boolean save &#61; yechuanjiangService.save(yeChuanJiang);return yechuanjiangService.getOne(Wrappers.lambdaQuery(YeChuanJiang.class).eq(YeChuanJiang::getAge,27));}}
多动手&#xff0c;经验都是实践出来的
学无止境&#xff0c;加油&#xff01;