使用idea创建springboot项目
项目下载地址:
解压后的 demo09
1、环境准备
开发环境:idea 2020.1.1,jdk 1.8,maven3.6.0(
jdk安装教程
)(
idea2020.1.1安装教程
)
(maven的安装配置)
数据库
create database demo;
use demo;
create table user
(
id int auto_increment
primary key,
username varchar(20) null,
gender varchar(1) null,
age int(3) null
);
2、创建项目
提高构建速度,配置下面的参数
https://start.aliyun.com/
其余的默认即可
springboot版本和插件选择
项目名及位置
3、代码编写
创建如图所示的目录结构
application.properties
server.port=8001
server.servlet.context-path=/demo09
spring.application.name=demo09
# mysql数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8&useSSL=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations= classpath:mappers/*.xml
mybatis.type-aliases-package= com.mystery.domain
Demo09Application.java
package com.mystery;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("com.mystery.dao")
public class Demo09Application {
public static void main(String[] args) {
SpringApplication.run(Demo09Application.class, args);
}
}
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.mystery.dao.UserMapper">
<select id="getUserName" parameterType="int" resultType="String">
select username from user WHERE id=#{id}
</select>
</mapper>
pom.xml
<?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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.mystery</groupId>
<artifactId>demo09</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo09</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--线程池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<!--添加配置跳过测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--添加配置跳过测试-->
</plugins>
</build>
</project>
运行结果
版权声明:本文为qq_43430759原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。