使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项目
(注意项目名不能有大写。。。。。。),把项目类型 改成 War 类型。(web项目)
使用 mybatis-generator 插件 生成 实体类 和 接口
在 resources 目录 中 新建一个 generatorConfig.xml 的文件
generatorConfig.xml 文件信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <context id="default" targetRuntime="MyBatis3Simple"> 8 <!--创建class时,对注释进行控制--> 9 <commentGenerator> 10 <property name="suppressDate" value="true" /> 11 <!--去除注释--> 12 <property name="suppressAllComments" value="true"/> 13 </commentGenerator> 14 15 <!--jdbc的数据库连接--> 16 <jdbcConnection driverClass="org.mariadb.jdbc.Driver" 17 connectionURL="jdbc:mariadb://localhost:3306/test" 18 userId="oukele" password="oukele"> 19 </jdbcConnection> 20 <!-- Model模型生成器 21 targetPackage -> 指定生成的model生成所在的包名 22 targetProject -> 指定在该项目下所在的路径 23 --> 24 <javaModelGenerator targetPackage="com.oukele.myspringbootproject.entity" targetProject="src/main/java"> 25 <!-- 是否对类CHAR类型的列的数据进行trim操作 --> 26 <property name="trimStrings" value="true" /> 27 </javaModelGenerator> 28 <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件--> 29 <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> 30 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 31 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 32 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 33 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 34 --> 35 <javaClientGenerator type="XMLMAPPER" targetPackage="com.oukele.myspringbootproject.dao" targetProject="src/main/java"/> 36 37 <!-- tableName 表名 % -> 全部表 --> 38 <table tableName="user"> 39 <generatedKey column="id" sqlStatement="Mysql"/> 40 </table> 41 42 </context> 43 </generatorConfiguration>
在 pom.xml 文件添加依赖
1 <!-- mybatis.generator 插件 --> 2 <plugin> 3 <groupId>org.mybatis.generator</groupId> 4 <artifactId>mybatis-generator-maven-plugin</artifactId> 5 <version>1.3.7</version> 6 7 <configuration> 8 <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> 9 <overwrite>true</overwrite> 10 </configuration> 11 <!-- 数据库依赖 --> 12 <dependencies> 13 <dependency> 14 <groupId>org.mariadb.jdbc</groupId> 15 <artifactId>mariadb-java-client</artifactId> 16 <version>2.3.0</version> 17 </dependency> 18 </dependencies> 19 </plugin>
生成成功。
现在 来 配置 application.properties 文件。
1 #设置 Tomcat 端口号 2 server.port=8081 3 4 # 配置 mybatis 5 # 设置 别名类型包 (实体类) 6 mybatis.type-aliases-package=com.oukele.myspringbootproject.entity 7 # 设置 mybatis 映射 的 SQL语法 xml文件 8 mybatis.mapper-locations=classpath:mapper/*.xml 9 10 # 连接数据库 11 spring.datasource.driver-class-name=org.mariadb.jdbc.Driver 12 spring.datasource.url=jdbc:mariadb://localhost:3306/test 13 spring.datasource.username=oukele 14 spring.datasource.password=oukele 15 # 设置数据源 (c3p0连接池) 16 spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource 17 18 #启动 热部署 19 spring.devtools.livereload.enabled=true
如果嫌 上面的写法 太啰嗦 可将 application.properties 文件 后缀名改成 yml
内容改成如下:
# mybatis 配置
mybatis:
typeAliasesPackage: com.oukele.myspringbootproject.entity
mapperLocations: classpath:mapper/*.xml
spring:
datasource:
driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/test
username: oukele
password: oukele
# 数据源
type: com.mchange.v2.c3p0.ComboPooledDataSource
# 这里是默认配置
spring.devtools.restart.enabled: true
# 设置重启的目录
spring.devtools.restart.additional-paths: src/main/java
# classpath目录下的WEB-INF文件夹内容修改不重启
server:
port: 8081
pom.xml 文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.1.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.oukele</groupId> 12 <artifactId>my-springboot-project</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <packaging>war</packaging> 15 <name>my-springboot-project</name> 16 <description>Demo project for Spring Boot</description> 17 18 <properties> 19 <java.version>1.8</java.version> 20 </properties> 21 22 <dependencies> 23 <!--监控容器--> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-actuator</artifactId> 27 </dependency> 28 <!-- 资源库 --> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-data-jpa</artifactId> 32 </dependency> 33 <!--视图引擎--> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-thymeleaf</artifactId> 37 </dependency> 38 <!-- web --> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-web</artifactId> 42 </dependency> 43 <!-- mybatis --> 44 <dependency> 45 <groupId>org.mybatis.spring.boot</groupId> 46 <artifactId>mybatis-spring-boot-starter</artifactId> 47 <version>1.3.2</version> 48 </dependency> 49 <!-- 热部署--> 50 <dependency> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-devtools</artifactId> 53 <scope>runtime</scope> 54 </dependency> 55 <!-- Tomcat --> 56 <dependency> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-starter-tomcat</artifactId> 59 <scope>provided</scope> 60 </dependency> 61 <!-- springboot 自动配置 --> 62 <dependency> 63 <groupId>org.springframework.boot</groupId> 64 <artifactId>spring-boot-starter-test</artifactId> 65 <scope>test</scope> 66 </dependency> 67 <!--数据库驱动--> 68 <dependency> 69 <groupId>org.mariadb.jdbc</groupId> 70 <artifactId>mariadb-java-client</artifactId> 71 <version>2.3.0</version> 72 </dependency> 73 <!--c3p0 连接池--> 74 <dependency> 75 <groupId>com.mchange</groupId> 76 <artifactId>c3p0</artifactId> 77 <version>0.9.5.2</version> 78 </dependency> 79 </dependencies> 80 81 <build> 82 <plugins> 83 <plugin> 84 <groupId>org.springframework.boot</groupId> 85 <artifactId>spring-boot-maven-plugin</artifactId> 86 </plugin> 87 88 <!-- mybatis.generator 插件 --> 89 <plugin> 90 <groupId>org.mybatis.generator</groupId> 91 <artifactId>mybatis-generator-maven-plugin</artifactId> 92 <version>1.3.7</version> 93 94 <configuration> 95 <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> 96 <overwrite>true</overwrite> 97 </configuration> 98 <!-- 数据库依赖 --> 99 <dependencies> 100 <dependency> 101 <groupId>org.mariadb.jdbc</groupId> 102 <artifactId>mariadb-java-client</artifactId> 103 <version>2.3.0</version> 104 </dependency> 105 </dependencies> 106 </plugin> 107 108 </plugins> 109 </build> 110 111 </project>
这里,我们启动项目,看下能不能启动。
首先在 templates 文件中 新建一个 index.html 网页
然后
最后,
启动成功
效果如下:
开始我们的主菜。
项目结构
service包中UserService接口
1 package com.oukele.myspringbootproject.service; 2 3 import com.oukele.myspringbootproject.entity.User; 4 import java.util.List; 5 6 7 public interface UserService { 8 List<User> listAll(); 9 }
serviceImp包中UserServiceImp类
1 package com.oukele.myspringbootproject.serviceImp; 2 3 import com.oukele.myspringbootproject.dao.UserMapper; 4 import com.oukele.myspringbootproject.entity.User; 5 import com.oukele.myspringbootproject.service.UserService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import java.util.List; 10 11 @Service 12 public class UserServiceImp implements UserService { 13 14 @Autowired 15 private UserMapper userMapper;//这里出现红色波浪线,并不影响使用 16 17 @Override 18 public List<User> listAll() { 19 return userMapper.selectAll(); 20 } 21 }
controller包中Usercontroller类
package com.oukele.myspringbootproject.controller; import com.oukele.myspringbootproject.entity.User; import com.oukele.myspringbootproject.serviceImp.UserServiceImp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired private UserServiceImp userServiceImp; @GetMapping(path = "/list") public List<User> getAll(){ return userServiceImp.listAll(); } }
类的修改。。。
package com.oukele.myspringbootproject; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.oukele.myspringbootproject.dao")//扫描接口 public class MySpringbootProjectApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootProjectApplication.class, args); } }
重启项目,访问/list 的结果:
使用logback日志:
在resources文件中 新建logback-spring.xml 文件 (springboot默认集成了 logback 直接在resources创建 logback-spring.xml即可)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 4 <layout class="ch.qos.logback.classic.PatternLayout"> 5 <Pattern> 6 %msg%n 7 </Pattern> 8 </layout> 9 </appender> 10 11 <logger name="com.oukele.myspringbootproject.dao" level="TRACE" /> 12 13 <root level="error"> 14 <appender-ref ref="STDOUT" /> 15 </root> 16 </configuration>
重新启动(结果):
示例源码下载地址: https://github.com/oukele/my-SpringBoot-Project-One