Home>Article>Java> Springboot project creation tutorial (IntelliJIDEA, springboot 2.0 +mybatis)

Springboot project creation tutorial (IntelliJIDEA, springboot 2.0 +mybatis)

不言
不言 forward
2019-03-08 15:44:08 4717browse

The content of this article is about the springboot project creation tutorial (IntelliJIDEA, springboot 2.0 mybatis). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Environment:

JDK8 windows10

StepsNew Module —>Spring Initializr—>next

1

2.

3. Check web for web. You can uncheck sql and add it later. In addition,If MyBatis is checked, an error will be reported such as Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required. I checked it here to solve this error later

After selecting, just go to the next step and go to the end

The automatically generated pom file is as follows

  4.0.0  org.springframework.boot spring-boot-starter-parent 2.0.8.RELEASE    com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot  1.8    org.springframework.boot spring-boot-starter-jdbc   org.springframework.boot spring-boot-starter-web   org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0   mysql mysql-connector-java runtime   org.springframework.boot spring-boot-starter-test test      org.springframework.boot spring-boot-maven-plugin    

## Directory structure

Because there is no configuration database and any files, the application is empty, and the default port is 8080

We need to ignore the database configuration at startup

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

Then use postman to access it, indicating that the project is normal

4. Configure the database application.yml file

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架包找不到

Remove06db57cb000bdd2564c5b32a302b10e2runtime03b1008234ba0cf6ad3c873aea327e8a. He means that it is not needed at runtime, but it is actually needed.

 mysql mysql-connector-java runtime 

Just change it to the default version

 mysql mysql-connector-java 

Then driverClassName returns to normal

导入 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> test(){ log.info("进入了test方法!"); List> list=userService.userQueryAll(); return list; } }
UserService
public interface UserService { List> userQueryAll(); }
UserserviceImpl
@Service public class UserserviceImpl implements UserService { @Resource UserMapper userDao; @Override public List> userQueryAll() { return userDao.userQueryAll(); } }

UserMapper

@Mapper public interface UserMapper { List> userQueryAll(); }

UserMapper.xml

    
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); } }

Okay,

The next step is to start

5Start

Note: If exclude = {DataSourceAutoConfiguration.class}

## is not removed after dependency injection #The following error will be reported at startup:

Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

Error reason 1:

很多人说 创建项目的时候勾选了mybatis导致 实际上是这个架包的原因 如果不要的话也不能使用mybatis了  org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 

Error Reason 2,

dataSource.type 使用默认的连接池导致的

Solution 1: Remove exclude = {DataSourceAutoConfiguration.class}, and it will start normally

Solution 2: Use other connection pools, such as Alibaba druid connection pool

 com.alibaba druid-spring-boot-starter 1.1.13 

Then modify the spring.dataSource.type=com.alibaba.druid.pool.DruidDataSource

in application.yml without deleting exclude = {DataSourceAutoConfiguration.class} , in fact it is of little significance and is a low-level error

#6 Continue to start, the log is correct, and it may appear when accessing the database

Such an error

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.dao.UserMapper.userQueryAll

Possible cause of error 1

. The id of select in mapper.xml is written incorrectly

Solution: Check the code

Possible cause of error 2

. The program is not compiled xml file

Solution: Add code to the pom.xml file build

  src/main/java  **/*.xml  false  

Start again:

Success

The final pom.xml file is as follows

  4.0.0  org.springframework.boot spring-boot-starter-parent 2.0.8.RELEASE    com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot  1.8    org.springframework.boot spring-boot-starter-jdbc   org.springframework.boot spring-boot-starter-web   org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0    com.alibaba druid-spring-boot-starter 1.1.13   mysql mysql-connector-java ${mysql.version}   org.springframework.boot spring-boot-starter-test test      org.springframework.boot spring-boot-maven-plugin     src/main/java  **/*.xml  false    

The above is the detailed content of Springboot project creation tutorial (IntelliJIDEA, springboot 2.0 +mybatis). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete