這篇文章帶給大家的內容是關於springboot專案創作教學(IntelliJIDEA,springboot 2.0 mybatis),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
環境:
JDK8 windows10
步驟New Module —>Spring Initializr—>next
#1
2.
3。 web勾選web,sql裡面可以不勾,後續添加,另外,勾選了MyBatis會報錯Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 這樣的錯誤。這裡我勾選了是為了待會解決這個錯誤
選完直接下一步到最後就行了
自動產生的pom檔如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
## 目錄結構
因為沒有配置資料庫和任何文件,application是空的,預設連接埠是8080
我們需要在啟動累忽略資料庫配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
然後用postman存取 說明項目是正常的
4.設定資料庫 application.yml 檔案
server: port: 8080 tomcat: uri-encoding: UTF-8 servlet: context-path: / spring: dataSource: url: jdbc:mysql://localhost:3306/db-test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false username: root password: 123456 driverClassName: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:com/example/demo/mapper/*Mapper.xml #注意:一定要对应mapper映射xml文件的所在路径 type-aliases-package: com.example.demo.model # 注意:对应实体类的路径 configuration: call-setters-on-nulls: true # 解决使用map类型接收查询结果的时候为null的字段会没有的情况
这时候 com.mysql.jdbc.Driver 标红 说明mysql架包找不到
#把
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
改成 版本預設就行
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
然後driverClassName 恢復正常
导入 service dao xml (这里没有使用实体类返回,使用的是map返回,所以表我们可以随意自建) UserController
@Controller @RequestMapping("/usersDemo") public class UserController { private static Logger log = LoggerFactory.getLogger(UserController.class); @Resource UserService userService; @ResponseBody @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET}) public List<Map<String, Object>> test(){ log.info("进入了test方法!"); List<Map<String,Object>> list=userService.userQueryAll(); return list; } }
UserService
public interface UserService { List<Map<String, Object>> userQueryAll(); }
UserserviceImpl
@Service public class UserserviceImpl implements UserService { @Resource UserMapper userDao; @Override public List<Map<String, Object>> userQueryAll() { return userDao.userQueryAll(); } }
UserMapper
@Mapper public interface UserMapper { List<Map<String, Object>> userQueryAll(); }
UserMappererr. ##好了,
接下來就是啟動了
5啟動
注意:依賴注入後如果沒有去掉exclude = {DataSourceAutoConfiguration.class}
#啟動會報如下錯誤:
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
錯誤原因1:##
<?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.dao.UserMapper"> <select id="userQueryAll" parameterType="Map" resultType="Map"> SELECT * FROM `users` </select> </mapper>
DemoApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @MapperScan("com.example.demo.dao") @ComponentScan(basePackages = {"com.example.demo.*"}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
6 繼續啟動,
日誌是沒錯了, 然後存取資料庫的時候可能會出現這樣的錯誤org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.dao.UserMapper.userQueryAll#錯誤可能原因1
. mapper.xml select 的id寫錯了解決方案:檢查程式碼
錯誤可能原因2
.程式沒有編譯xml檔解決方案:pom.xml 檔案build裡面增加程式碼
很多人说 创建项目的时候勾选了mybatis导致 实际上是这个架包的原因 如果不要的话也不能使用mybatis了 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
dataSource.type 使用默认的连接池导致的
以上是springboot專案創建教學(IntelliJIDEA,springboot 2.0 +mybatis)的詳細內容。更多資訊請關注PHP中文網其他相關文章!