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

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

不言
Release: 2019-03-08 15:44:08
forward
4866 people have browsed it

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

<?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>
Copy after login

## 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的字段会没有的情况
Copy after login
这时候 com.mysql.jdbc.Driver 标红 说明mysql架包找不到
Copy after login

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
Copy after login

Just change it to the default version

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
Copy after login

Then driverClassName returns to normal

导入 service dao xml (这里没有使用实体类返回,使用的是map返回,所以表我们可以随意自建)

UserController
Copy after login
@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;
    }
}
Copy after login
UserService
Copy after login
public interface UserService {
    List<Map<String, Object>> userQueryAll();
}
Copy after login
UserserviceImpl
Copy after login
@Service
public class UserserviceImpl  implements UserService {
    @Resource
    UserMapper userDao;
    @Override
    public List<Map<String, Object>> userQueryAll() {
        return userDao.userQueryAll();
    }
}
Copy after login

UserMapper

@Mapper
public interface UserMapper {
    List<Map<String, Object>> userQueryAll();
}
Copy after login

UserMapper.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.dao.UserMapper">
    <select id="userQueryAll" parameterType="Map" resultType="Map">
        SELECT * FROM `users`
    </select>
</mapper>
Copy after login
DemoApplication
Copy after login
@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);
    }

}
Copy after login

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了 
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
Copy after login

Error Reason 2,

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

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

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

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.13</version>
</dependency>
Copy after login

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

<resources>
<resource>
    <directory>src/main/java</directory>
    <includes>
        <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
</resource>
</resources>
Copy after login

Start again:

Success

The final pom.xml file is as follows

<?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>
        <!-- alibaba的druid数据库连接池监控依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.13</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </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>
        <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        </resources>
    </build>

</project>
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template