(注意项目名不能有大写。。。。。。),把项目类型 改成 War 类型。(web项目)

使用 mybatis-generator 插件 生成 实体类 和 接口

在 resources 目录 中 新建一个 generatorConfig.xml 的文件

generatorConfig.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>
<context id="default" targetRuntime="MyBatis3Simple">
<!--创建class时,对注释进行控制-->
<commentGenerator>
<property name="suppressDate" value="true" />
<!--去除注释-->
<property name="suppressAllComments" value="true"/>
</commentGenerator> <!--jdbc的数据库连接-->
<jdbcConnection driverClass="org.mariadb.jdbc.Driver"
connectionURL="jdbc:mariadb://localhost:3306/test"
userId="oukele" password="oukele">
</jdbcConnection>
<!-- Model模型生成器
targetPackage -> 指定生成的model生成所在的包名
targetProject -> 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="com.oukele.myspringbootproject.entity" targetProject="src/main/java">
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.oukele.myspringbootproject.dao" targetProject="src/main/java"/> <!-- tableName 表名 % -> 全部表 -->
<table tableName="user">
<generatedKey column="id" sqlStatement="Mysql"/>
</table> </context>
</generatorConfiguration>

在 pom.xml 文件添加依赖

             <!-- mybatis.generator 插件  -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version> <configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<!-- 数据库依赖 -->
<dependencies>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</plugin>

生成成功。

现在 来 配置 application.properties 文件。

 #设置 Tomcat 端口号
