1.创建项目
按着步骤来就行了
直接next就好
继续next
这里选择我们要使用的数据库 我这里使用的mysql,然后next
2.项目结构
完成后我们来看看相应项目结构
对项目结构有不清晰的地方请看这里有
可以检查下自己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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bai</groupId>
<artifactId>springboootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboootdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
3.项目配置
如果现在我们想启动springboot肯定会报错,因为这次我们整合了mybatis但是没有对mybatis进行相应的配置,该如何配置呢?
Spring Boot 使用一个全局的配置文件 application.properties 或 application.yml,放置在【src/main/resources】目录或者类路径的 /config 下
在配置前我们需要在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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baidu</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis 自动生成类-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!--防止反复生成-->
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--是否允许自动覆盖文件-->
<overwrite>true</overwrite>
<!--配置文件-->
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
这里我直接放上了完整的架包,具体解释也有相应的注释
这时候我们要加入两个配置文件:application.properties、mybatis-generator.xml
application.properties 是springboot默认使用的全局配置文件,或者使用application.yml也可以
配置项 | 说明 | 举例 |
---|---|---|
server.port | 应用程序启动端口 | server.port=8080,定义应用程序启动端口为8080 |
server.context-path | 应用程序上下文 | server.port=/api,则访问地址为:http://ip:port/api |
spring.http.multipart.maxFileSize | 最大文件上传大小,-1为不限制 | spring.http.multipart.maxFileSize=-1 |
spring.jpa.database | 数据库类型 | spring.jpa.database=MYSQL,指定数据库为mysql |
spring.jpa.properties.hibernate.dialect | hql方言 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect |
spring.datasource.url | 数据库连接字符串 | spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true |
spring.datasource.username | 数据库用户名 | spring.datasource.username=root |
spring.datasource.password | 数据库密码 | spring.datasource.password=root |
spring.datasource.driverClassName | 数据库驱动 | spring.datasource.driverClassName=com.mysql.jdbc.Driver |
spring.jpa.showSql | 控制台是否打印sql语句 | spring.jpa.showSql=true |
这里我使用的 application.properties
#服务器启动端口
server.port=8087
#mapper映射xml文件的所在路径
mybatis.mapperLocations = classpath:mapping/*.xml
spring.datasource.name=miaosha
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha
spring.datasource.username=root
spring.datasource.password=123456
#使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
mybatis-generator.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="application.properties"></properties>
<!--数据库驱动-->
<!--<classPathEntry location="mysql-connector-java-5.0.8-bin.jar"/>-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/miaosha" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.baidu.springbootdemo.dataobject" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.baidu.springbootdemo.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
<table tableName="user_password" domainObjectName="UserPasswordDO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
</context>
</generatorConfiguration>
配置完成后我们需要把我们在mybatis-generator.xml用到的路径创建出来,即生成mapping,model,dao
这里我直接放出我的路径:
所有文件创建完成后,还需要进行简单的配置。Run→Edit Configurations…,然后选择maven,配置Command line
Command line:mybatis-generator:generate
然后运行这个maven 就可以了
之后会生成这几个文件
对于mybatis-generator使用有疑问可以看这里
这里是sql创建表:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user_info`
-- ----------------------------
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(64) collate utf8_unicode_ci default '''''',
`gender` tinyint(4) default '0' COMMENT '//1代表男性,0代表女性',
`age` int(11) default NULL,
`telphone` varchar(255) collate utf8_unicode_ci default '''''',
`register_mode` varchar(255) collate utf8_unicode_ci default '"' COMMENT '//byphone,bywechat,byalipay',
`third_party_id` varchar(64) collate utf8_unicode_ci default '"',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Records of user_info
-- ----------------------------
-- ----------------------------
-- Table structure for `user_password`
-- ----------------------------
DROP TABLE IF EXISTS `user_password`;
CREATE TABLE `user_password` (
`id` int(11) NOT NULL auto_increment,
`encrpt_passowrd` varchar(128) collate utf8_unicode_ci default NULL,
`user_id` int(11) default NULL,
PRIMARY KEY (`id`)
)
4.测试
所有配置完成后我们进行测试:
SpringbootDemoApplication
package com.baidu.springbootdemo;
import com.baidu.springbootdemo.dao.UserDOMapper;
import com.baidu.springbootdemo.dataobject.UserDO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication(scanBasePackages = {"com.baidu.springbootdemo"})
@RestController
@MapperScan("com.baidu.springbootdemo.dao")
public class SpringbootDemoApplication {
@Autowired
private UserDOMapper userDOMapper;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
System.out.println("hello word!");
UserDO userDO = userDOMapper.selectByPrimaryKey(1);
if (userDO == null) {
return "用户对象不存在";
} else {
return userDO.getName();
}
}
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
- @RestController 注解: 该注解是 @Controller 和 @ResponseBody 注解的合体版
- @SpringBootApplication 是 Spring Boot 的核心注解,它是一个组合注解,该注解组合了:@Configuration、@EnableAutoConfiguration、@ComponentScan; 若不是用 @SpringBootApplication 注解也可以使用这三个注解代替
- @EnableAutoConfiguration 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置,例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。
- Spring Boot 还会自动扫描 @SpringBootApplication 所在类的同级包以及下级包里的 Bean ,所以入口类建议就配置在 grounpID + arctifactID 组合的包名下
- @MapperScan对mapper包扫描
如果需要源码可以留言,要的人多会传到github给大家下载