使⽤SpringBoot+MyBatis+MySQL完成⾯试题(部分)1.先搭建项⽬,选择Web、MyBatis、MySQL等依赖。
整体项⽬格式:
配置⽂件:
spring.datasource.url=jdbc:mysql://localhost:3306/Interview?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123
#spring.datasource.sql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*l
server.port=8081
注意,这⾥如果开启了
spring.datasource.sql.jdbc.Driver会报错:
长度英语怎么读Loading class `sql.jdbc.Driver'. This is deprecated. The new driver class is `sql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of th
e driver class is generally unnecessary.
2.创建好数据库和表。
3.在l⽂件⾥引⼊lombok依赖。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
4.创建实体类ShopPojo:
ample.demo.Pojo;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class ShopPojo {
private String id;
private String shopName;asccii码
}
l:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"> <mapper namespace="ample.demo.Mapper.ShopMapper">
<resultMap id="shop" type="ample.demo.Pojo.ShopPojo">
<id property="id" column="id"></id>
<result property="shopName" column="shopName"></result>
</resultMap>
<select id="findAllShops" resultMap="shop">
watermelon sugar歌词SELECT * FROM shop
</select>
</mapper>
ShopMapper接⼝:
ample.demo.Mapper;
ample.demo.Pojo.ShopPojo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ShopMapper {
List<ShopPojo>findAllShops();
mysql面试题基础知识
}
(@Mapper注意不要漏了)
6.ShopService 和 ShopServiceImpl:
ample.demo.Service;
ample.demo.Pojo.ShopPojo;
import java.util.List;
public interface ShopService {
List<ShopPojo>findAllShops();
}
ample.demo.Service.ServiceImpl;
ample.demo.Mapper.ShopMapper;
ample.demo.Pojo.ShopPojo;
ample.demo.Service.ShopService;
ps里两个图形怎么相切import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ShopServiceImpl implements ShopService {
tokenizer bert@Autowired
private ShopMapper shopMapper;
@Override
public List<ShopPojo>findAllShops(){
List<ShopPojo> shops = shopMapper.findAllShops();
for(ShopPojo shop : shops){
System.out.println(shop);
}
return shops;
}
}
7.ShopController:
ample.demo.Controller;
ample.demo.Pojo.ShopPojo;
ample.demo.Service.ShopService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ShopController {
@Autowired
private ShopService shopService;
@RequestMapping("/findAllShops")
public List<ShopPojo>findAllShops(){
return shopService.findAllShops();}
}
@RestController:是@RequestBody和@Controller的合体。
8.测试结果:
遇到的问题:
数据库时区问题,可以这样解决:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。