server.port=8081 # 配置 mybatis
# 设置 别名类型包 (实体类)
mybatis.type-aliases-package=com.oukele.myspringbootproject.entity
# 设置 mybatis 映射 的 SQL语法 xml文件
mybatis.mapper-locations=classpath:mapper/*.xml # 连接数据库
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/test
spring.datasource.username=oukele
spring.datasource.password=oukele
# 设置数据源 (c3p0连接池)
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource #启动 热部署
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 文件

 <?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.oukele</groupId>
<artifactId>my-springboot-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>my-springboot-project</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-actuator</artifactId>
</dependency>
<!-- 资源库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--视图引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- springboot 自动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.3.0</version>
</dependency>
<!--c3p0 连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> <!-- mybatis.generator 插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version> <configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<!-- 数据库依赖 -->
<dependencies>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</plugin> </plugins>
</build> </project>

这里,我们启动项目,看下能不能启动。

首先在 templates 文件中 新建一个 index.html 网页

然后

最后,

启动成功

效果如下:

开始我们的主菜。

项目结构

service包中UserService接口

 package com.oukele.myspringbootproject.service;

 import com.oukele.myspringbootproject.entity.User;
import java.util.List; public interface UserService {
List<User> listAll();
}

serviceImp包中UserServiceImp类

 package com.oukele.myspringbootproject.serviceImp;

 import com.oukele.myspringbootproject.dao.UserMapper;
import com.oukele.myspringbootproject.entity.User;
import com.oukele.myspringbootproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UserServiceImp implements UserService { @Autowired
private UserMapper userMapper;//这里出现红色波浪线,并不影响使用 @Override
public List<User> listAll() {
return userMapper.selectAll();
}
}

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即可)

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%msg%n
</Pattern>
</layout>
</appender> <logger name="com.oukele.myspringbootproject.dao" level="TRACE" /> <root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>

重新启动(结果):

示例源码下载地址: https://github.com/oukele/my-SpringBoot-Project-One

使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项目的更多相关文章

  1. 使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项

    (注意项目名不能有大写......),把项目类型 改成 War 类型.(web项目) 使用 mybatis-generator 插件 生成 实体类 和 接口 在 resources 目录 中 新建一个 ...

  2. springboot + mybatis 前后端分离项目的搭建 适合在学习中的大学生

    人生如戏,戏子多半掉泪! 我是一名大四学生,刚进入一家软件件公司实习,虽说在大学中做过好多个实训项目,都是自己完成,没有组员的配合.但是在这一个月的实习中,我从以前别人教走到了现在的自学,成长很多. ...

  3. Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git小结(转)

    摘要 出于兴趣,想要搭建一个自己的小站点,目前正在积极的准备环境,利用Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git,这里总结下最近遇到的一些问题及解决 ...

  4. 使用gulp搭建一个传统的多页面前端项目的开发环境

    1.简介 使用gulp搭建一个传统的多页面前端项目的开发环境 支持pug scss es6编译支持 支持开发环境和打包生成sourceMap 支持文件变动自动刷新浏览器,css是热更新(css改动无需 ...

  5. SLF4J 和 Logback 在 Maven 项目中的使用方法

    原文:http://blog.csdn.net/llmmll08/article/details/70217120 本文介绍 SLF4J 和 Logback 在 Maven 项目中的用法,包括日志框架 ...

  6. 5分钟快速搭建一个springboot的项目

      现在开发中90%的人都在使用springboot进行开发,你有没有这样的苦恼,如果让你新建一个springboot开发环境的项目,总是很苦恼,需要花费很长时间去调试.今天来分享下如何快速搭建. 一 ...

  7. 使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目

    现在创建个项目: 勾上 自已 需要东西.(这里作为演示) maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} 关闭项目 ...

  8. 基于SpringBoot + Mybatis实现SpringMVC Web项目

    一.热身 一个现实的场景是:当我们开发一个Web工程时,架构师和开发工程师可能更关心项目技术结构上的设计.而几乎所有结构良好的软件(项目)都使用了分层设计.分层设计是将项目按技术职能分为几个内聚的部分 ...

  9. SpringBoot + Mybatis + Redis 整合入门项目

    这篇文章我决定一改以往的风格,以幽默风趣的故事博文来介绍如何整合 SpringBoot.Mybatis.Redis. 很久很久以前,森林里有一只可爱的小青蛙,他迈着沉重的步伐走向了找工作的道路,结果发 ...

随机推荐

  1. python数值列表

    使用range函数生成数值列表 使用range函数打印1~5的数字 for i in range(1,6): print(i) 输出 1 2 3 4 5 利用range函数生成数值列表 >> ...

  2. Oracle-DQL 6- 子查询

    子查询: --查询emp表中工资高于allen的员工信息SELECT sal FROM empWHERE ename = 'ALLEN'; SELECT * FROM empWHERE sal > ...

  3. Excel 下来公式 内容却一样

    首先我们打开我们电脑里面的excel2007的软件   我们随便输入一点输入,进行公式计算   我们在上边输入=A1+B1,就能算出这个的结果   我们把上边的公式算好了,点击下拉试试   我们发现虽 ...

  4. mybatis报错无法扫描到xml文件

    错误:java.io.IOException: Could not find resource com/xxx/xxxMapper.xml 原因:target目录下没有***.xml文件 解决方法:在 ...

  5. Django模板(Template)系统

    Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} 变量名由字母数字和下划线组成. 点 ...

  6. hdu 5651 重复全排列+逆元

    知识点: n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列. 若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为 ,即其重复度为ni! ...

  7. 非常有用的pointer-events属性

    介绍 pointer-events是css3的一个属性,指定在什么情况下元素可以成为鼠标事件的target(包括鼠标的样式) 属性值 pointer-events属性有很多值,但是对于浏览器来说,只有 ...

  8. 记项目管理大作业Web项目Mandrian的全流程[其一] 整体分析: 功能划分, 组织结构

    Mandrian是个图书管理系统, 具体需求老师给出 这个项目的目的主要是管理过程和高层设计的学习和实践 11人小组, 路人局 成员调查 这里由于很多人我都不认识, 所以我提前发了一个能力调查表, 调 ...

  9. Python实现串口通信(pyserial)

    pyserial模块封装了对串口的访问,兼容各种平台. 安装 pip insatll pyserial 初始化 简单初始化示例 import serial ser = serial.Serial('c ...

  10. dhcpd.conf例解

      ddns-update-style interim; //设置dhcp互动更新模式 ignore client-updates; //忽略客户端更新 #子网声明 subnet 192.168.12 ...