使用SpringBoot+MyBatis+MySQL完成面试题(部分)
2019-02-27 17:04
561 查看
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.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*Mapper.xml server.port=8081
注意,这里如果开启了
spring.datasource.driver-class-name=com.mysql.jdbc.Driver会报错:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2.创建好数据库和表。
3.在pom.xml文件里引入lombok依赖。
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency>
4.创建实体类ShopPojo:
package com.example.demo.Pojo; import lombok.Data; import lombok.ToString; @Data @ToString public class ShopPojo { private String id; private String shopName; }
5.ShopMapper.xml:
<?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.example.demo.Mapper.ShopMapper"> <resultMap id="shop" type="com.example.demo.Pojo.ShopPojo"> <id property="id" column="id"></id> <result property="shopName" column="shopName"></result> </resultMap> <select id="findAllShops" resultMap="shop"> SELECT * FROM shop </select> </mapper>
ShopMapper接口:
package com.example.demo.Mapper; import com.example.demo.Pojo.ShopPojo; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface ShopMapper { List<ShopPojo> findAllShops(); }
(@Mapper注意不要漏了)
6.ShopService 和 ShopServiceImpl:
package com.example.demo.Service; import com.example.demo.Pojo.ShopPojo; import java.util.List; public interface ShopService { List<ShopPojo> findAllShops(); }
package com.example.demo.Service.ServiceImpl; import com.example.demo.Mapper.ShopMapper; import com.example.demo.Pojo.ShopPojo; import com.example.demo.Service.ShopService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ShopServiceImpl implements ShopService { @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:
package com.example.demo.Controller; import com.example.demo.Pojo.ShopPojo; import com.example.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.测试结果:
遇到的问题:
数据库时区问题,可以这样解决:
相关文章推荐
- spring boot整合mybatis使用c3p0数据源连接mysql
- Spring Boot使用mybatis连接mysql
- spring boot整合mybatis使用c3p0数据源连接mysql
- 使用spring boot+mybatis+mysql 构建RESTful Service
- Spring Boot:在Spring Boot中使用Mysql和JPA
- springboot+springmvc+mybatis 使用注解对数据库的增、改和查操作
- SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
- eclipse 使用maven 搭建 springboot+mybatis + 多数据源
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题
- Spring Boot中MyBatis的使用
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- spring-boot 集成mybatis配置使用
- Spring-Boot整合mybatis(一),使用默认的数据库连接池
- SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建
- Spring Boot使用mybatis 入门
- springboot+shiro+mybatis整合发现部分功能事务没有被spring管理
- 使用Spring boot,Kotlin,Mysql建立外键,关联表
- MyBatis 使用Interceptor进行分页(springMVC+mybatis(mysql))
- 从零学spring boot--集成mybatis--项目--使用注解方式